简单客户端服务器程序的问题

时间:2016-01-08 19:31:54

标签: java client-server

我编写了双向客户端 - 服务器通信,其中客户端首先发送消息,然后服务器发送回复。但是,客户端在读取服务器发送的回复时被阻止(即,客户端没有打印任何内容,但服务器已正确发送回复)。以下是代码: -

服务器代码: -

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
    // Socket for accepting connections.
    private static Socket socket;

    public static void main( String[] args )
    {
        try
        {
            int port = 2550;
            // Creating server socket on specified port.
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server started listening on port "+port);

            while(true)
            {
                // Accept the connection and read the message.
                socket = serverSocket.accept();
                BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream()));
                String message = br.readLine();
                System.out.println("Message recieved from client is :- " + message);

                // Prepare and send the response back to client.
                BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
                String reply = "Thanks.Your reply has been recieved";
                bw.write(reply);
                bw.flush();
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch( Exception e )
            {
            }

        }
    }
}

客户代码: -

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client
{
    private static Socket socket;

    public static void main( String[] args )
    {
        try
        {
            String host = "localhost";
            int port = 2550;

            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(address, port);

            // Send the message to the server.
            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
            String message = "This is the message from Client.\n";
            bw.write(message);
            bw.flush();
            System.out.println("Message sent to the server :- " + message);

            // Get the reply from the server.
            BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream()));
            String reply = br.readLine();
            System.out.println("Message recieved from the server :- " + reply);
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            // Closing the socket.
            try
            {
                socket.close();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }
}

D:\Java_P>java Server
Server started listening on port 2550
Message recieved from client is :- This is the message from Client.

D:\Java_P>java Client
Message sent to the server :- This is the message from Client.

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为您的服务器发送了“谢谢。您的回复已收到”,其中不包含任何换行符。原因是因为在客户端,缓冲读取器readline方法阻塞,直到找到换行符。

进行以下更改:

String reply = "Thanks.Your reply has been recieved\nDummy line 1";

现在,您可以在客户端日志中看到这些行。