【发布时间】:2014-12-24 07:13:37
【问题描述】:
我正在制作一对客户端服务器。我的客户端很好地连接到服务器,它通过套接字创建 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;
}
【问题讨论】:
-
正如我前几天向某人解释的那样,对象和数据输入/输出流是过去时代的技术,大端与小端之类的问题仍然存在,而且没有良好的沟通标准。我们现在有了 XML 和 JSON - 它们都是比使用对象流更好的解决方案
-
我认为这实际上不是我问题的答案?
-
问题似乎消失得像它看起来一样奇怪!
-
@ControlAltDel 你不能到处声称 XML 和 JSON 已经过时了二进制协议。他们没有,也不能。
-
@ControlAltDel - 事实上,(修辞)反驳论点是像 Protobuf 这样的二进制协议已经过时了 XML 和 JSON。现实情况是,基于二进制和文本的协议都有一席之地,而且它们都不会过时。
标签: java sockets object stream server