Java套接字将发送但不接收响应

时间:2016-05-11 16:49:12

标签: java sockets

我目前正在学习Java并正在使用套接字编程。

我的目标是实现HTTP之类的请求 - 响应通信。我可以从客户端到服务器进行单向通信。但是当我对客户端进行编程以侦听响应时,它会导致服务器不打印请求。

我已经使用flush()方法搜索了该问题和大多数帖子,我尝试过这种方法。有人有什么想法吗?

以下是客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

class Client {
    public static void main(String args[]) throws IOException {

        // Get the port number from the first command line argument

        int port_number;

        if(args.length == 0) {
            port_number = 80;
        } else {
            port_number = Integer.parseInt(args[0]);
        }

        try (

            // Create a socket
            Socket socket = new Socket("localhost", port_number);

            // Input reader
            BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream())
            );

            // Output writer
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        ) {

            System.out.println("Sending request");
            out.print("Foo");
            out.flush();

            System.out.println("Sending Complete");

            // If this next section is commented out then the server prints the message

            StringBuilder message = new StringBuilder();
            int data;

            while((data = in.read()) != -1) {
                message.append((char) data);
            }

            System.out.print(message);

        }
    }
}

这是我的服务器代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String args[]) throws IOException {

        // Get the port number from the first command line argument
        int portNumber = Integer.parseInt(args[0]);

        System.out.println("Listening on port " + portNumber);
        ServerSocket serverSocket = new ServerSocket(portNumber);

        while (true) {

            try (
                Socket socket = serverSocket.accept();
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(socket.getInputStream())
                );
            ){  

            StringBuilder message = new StringBuilder();
            int data;

            while((data = in.read()) != -1) {
                message.append((char) data);
            }

            System.out.println(message);

            out.print("Bar");

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为您的服务器当前循环直到read()返回-1,这在您的情况下从未发生过,所以他永远不会到达他应该发送Bar的代码部分,你需要一个特定的marker来表示您的消息结束,例如,您可以逐行发送数据,以便继续下一步:

客户端:

System.out.println("Sending request");
// Send a line to the server
out.println("Foo");
out.flush();

System.out.println("Sending Complete");

// Read a line sent by the server
System.out.print(in.readLine());

<强>输出:

Sending request
Sending Complete
Bar

服务器端:

// Print the line sent by the client
System.out.println(in.readLine());
// Send a message to the client
out.println("Bar");

<强>输出:

Listening on port 9999
Foo