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

时间:2018-11-22 21:06:06

标签: sockets tcp server client serversocket

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

服务器代码:

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);
    }



}
}

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

1 个答案:

答案 0 :(得分:0)

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