[英]Java Client/Server don't communicate through Socket/ServerSocket
I have a simple socket
/ serversocket
example that I'm trying to get running, but both the client and the server hang when their BufferedReader
s try to read. 我有一个简单的
socket
/ serversocket
示例,尝试运行,但是当BufferedReader
尝试读取时,客户端和服务器都挂起。 Here is the code for each: 这是每个代码:
SERVER 服务器
package picturePerfect;
--imports--
public class PictureServer implements Runnable{
static ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(2342);
Thread firstSessionThread = new Thread(new PictureServer());
firstSessionThread.start();
}
@Override
public void run() {
Socket socket = null;
try {
socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String clientRequest = bufferedReader.readLine();
System.out.println(clientRequest);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println("Sent from server!");
} catch (IOException e) {
e.printStackTrace();
}
}
} }
CLIENT 客户
package picturePerfect;
--imports--
public class PictureClient {
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
Socket socket = new Socket("localhost", 2342);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.write("Sent from client!");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = bufferedReader.readLine();
System.out.println(response);
socket.close();
}
}
This is the barest I could simplify my code to. 这是我可以简化我的代码所能做到的。 I have a sample program that I've been following, which seems nearly exactly the same.
我有一个我一直关注的示例程序,看起来几乎完全一样。 This is the sample server and client (that does work):
这是样板服务器和客户端(即不工作):
SAMPLE SERVER 样本服务器
--imports--
public class Server implements Runnable{
static ServerSocket ss;
public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}
@Override
public void run() {
while ( true ) {
Socket s = null;
try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}
SAMPLE CLIENT 样本客户
--imports--
public class Server implements Runnable{
static ServerSocket ss;
public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}
@Override
public void run() {
while ( true ) {
Socket s = null;
try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}
I've tried putting the while loop into my server and moving my client and server to the default package, but neither helped. 我尝试将while循环放入服务器,并将客户端和服务器移至默认程序包,但均无济于事。 I also tried using
read()
instead of readLine()
, and ending the printWriter's lines with \\r\\n
, but was just as unsuccessful there. 我还尝试使用
read()
而不是readLine()
,并用\\r\\n
结束printWriter的行,但在那里同样失败。
Why does my code hang on readLine()
, especially when the sample code doesn't? 为什么我的代码挂在
readLine()
,尤其是在示例代码没有挂起的时候?
What readline()
does is wait until it sees a new line character until it returns, hence readLine(). readline()
作用是等到看到新的换行符后再返回,因此是readLine()。
In your client, you do not write a new line. 在您的客户端中,您无需写新行。 You use:
你用:
printWriter.write("Sent from client!");
Instead, write a newline character into the stream using println, 而是使用println将换行符写入流中,
printWriter.println("Sent from client!");
Server expects to read line, so you need to add line separators after your massage. 服务器希望读取行,因此您需要在按摩后添加行分隔符。 To do this instead of
为此,而不是
printWriter.write("Sent from client!");
in client use 供客户使用
printWriter.println("Sent from client!");
// ^^^^^^^
or 要么
printWriter.write("Sent from client!"+System.lineSeparator());
printWriter.flush();
You will need to flush
yourself because autoflush in PrintWriter works only for println
, printf
, or format
, not for write
method 您将需要
flush
自己,因为PrintWriter中的自动flush
仅适用于println
, printf
或format
,不适用于write
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.