2017-08-19 122 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谢谢。你不喜欢手机自动更正... –

相关问题