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

时间:2014-04-12 22:48:53

标签: java tcp

我已经尝试了几个小时,我似乎无法完成它。我想从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();
        }

    }

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

1 个答案:

答案 0 :(得分:0)

您还没有将首都资本写回客户,这就是客户在第二个readLine()等待的原因,如下所述:

        response2 = inFromServer.readLine();
        response = inFromServer.readLine();

将下面的行放在其他部分

中的服务器端代码中
 outToClient.writeBytes(capitalizedSentence2);

它可能会解决您的问题。

相关问题