我正在制作一个客户端服务器。我的客户端很好地连接到服务器,它在套接字上创建ObjectInputStream(socket.getInputStream()),从服务器到客户端,反之亦然。然后出于一些神秘的原因,我的服务器的ObjectInputStream以某种方式捕获一个空对象。客户端没有通过套接字发送任何东西(我确实把/../放在了对象发送方法上,以确保这一点,甚至System.out.printed所有先前发送的对象)服务器捕获了神秘对象只有一次,之后客户端发送的所有对象都按照它们的方式工作..
class ClientThread extends Thread {
//The socket where to listen/talk
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
InputStream fInput;
OutputStream Output;
//my unique id (easier for deconnection)
int id;
//Objects that we will be receiving
Incomingdata datain;
//the date we connect
String date;
Player player;
boolean Connected = false;
//Constructor
ClientThread(Socket socket){
id = uniqueId++;
this.socket = socket;
try{
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
Output = socket.getOutputStream();
} catch (Exception e){
System.out.println("Couldn't create Input/Output streams");
}
date = new Date().toString();
}
// what will run forever
public void run() {
// to loop until LOGOUT
Connected = true;
while(Connected) {
try {
datain = (Incomingdata) sInput.readObject(); //<--- this catches the mystical null! Even if nothing is sent over the socket?
}
catch (IOException e) {
TextArea.AddLine("Exception reading Streams: " + e);
break;
}
catch(ClassNotFoundException e2) {
break;
}
答案 0 :(得分:0)
如果发送空值,则只能收到空值。
(人们普遍存在一种误解,即readObject()
在流的末尾返回null。它没有。)