[英]Java socket will send but then not receive response
I am currently learning Java
and am playing around with socket programming.我目前正在学习
Java
并且正在玩套接字编程。
My goal is to implement request-response communication like HTTP
.我的目标是实现像
HTTP
一样的请求-响应通信。 I can get one way communication from the client to the server.我可以获得从客户端到服务器的一种通信方式。 But when I program the client to listen for a response it causes the server to not print out the request.
但是当我对客户端进行编程以侦听响应时,它会导致服务器不打印出请求。
I've googled the issue and most posts mention using the flush()
method, which I have tried.我在谷歌上搜索了这个问题,大多数帖子都提到了使用我尝试过的
flush()
方法。 Anyone have any thoughts?有人有什么想法吗?
Here is the client code:这是客户端代码:
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);
}
}
}
and here is my server code:这是我的服务器代码:
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");
}
}
}
}
This is because your sever currently loop until read()
returns -1
which never happens in your case so he never reaches the part of the code where he is supposed to send Bar
, you need a specific marker
to indicate the end of your message so for example here you can send data line by line, for this proceed as next:这是因为您的服务器当前循环直到
read()
返回-1
这在您的情况下永远不会发生所以他永远不会到达他应该发送的代码部分Bar
,您需要一个特定的marker
来指示您的消息的结尾例如在这里您可以逐行发送数据,为此继续如下:
Client side:客户端:
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());
Output:输出:
Sending request
Sending Complete
Bar
Server side:服务器端:
// Print the line sent by the client
System.out.println(in.readLine());
// Send a message to the client
out.println("Bar");
Output:输出:
Listening on port 9999
Foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.