[英]Java SocketServer is accepting input from a Socket client but the Socket client is not receiving input from the SocketServer
I'm trying to learn Java Network Programming but I'm running into some roadblocks. 我正在尝试学习Java网络编程,但是遇到了一些障碍。 I've written a server and a client already but every time I try to connect them, I instantly get a connection closed error.
我已经写了一个服务器和一个客户端,但是每次尝试连接它们时,我都会立即收到连接关闭错误。 Then, I tried to edit it but now I get a connection refused error.
然后,我尝试对其进行编辑,但现在出现连接拒绝错误。 Giving up on that, I decided to work on a very simple server to test out the basics of Sockets and ServerSockets.
放弃了这一点,我决定在一个非常简单的服务器上工作,以测试Sockets和ServerSockets的基础。 Doing so, I've come up with these two classes:
这样做,我提出了以下两个类:
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class SimpleServer {
public static void main(String[] args) throws Exception {
System.out.println("hey there");
ServerSocket server = new ServerSocket(50010);
Socket socket = server.accept();
System.out.println("Connection at " + socket);
InputStream in = socket.getInputStream();
int c = 0;
while ((c = in.read()) != -1) {
System.out.print((char)c);
}
OutputStream out = socket.getOutputStream();
for (byte b : (new String("Thanks for connecting!")).getBytes()) {
out.write(b);
out.flush();
}
in.close();
out.close();
socket.close();
server.close();
}
}
and 和
import java.net.Socket;
import java.io.*;
public class SimpleClient {
public static void main(String[] args) throws Exception {
System.out.println("Attempting connection");
Socket s = new Socket("130.49.89.208", 50010);
System.out.println("Cool");
OutputStream out = s.getOutputStream();
for (byte b : (new String("Hey server\r\nThis message is from the client\r\nEnd of message\r\n")).getBytes()) {
out.write(b);
out.flush();
}
InputStream in = s.getInputStream();
int c = 0;
System.out.println("this message will print");
while ((c = in.read()) != -1) {
System.out.print((char)c);
System.out.println("this does not print");
}
out.close();
in.close();
s.close();
}
}
The server receives the client's message perfectly fine, but then when it is the server's turn to write to the client, everything just blocks. 服务器完全可以接收到客户端的消息,但是轮到服务器写客户端时,一切都将阻塞。
Server's output: 服务器的输出:
-java SimpleServer
----hey there
----Connection at Socket[addr=/130.49.89.208,port=59136,localport=50010]
----Hey server
----This message is from the client
----End of message
Client's Output: 客户的输出:
-java SimpleClient
----Attempting connection
----Cool
----this message will print
The client and the server both run on my laptop on an ethernet connection to a university's internet connection, if that helps. 如果有帮助,客户端和服务器都可以在我的笔记本电脑上通过以太网连接到大学的Internet连接上运行。
According to the Javadocs, the InputStream.read()
is described as: 根据Javadocs,
InputStream.read()
描述为:
If no byte is available because the end of the stream has been reached, the value -1 is returned.
如果由于已到达流的末尾而没有字节可用,则返回值-1。 This method blocks until input data is available, the end of the stream is detected, or an exception is thrown .
此方法将阻塞,直到可用输入数据,检测到流的末尾或引发异常为止。
In your case, the only possibility to break the while-loop is when the client closes the connection, thereby resulting in end-of-stream. 在您的情况下,打破while循环的唯一可能性是客户端关闭连接时,从而导致流结束。
This is expected as per what you've coded! 根据您的编码,这是预期的!
Your server code is getting stuck here and thus never writing back to the client 您的服务器代码被卡在这里,因此永远不会写回客户端
while ((c = in.read()) != -1) {
System.out.print((char)c);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.