0
以下是错误Java程序套接字编程抛出EOFException类的OutputStream被刷新后也
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:800)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at mypackage.JChatComm.receiveMessage(JChatComm.java:83)
at mypackage.JThread.run(JThread.java:19)
at java.lang.Thread.run(Thread.java:701)
这里的完整的堆栈跟踪我的receiveMessage代码: 该函数创建一个对象JPacket和打印该对象的ObjectInputStream的。 但经过一次迭代,它抛出EOFException并打印4,11,8和stackTrace。
public boolean receiveMessage() throws IOException, ClassNotFoundException{
try{
System.out.println("4");
InputStream inFromServer = client.getInputStream();
System.out.println("11");
ObjectInputStream in = new ObjectInputStream(inFromServer);
//System.out.println("hellow owrol");
//System.out.println("5");
final JPacket jp;
jp = (JPacket) in.readObject();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("6");
if (type.equals("s")){
tab.ta.append("Client: "+jp.msg+"\n");
}
else {
cp.ta.append("Server: "+jp.msg+"\n");
}
}
});
System.out.println("other: "+jp.msg+ " Sent on:" + jp.a1);
//System.out.println("msg got");
return true;
}
catch (Exception ea){
System.out.println("8");
ea.printStackTrace();
return false;
}
}
这里是我的sendMessage计划 此方案是发送邮件的主程序。它始终工作得很好,并正确地冲洗输出流。但仍然receiveMessage抛出错误。
public boolean sendMessage() throws IOException{
try{
System.out.println("3");
final String msg;
if (type.equals("c")){
msg = cp.tf.getText();
}
else msg = tab.tf.getText();
System.out.println(msg);
JPacket j1 = new JPacket(msg);
OutputStream outToServer = client.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outToServer);
out.writeObject(j1);
System.out.println("13");
out.flush();
System.out.println("12");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (type.equals("s")){
tab.ta.append("You: "+msg+"\n");
tab.tf.setText("");
}
else {
System.out.println("Client");
cp.ta.append("You: "+msg+"\n");
cp.tf.setText("");
}
}
});
if(msg.equals("End Chat")) {
endChat();
return false;
}
else
return true;
}
catch (Exception ea){
ea.printStackTrace();
System.out.println("7");
return false;
}
}
我每次刷新对象输出流,但仍然抛出相同的错误。
'client'大概是socket?如果它是同一个套接字,那么你写给它的信息将被发送到你的服务器,而不是同一个套接字的输入流。 –
实际上这两种方法都是类的一部分,一次客户端套接字是由'new socket.connect(ip,port)'实例化的客户端套接字,在不同的运行时间中,它通过接受由serversocket实现的连接来实例化。 – Dheerendra