2017-08-19 181 views
-1

我正在編寫一個Java套接字程序,用於將JSON對象從客戶端發送到另一個客戶端。現在,我只是通過發送由控制檯輸入的字符串數據來測試它。當我在控制檯中鍵入一些內容並將其發送到服務器時,服務器應該將其打印出來併發送回客戶端,然後客戶端也應該打印出來。但是我的服務器程序沒有在控制檯中顯示任何內容,客戶端也沒有。我找不到問題所在。Java套接字無法將數據發送到服務器

服務器:

public class GameServer { 
private Socket socket; 
private ServerSocket serverSocket; 
private ArrayList<GameServerThread> threads; 

public GameServer(){ 
    try{ 
     //Create new server socket 
     serverSocket = new ServerSocket(1234,100); 
     System.out.println("Waiting for clients ..."); 
     threads = new ArrayList<GameServerThread>(); 
     //Receive request from client 
     while(true){ 
      socket = serverSocket.accept(); 
      //Create new Thread once receive a request 
      new Thread(new GameServerThread(socket, this)).start();  
     } 

    }catch(IOException e){ 
     System.out.println("Failed to create socket!"); 
     e.printStackTrace(); 
    }finally{ 
     try { 
      serverSocket.close(); 
     } catch (IOException e) {    
      e.printStackTrace(); 
     } 
    }  
} 



public ArrayList<GameServerThread> getAllThreads(){ 
    return this.threads; 
} 


public void addNewThread(GameServerThread t){ 
    this.threads.add(t); 
} 



public static void main(String args[]){ 
    GameServer server = new GameServer(); 
} 
} 

ServerThread:

public class GameServerThread extends Thread{ 
private Socket socket; 
private GameServer server; 
private OutputStreamWriter writer; 
private BufferedReader reader; 
String userName; 

public GameServerThread(Socket s, GameServer srv) throws UnsupportedEncodingException, IOException{ 
    System.out.println("Succeeded to connect with one client!"); 
    this.socket = s; 
    this.server = srv; 
    writer = new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"); 
    reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8")); 
    this.server.addNewThread(this); 
} 


public void run(){ 
    try{ 
     while(true){ 
      receive(); 
     } 
    }catch(SocketException e){ 
     e.printStackTrace(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    }finally{  
     try { 
      this.socket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


private void receive() throws IOException{ 
    String str = null; 
    while((str=reader.readLine()) != null){ 
     System.out.println(str); 
     sendToOther(str);  } 
} 


public void send(String msg){ 
    try{ 
     writer.write(msg); 
     writer.flush(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 


private void sendToOther(String msg){ 
    for(GameServerThread thread: this.server.getAllThreads()){ 
     //if(!thread.equals(this)){ 
      thread.send(msg); 
     //} 
    } 
} 

客戶:

public class GameClient { 
private Socket socket; 
private String serverIP; 
private OutputStreamWriter writer; 
private BufferedReader reader; 

public GameClient(String host){ 
    this.serverIP = host; 

    try{ 
     //Connect to server 
     socket = new Socket(InetAddress.getByName(serverIP), 1234); 
     writer = new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"); 
     reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8")); 
     //Start a new thread for reading from server 
     new Thread(new GameClientThread(socket)).start(); 

     Scanner scanner = new Scanner(System.in); 
     System.out.println("Write something: "); 
     String str = ""; 
     while((str = scanner.nextLine()) != null){ 
      writer.write(str); 
      writer.flush(); 
      System.out.println("Write something: "); 
     } 
    }catch(IOException e){ 
     System.out.println("Client failed to connect!"); 
     e.printStackTrace(); 
} 
} 

客戶端線程:

public class GameClientThread extends Thread{ 
private Socket socket; 
private BufferedReader reader; 

//private OutputStreamWriter writer; 
//private String userName = ""; 

public GameClientThread(Socket soc){ 
    this.socket = soc; 

    try{ 
     reader = new BufferedReader(new InputStreamReader (this.socket.getInputStream(), "UTF-8")); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 


} 


public void run(){ 
    while(true){ 
     receive(); 
    } 
} 


private void receive(){ 
    String msg; 
    try { 
     msg = reader.readLine(); 
     System.out.println(msg); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 
+0

對不起,這不是StackOverflow的工作原理。形式問題_「這是我的一堆代碼,它不起作用,有人能幫我弄清楚」_被認爲是無關緊要的。請訪問[幫助]並閱讀[問]獲取更多信息,尤其是閱讀[爲什麼是「有人可以幫助我?」不是一個實際問題?](http://meta.stackoverflow.com/q/284236/18157 ) –

回答

0

BufferedReader.readLine需要在每行末尾添加一行(\ n,\ r或\ r \ n),但在寫入數據時不要寫入新行。每次您寫入一個尚未以新行結尾的字符串時,請添加writer.write('\n');

+0

「發育不良數據」是什麼意思? –

+0

@Jim謝謝。你不喜歡手機自動更正... –

相關問題