在Java中通过同一TCP连接发送多个字符串?

在Java中通过同一TCP连接发送多个字符串?,java,tcp,Java,Tcp,我已经试了好几个小时了,但似乎还是做不成。我想从TCP客户端向服务器发送两个字符串,并将它们都大写返回。这两个字符串是用户名和密码: 客户端代码为: public void actionPerformed(ActionEvent e) { final String s1 =textPane.getText(); //retrieve username final String s2=passwordField.getText();//retrie

我已经试了好几个小时了,但似乎还是做不成。我想从TCP客户端向服务器发送两个字符串,并将它们都大写返回。这两个字符串是用户名和密码: 客户端代码为:

public void actionPerformed(ActionEvent e) {
            final String s1 =textPane.getText();  //retrieve username
            final String s2=passwordField.getText();//retrieve password         
            //**************************************************************************
            //after we have gotten the credentials, we have to send them to the server to check in the database
            Socket clientSocket = null; //create new socket
            String response=null; //create what is supposed to become the server's response
            String response2=null;
            try {
                /**
                 * construct the client socket, specify the IP address which is here
                 * localhost for the loopback device and the port number at which a
                 * connection needs to be initialized.
                 */
                clientSocket = new Socket("localhost", 6789);
                // These streams are for the same purpose as in the server side //
                DataOutputStream outToServer = new DataOutputStream(clientSocket
                        .getOutputStream());
                DataOutputStream outToServer2 = new DataOutputStream(clientSocket
                        .getOutputStream());

                BufferedReader inFromServer = new BufferedReader(
                        new InputStreamReader(clientSocket.getInputStream()));
                // read the user's input from the console. //

                // send the sentence to the server: note the need to append it with
                // an endline character //
                outToServer.writeBytes(s1 + '\n'); //send username
                outToServer.writeBytes(s2 + '\n'); //send password
                // listen on the socket and read the reply of the server //
                response2=inFromServer.readLine();
                response = inFromServer.readLine();

            } catch (UnknownHostException ex) {// catch the exceptions //
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println("FROM SERVER: " + response); //print the response of the server
            System.out.println("FROM SERVER: " + response2);
            try {
                // close the socket when done //
                clientSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    });
大部分服务器代码是:

  while(true) {
        // connection socket //
        Socket connectionSocket;
        try {
            // accept connection on the connection socket from the welcome socket which is a server socket //
            connectionSocket = welcomeSocket.accept();
            // Get the input stream from the connection socket and save it in a buffer //
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            // Allocate an output stream to send data back to the client //
            DataOutputStream  outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            // Read the client sentence from the receive buffer //
            clientSentence = inFromClient.readLine();
            clientSentence2=inFromClient.readLine();

            System.out.println("From user: " + clientSentence);
            // capitalize the client's sentence, here the server does actions on the client's data //
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
           capitalizedSentence2=clientSentence2.toUpperCase()+'\n';

            if(clientSentence.equalsIgnoreCase("QUIT")){// close the socket if the server sends a quit message //
               connectionSocket.close();
               System.out.println("Connection Socket Quitting");
               // Note that here the server will not exit or end up it will just close the connection with the client //
            } 
            else outToClient.writeBytes(capitalizedSentence);// Send the capitalized sentence back to the client //
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

我做错了什么?一个缓冲读取器不能接受两个单独的字符串吗?以后如何将它们分开?

您还没有将CapitalizedEntence2写回客户端,这就是为什么客户端在第二个
readLine()处等待的原因,如下所述:

        response2 = inFromServer.readLine();
        response = inFromServer.readLine();
在else部分的服务器端代码的下面一行

 outToClient.writeBytes(capitalizedSentence2);

它可能会解决你的问题。

它挂了吗?或者抛出任何错误?还有什么?不要在事件线程上执行网络操作。哪个线程被冻结了?客户端还是服务器?您是否得到任何输出?它解决了冻结问题,但现在响应和响应2完全相同。谢谢btwMy从服务器输出的
:从服务器输入的密码:用户名
我从客户端发送了用户名和密码。请再次检查您在流中写入的内容。还有一个人认为您在
响应
之前设置了
响应2
。可以吗?