[英]Socket readLine() doesn't receive line sent
I'm building a client-server project. 我正在建立一个客户服务器项目。
What I need is that the client sends a string, such as "Pendu", and the server receives this string and send an object named "Pendu" back to the client. 我需要的是客户端发送一个字符串,例如“ Pendu”,服务器接收该字符串,然后将一个名为“ Pendu”的对象发送回客户端。
Here is my code: 这是我的代码:
// server
ServerSocket serverSocket = new ServerSocket(6789);
System.out.println("accepting...");
Socket socket = serverSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ObjectOutputStream outToClient = new ObjectOutputStream(socket.getOutputStream());
String clientMsg = inFromClient.readLine();
System.out.println("Received from client: " + clientMsg);
Object obj;
System.out.println("building object...");
obj = Class.forName("Data." + clientMsg).newInstance();
System.out.println("object built");
if(obj instanceof Pendu)
{
System.out.println("Send to client: " + obj);
outToClient.writeObject(new Pendu());
}
//client
Socket socket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
System.out.println("sending...");
outToServer.writeBytes("Pendu");
System.out.println("sent");
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
System.out.println("receiving...");
Object obj = ois.readObject(); // it is blocked here and I don't know why
System.out.println("received");
The class Pendu
is defined in the package Data
: Pendu
类在包Data
定义:
package Data;
public class Pendu implements Serializable
{
private static final long serialVersionUID = 1L;
private static String[] words = new String[]{"bonjour", "bonsoir", "hier", "france", "ordinateur"};
private static Random rand = new Random();
private String but;
public Pendu()
{
this.but = words[rand.nextInt(words.length)];
}
public int getLenth()
{
return this.but.length();
}
public String getBut()
{
return this.but;
}
@Override
public String toString()
{
return "I'm a pendu object";
}
}
My problem is: 我的问题是:
First I execute the server and I can see that accepting...
is shown in the console. 首先,我执行服务器,然后可以看到控制台中显示了正在
accepting...
Then I execute the client, in the console I get messages as below: 然后执行客户端,在控制台中,我收到如下消息:
sending...
sent
receiving...
At the same time, nothing new is shown at the server side. 同时,服务器端未显示任何新内容。
Now I stop the client and all other messages of the server are shown: 现在,我停止客户端,并显示服务器的所有其他消息:
Received from client: Pendu
building object...
object built
Of course I get also the error: 当然我也得到错误:
java.net.SocketException: Broken pipe
java.net.SocketException:管道损坏
This is logical because I kill the client which hasn't finish receiving. 这是合乎逻辑的,因为我杀死了尚未完成接收的客户端。
I don't know why the client can't receive as expected. 我不知道为什么客户不能收到预期的结果。
You are reading lines but not writing lines. 您正在阅读的行,而不是在写行。 You need to add a line terminator to the
"Pendu"
message. 您需要在
"Pendu"
消息中添加行终止符。
NB When you send a Pendu
, why are you sending a new one instead of the one you just created? 注意:发送
Pendu
,为什么要发送新的Pendu
而不是刚创建的Pendu
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.