TCP 服务器未从 TCP 客户端接收消息 (Java)

TCP Server Not Receiving Message From TCP Client (Java)

服务器似乎没有收到客户端发送的消息。据我了解,客户端正在写入套接字输出流。服务器正在从套接字输入流中读取。请帮忙。

服务器代码:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
static final int DEFAULT_PORTNUMBER = 1236;

public static void main(String[] args){
    int portnumber;

    if(args.length >= 1){
        portnumber = Integer.parseInt(args[0]);
    }else{
        portnumber = DEFAULT_PORTNUMBER;
    }

    //Setting a server socket and a possible client socket
    ServerSocket server = null;
    Socket client;

    try{
        server = new ServerSocket(portnumber);
    } catch (IOException e){
        e.printStackTrace();
    }
    while(true){

        try{
            System.out.println("Waiting for client...");
            client = server.accept();
            System.out.println("Client accepted... ");
            //Read data form the client
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

            while(!br.ready()){
                System.out.println("No message from client");
            }
            String msgFromClient = br.readLine();
            //System.out.println("Message received from client = " + msgFromClient);

            //Send Response
            if(msgFromClient != null && !msgFromClient.equalsIgnoreCase("bye")){
                OutputStream clientOut = client.getOutputStream();
                PrintWriter pw = new PrintWriter(clientOut, true);
                String ansMsg = "Hello, " + msgFromClient;
                pw.println(ansMsg);
            }

            if(msgFromClient != null && msgFromClient.equalsIgnoreCase("Bye")){
                server.close();
                client.close();
                break;
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
        //New thread for client
        /*new ServerThread(client).start();
        System.out.println("Client connection accepted... ");*/
    }

}
}

客户代码:

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;


public class TCPClient {
static final int DEFAULT_PORTNUMBER = 1236;
public static void main(String args[]){
    Socket client = null;
    int portnumber;

    //Default port number if not specified as an argument

    if(args.length >= 1){
        portnumber = Integer.parseInt(args[0]);
    }else{
        portnumber = DEFAULT_PORTNUMBER;
    }


    try {
        String msg = "";

        //Creating a client socket
        client = new Socket(InetAddress.getLocalHost(), portnumber);
        System.out.println("Client socket is created: " + client);

        //Creating an output stream for the client socket
        OutputStream clientOUt = client.getOutputStream();
        PrintWriter pw = new PrintWriter(clientOUt, true);

        //Creating an input stream for the client socket
        InputStream clientIn = client.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));

        //Creating a buffered reader for standard input System.in
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter your name. Type Bye to exit.");

        //Read data from standard input and write to output stream
        msg = stdIn.readLine().trim();
        pw.print(msg);

        while(!br.ready()){
            //System.out.println("No Input From Server");
        }
        //Read data from input stream of client socket
        System.out.println("Message returned from the server = " + br.readLine());

        pw.close();
        br.close();
        client.close();

        //Stop operation
        if (msg.equalsIgnoreCase("Bye")) {
            System.exit(0);
        } else {
        }

    } catch (IOException e) {
        System.out.println("I/O error " + e);
    }



}
}

注意:我确实禁用了防火墙,但这没有帮助。

找到答案PrintWriter or any other output stream in Java do not know "\r\n"。它描述了 printwriter 如何无法使用 printwriter.print() 正确刷新,而是仅在您使用 printwriter.println() 时才有效。