2012-12-08 69 views
0

與客戶端的連接我創建了一個服務器和客戶端應用程序,它的工作原理。但客戶端或服務器不能正常工作。如果你運行服務器並在同一臺機器上運行客戶端,它就可以工作。但是如果你在一臺機器上運行服務器並在另一臺機器上運行客戶機,則它不起作用。如果你能幫我找出爲什麼它不允許連接或如何建立兩臺計算機之間的連接,那將是非常好的。謝謝你加載。代碼如下:如何建立局域網

server----------------------------------------------------------------- 

import java.io.*; 
import java.net.*; 

import javax.swing.*; 

public class AdditionalServer extends JFrame { 
    private JTextArea textWindow= new JTextArea(); 
    private int port; 

    // the constructor 
    public AdditionalServer(int portIn) 
    { 
     port = portIn; 
     setTitle("Addition Sever"); 
     add("Center",textWindow); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(400,300); 
     setVisible(true); 
     startServer(); 
    } 

    private void startServer() { 
     // TODO Auto-generated method stub 
     // declare a "general" socket and a server socket 
     Socket connection; 
     ServerSocket listenSocket; 
     //declare low level and high level objects for input 
     InputStream inStream; 
     DataInputStream inDataStream; 

     // declare low level and high level objects for output 
     OutputStream outStream; 
     DataOutputStream outDataStream; 

     // declare other variables 
     String client; 

     boolean connected; 

     while(true){ 
      try{ 
       // create a server socket 
       listenSocket= new ServerSocket(port,0, InetAddress.getLocalHost()); 
      // listenSocket= new ServerSocket(port); 
       textWindow.append("Listening on port "+ port +"\n"); 

       //listen for a connection from the client 
       connection =listenSocket.accept(); 
       connected = true; 

       // create an input stream from the client 
       inStream = connection.getInputStream(); 
       inDataStream = new DataInputStream(inStream); 

       // create an output stream from the client 
       outStream = connection.getOutputStream(); 
       outDataStream = new DataOutputStream(outStream); 

       // wait for a string from the client 
       client = inDataStream.readUTF(); 
       textWindow.append("Connection esablished with "+ client+ "\n"); 

       int first, second,sum1; 
       String sum = "hi"; 
       while(connected){ 
        //read an integer from the client 
        first = inDataStream.readInt(); 
        textWindow.append("First number receievd: "+ first + "\n"); 

        //read an integer from the client 
        second = inDataStream.readInt(); 
        textWindow.append("Second number receievd: "+ second + "\n"); 

        sum1 = first + second; 
        textWindow.append("Sum returned: " + sum1 +"\n"); 

        // send the sum to the client 
        outDataStream.writeInt(sum1); 
        //outDataStream.writeUTF("hi"); 
       } 
      }catch(IOException e){ 
       connected = false; 
      } 
     } 
    } 

    public static void main (String args []){ 
     new AdditionalServer(8900); 

    } 
} 

client---------------------------------------------------------------------- 
import java.net.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class AdditionClient extends JFrame implements ActionListener { 

    // declare the visual components 

    private JTextField firstNumber = new JTextField(3); 
    private JLabel plus = new JLabel("+"); 
    private JTextField secondNumber = new JTextField(3); 
    private JLabel equals = new JLabel("="); 
    private JLabel sum= new JLabel(); 
    private JTextField msg = new JTextField(20); 
    private JButton addButton= new JButton("Press to see the sum of the two numbers"); 

    // declare low level and high level objects for input 
    private InputStream inStream; 
    private DataInputStream inDataStream; 

    // declare low level and high level objects for output 
    private OutputStream outStream; 
    private DataOutputStream outDataStream; 

    // declare socket 
    private Socket connection; 

    // declare attribute to told details of remote machine and port 
    private String remoteMachine; 
    private int port; 

    // constructor 

    public AdditionClient(String remoteMachineIn, int portIn){ 
     remoteMachine = remoteMachineIn; 
     port= portIn; 

     //add the visual components 
     add(firstNumber); 
     add(plus); 
     add(secondNumber); 
     add(equals); 
     add(sum); 
     add(msg); 
     add(addButton); 

     // configure the frame 
     setLayout(new FlowLayout()); 
     setTitle("Addtion Client"); 
     msg.setHorizontalAlignment(JLabel.CENTER); 
     addButton.addActionListener(this); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(300,150); 
     setVisible(true); 

     //start the helper method that starts the client 
     startClient(); 
    } 

    private void startClient() { 
     // TODO Auto-generated method stub 
     try{ 
      // attempt to create a connection to the server 
      connection = new Socket(remoteMachine,port); 
      msg.setText("connection establish"); 

      // create an input stream from the server 
      inStream = connection.getInputStream(); 
      inDataStream = new DataInputStream(inStream); 

      //create output stream to the server 
      outStream = connection.getOutputStream(); 
      outDataStream = new DataOutputStream(outStream); 

      //send the host IP to the server 
      outDataStream.writeUTF(connection.getLocalAddress().getHostAddress()); 

     }catch (UnknownHostException e){ 
      msg.setText("Unknow host"); 
     } 
     catch (IOException except){ 
      msg.setText("Network Exception"); 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 
     try{ 
      // send the two integers to the server 
      outDataStream.writeInt(Integer.parseInt(firstNumber.getText())); 
      outDataStream.writeInt(Integer.parseInt(secondNumber.getText())); 

      //read and display the results sent back from the server 
      //String results= inDataStream.readUTF(); 
      int results= inDataStream.readInt(); 
      sum.setText(""+results); 
     }catch(IOException ie){ 
      ie.printStackTrace(); 
     } 
    } 

    public static void main (String args[]){ 
     new AdditionClient("192.168.07", 8900); 
    } 
} 
+0

聽起來像一個網絡問題。檢查正確的IP地址或主機名,檢查防火牆,檢查打開的端口。 –

+0

我試圖禁用煙花,仍然是一樣的。我正在使用InetAddress.getLocalHost();所以應該在所有的時間 – user629283

回答

2

我認爲你的問題在這裏。

listenSocket= new ServerSocket(port,0, InetAddress.getLocalHost()); 

InetAddress.getLocalHost()應該是你綁定IP。在這裏提供你的機器ip,以及哪些應該對你的客戶也是可見的。我建議你發送一個ping來自客戶機器的請求。

嘗試做一個ping "your bind ip"檢查您的服務器的機器是通過網絡訪問。

或者只給端口嘗試。

listenSocket= new ServerSocket(port) 
+0

拿到機器的IP地址,你的意思是這樣listenSocket =新的ServerSocket(端口,192.168.0.0.1);類似的東西? – user629283

+0

listenSocket =新ServerSocket的(端口)的作品,但我得到了相同的結果 – user629283

+0

確定服務器和客戶端的網絡連接之間?你可以使用pin命令來檢查它。 – Suranga