Java TCP发送第一条消息,然后无限期等待

时间:2013-05-19 15:27:56

标签: java tcp

我的问题基于以下代码:

  • 运行TCPServer.java
  • 然后运行TCPClient.java

我希望客户打印出来

服务器说(1):HEY DUDE 1

服务器说(2):HEY DUDE 2

......但它仍然留在HEY DUDE 1.我在做什么不能产生我想要的结果?

TCPServer.java

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

class TCPServer {
    public static void main (String args[]) throws Exception{
        new TCPServer();
    }
    TCPServer() throws Exception{
        //create welcoming socket at port 6789
        ServerSocket welcomeSocket = new ServerSocket(6789);

        while (true) {
            //block on welcoming socket for contact by a client
            Socket connectionSocket = welcomeSocket.accept();
            // create thread for client
            Connection c = new Connection(connectionSocket);
        }
    }
    class Connection extends Thread{
        Socket connectionSocket;
        Connection(Socket _connectionSocket){
            connectionSocket = _connectionSocket;
            this.start();
        }
        public void run(){
            try{
                //create input stream attached to socket
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream()));
                //create output stream attached to socket
                PrintWriter outToClient = new PrintWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
                //read in line from the socket
                String clientSentence = inFromClient.readLine();
                System.out.println("Client sent: "+clientSentence);
                //process
                String capitalizedSentence = clientSentence.toUpperCase() + '\n';
                //write out line to socket
                outToClient.print(capitalizedSentence);
                outToClient.flush();
            }catch(Exception e){}
        }
    }
}

TCPClient.java

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

class TCPClient {
    //String name="";
    String host = "localhost";
    int port = 6789;
    Socket socket = null;
    public static void main(String args[]) throws Exception{
        TCPClient client = new TCPClient();
        client.SendToServer("Hey dude 1");
        System.out.println("Server Said(1): "+client.RecieveFromServer());
        client.SendToServer("Hey dude 2");
        System.out.println("Server Said(2): "+client.RecieveFromServer());
        client.close();
    }

    TCPClient(String _host, int _port) throws Exception{
        host = _host;
        port = _port;
        socket = new Socket(host, port);
    }
    TCPClient() throws Exception{
        socket = new Socket(host, port);
    }
    void SendToServer(String msg) throws Exception{
        //create output stream attached to socket
        PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        //send msg to server
        outToServer.print(msg + '\n');
        outToServer.flush();
    }
    String RecieveFromServer() throws Exception{
        //create input stream attached to socket
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream()));
        //read line from server
        String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException 
        return res;
    }
    void close() throws IOException{
        socket.close();
    }
}

1 个答案:

答案 0 :(得分:6)

处理第一条消息后,您的服务器线程就会结束。您需要将服务器代码放入这样的循环中:

String clientSentence;
while ((clientSentence = inFromClient.readLine()) != null) {
    System.out.println("Client sent: "+clientSentence);
    //process
    String capitalizedSentence = clientSentence.toUpperCase() + '\n';
    //write out line to socket
    outToClient.print(capitalizedSentence);
    outToClient.flush();
}