Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要客户端能够从服务器接收多条消息_Java_Sockets_Server_Connection_Client Server - Fatal编程技术网

Java 我需要客户端能够从服务器接收多条消息

Java 我需要客户端能够从服务器接收多条消息,java,sockets,server,connection,client-server,Java,Sockets,Server,Connection,Client Server,我正在处理一个基本的客户机-服务器连接。 此代码工作正常,但客户端在关闭连接之前只能发送1条消息并接收其修改。 我怎样才能发送和接收多条消息 我曾想过使用while循环,但我不知道如何正确地实现它。 我需要能够发送一个以上的消息,以便有一个一致的连接 下面的代码是客户端向服务器发送字符串,服务器将其转换为大写 //Server: public class TCPServer { public static void main(String argv[]) throws Exceptio

我正在处理一个基本的客户机-服务器连接。 此代码工作正常,但客户端在关闭连接之前只能发送1条消息并接收其修改。 我怎样才能发送和接收多条消息

我曾想过使用while循环,但我不知道如何正确地实现它。 我需要能够发送一个以上的消息,以便有一个一致的连接

下面的代码是客户端向服务器发送字符串,服务器将其转换为大写

//Server:
public class TCPServer {

    public static void main(String argv[]) throws Exception 
    {
       String clientSentence;
       String capitalizedSentence;

       ServerSocket welcomeSocket = new ServerSocket(6789);
       while(true)
                    {
           Socket connectionSocket = welcomeSocket.accept(); 

           BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
           DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 

           clientSentence = inFromClient.readLine();

           capitalizedSentence = clientSentence.toUpperCase() + '\n';
           outToClient.writeBytes(capitalizedSentence);

           if(clientSentence.toUpperCase().trim().contentEquals("QUIT")) {
               connectionSocket.close();
               }
           }
       }
    }

//Client:
public class TCPClient {

    public static void main(String argv[]) throws Exception
    { 
        String sentence;
        String modifiedSentence;
        Socket clientSocket = new Socket("LocalHost", 6789);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.print("Enter characters to be capitalized: ");
        sentence = inFromUser.readLine(); 
        outToServer.writeBytes(sentence + '\n'); 
        modifiedSentence = inFromServer.readLine(); 

        System.out.println("FROM SERVER: " + modifiedSentence);
        }
    }
此代码的输出为:

Enter characters to be capitalized: hi
FROM SERVER: HI

您的服务器只能从每个客户端获取一条消息,因为在while循环中,在每次迭代中您都调用
welcomeSocket.accept()
。这意味着您的服务器代码在获得新的客户端连接之前停止


如果希望服务器支持多个客户端,请考虑使用多线程。例如,看一看:

我没有尝试将多个用户连接到同一服务器,我需要在同一客户端和服务器之间发送多条消息,而不关闭连接。@R.Bassil,但问题与我提到的相同。您的服务器正在等待接受新客户机的每次迭代。如果只想接受一个客户机,请在while循环之前执行此操作。