[英]TCP Client/Server
I have this code and for some reason it stucks at readline() line at servers end always waiting from client but client on the other end sends the data. 我有这段代码,由于某种原因,它总是卡在服务器端的readline()行上,总是等待客户端,但另一端的客户端发送数据。
Both the server and client's code is available below. 服务器和客户端的代码均在下面提供。
import java.io.*;
import java.net.*;
public class TCPServer {
public static final int SERVER_PORT = 6789;
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(SERVER_PORT);
while (true) {
Socket connectSocket = welcomeSocket.accept();
InputStream sin = connectSocket.getInputStream();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(sin));
PrintWriter outToClient = new PrintWriter(connectSocket.getOutputStream(), true);
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + "\r\n";
outToClient.print(capitalizedSentence);
}
}
}
import java.io.*;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws Exception {
String hostName = "localhost";
int port = 6789;
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostName, port);
PrintWriter outToServer = null;
clientSocket.getOutputStream();
BufferedReader inFromServer = null;
inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.print(sentence + "\r\n");
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " +modifiedSentence);
clientSocket.close();
}
}
After fixing your syntax errors around outToServer
, I think the problem lies in the way you're using PrintWriter
around the output stream on the client's side. 解决了outToServer
的语法错误outToServer
,我认为问题出在您在客户端的输出流上使用PrintWriter
的方式。 From the documentation : 从文档中 :
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. 与PrintStream类不同,如果启用了自动刷新,则仅在调用println,printf或format方法之一时才执行此操作,而不是在输出换行符时才执行。 These methods use the platform's own notion of line separator rather than the newline character. 这些方法使用平台自己的行分隔符概念,而不是换行符。
Since you're using print
with a manually appended new line, the message is never flushed to the socket's output stream. 由于您使用的是带有手动添加的新行的print
,因此消息永远不会刷新到套接字的输出流中。 I believe you can fix this by using println
instead: 我相信您可以通过使用println
来解决此问题:
outToServer.println(sentence);
Even better would be to use DataInputStream
and DataOutputStream
instead of BufferedReader
and PrintWriter
, as those are better suited for sending and receiving arbitrary data over a socket stream. 最好使用DataInputStream
和DataOutputStream
而不是BufferedReader
和PrintWriter
,因为它们更适合通过套接字流发送和接收任意数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.