2017-07-02 182 views
-4

我試圖創建服務器和客戶端之間的雙向通信,其中雙方(服務器和客戶端)可以互相發送消息,而無需等待另一方回覆。爲此,我創建了一個單獨的線程來偵聽套接字並在屏幕上打印傳入的消息。這是真正的問題:當我運行程序後,我第一次編譯和運行服務器和客戶端時,一切正常,我從服務器和客戶端都收到此錯誤消息。Java套接字編程,雙向通信

java.net.ConnectException: Connection refused (Connection refused) 
at java.net.PlainSocketImpl.socketConnect(Native Method) 
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
at java.net.Socket.connect(Socket.java:589) 
at java.net.Socket.connect(Socket.java:538) 
at java.net.Socket.<init>(Socket.java:434) 
at java.net.Socket.<init>(Socket.java:211) 
at Build.work(c.java:18) 
at s.main(s.java:83) 

Exception in thread "Thread-0" Exception in thread "main" java.lang.NullPointerException 
at Build.run(c.java:59) 
at java.lang.Thread.run(Thread.java:748) 

java.lang.NullPointerException 
at Build.speak(c.java:35) 
at c.main(c.java:85) 

服務器

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

class Build implements Runnable 
{ 
    int port = 3500; 
    String input1; 
    ServerSocket server; 
    Socket socket; 
    DataInputStream in; 
    DataOutputStream out; 
    Console reader; 

    public void work() 
    { 
     try 
     { 
      server = new ServerSocket(port); 
      socket = server.accept(); 
      in = new DataInputStream(socket.getInputStream()); 
      out = new DataOutputStream(socket.getOutputStream()); 
      reader = System.console(); 
     } 

     catch(IOException e){ 
     e.printStackTrace();} 
    } 

    public void speak() 
    { 
     try 
     { 
      while(true) 
      { 
       input1 = reader.readLine(); 
       out.writeUTF(input1); 
       if(input1.equals("stop")) 
       { 
        in.close(); 
        out.close(); 
        socket.close(); 
        server.close(); 
        System.exit(0); 
       }    
      } 
     } 
     catch (IOException e){ 
     e.printStackTrace();} 
    } 

    public void run() 
    { 
     String input2; 

     try 
     { 
      while(true) 
      { 
       input2 = in.readUTF(); 
       System.out.println(input2); 
       if(input2.equals("stop")) 
       { 
        in.close(); 
        out.close(); 
        socket.close(); 
        server.close(); 
        System.exit(0); 
       } 
      } 
     } 
     catch(IOException e){ 
     e.printStackTrace();} 
    } 
} 

public class server 
{ 
    public static void main(String[]args) 
    { 
     Build object = new Build(); 
     object.work(); 
     Thread t1 = new Thread(object); 
     t1.start(); 
     object.speak(); 
    } 
} 

客戶

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

class Build implements Runnable 
{ 
    String ip = "127.0.0.1"; 
    int port = 3500; 
    String input1; 
    Socket socket; 
    DataInputStream in; 
    DataOutputStream out; 
    Console reader; 

    public void work() 
    { 
     try 
     { 
      socket = new Socket(ip, port); 
      in = new DataInputStream(socket.getInputStream()); 
      out = new DataOutputStream(socket.getOutputStream()); 
      reader = System.console(); 
     } 

     catch(IOException e){ 
     e.printStackTrace();} 
    } 

    public void speak() 
    { 
     try 
     { 
      while(true) 
      { 
       input1 = reader.readLine(); 
       out.writeUTF(input1); 
       if(input1.equals("stop")) 
       { 
        in.close(); 
        out.close(); 
        socket.close(); 
        System.exit(0); 
       }    
      } 
     } 
     catch (IOException e){ 
     e.printStackTrace();} 
    } 

    public void run() 
    { 
     String input2; 

     try 
     { 
      while(true) 
      { 
       input2 = in.readUTF(); 
       System.out.println(input2); 
       if(input2.equals("stop")) 
       { 
        in.close(); 
        out.close(); 
        socket.close(); 
        System.exit(0); 
       } 
       } 
     } 
     catch(IOException e){ 
     e.printStackTrace();} 
    } 

} 

public class client 
{ 
    public static void main(String[]args) 
    { 
     Build object = new Build(); 
     object.work(); 
     Thread t1 = new Thread(object); 
     t1.start(); 
     object.speak(); 
    } 
} 
+0

@arghavan沒有'afterward'這樣的詞。 – EJP

+0

您的編輯沒有「可以隨意增加」,它從根本上改變了問題。不要這樣做。 – EJP

回答

1

這裏沒有服務器。服務器將創建一個ServerSocket,然後開始撥打accept()

您在這裏只有兩個客戶。

您也可能在readUTF()readLime()之間感到困惑。 readUTF()只能讀取由writeUTF()寫入的數據,而readLine()不能。

+0

謝謝指出!格式化代碼時,我一定弄錯了一些東西。問題仍然存在,我得到相同的錯誤信息。 EJP是否提出了一個可以創建雙向通信的工作解決方案? – Mikey

+0

直到您展示您的當前代碼。我們無法讀懂你的想法。 – EJP

+0

固定!對不起,麻煩:) – Mikey