Socket问题 Connection reset?

hym3009 2010-06-05 09:19:49
顶起。我也有差不多的问题的。不过我的从服务器接收客户端的消息接受一切正常,当我关闭客户端是。程序就报异常。说Connection reset ?为什么啊?希望高手给我看看。下面我代码:
public class ServerConClientThread extends Thread {
Socket s;

public ServerConClientThread(Socket s) {
this.s = s;

}

public void run()
{
// TODO Auto-generated method stub
while (true)
{
try {
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Message m = (Message) ois.readObject();
// 打印服务器受到的一方客户端的消息
System.out.println(m.getSender() + "给" + m.getGetter() + "说"+ m.getContent());
} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
}

}

}

}
报异常:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:113)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at com.qq.server.model.ServerConClientThread.run(ServerConClientThread.java:26)


26行就是 ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Java技术栈 2010-06-05
  • 打赏
  • 举报
回复
帮顶。。
hym3009 2010-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iihero 的回复:]
引用楼主 hym3009 的回复:
顶起。我也有差不多的问题的。不过我的从服务器接收客户端的消息接受一切正常,当我关闭客户端是。程序就报异常。说Connection reset ?为什么啊?希望高手给我看看。下面我代码:
public class ServerConClientThread extends Thread {
Socket s;

public ServerConClien……
[/Quote]

不行啊。。我之前也试过。。而且这样做提前报异常了。。我就是做个QQ的。。加上finally 现在一关闭聊天窗口。就报异常。我之前关闭聊天窗口还是正常的,直到关闭好友列表才报异常。。我是在登陆成功。就生成一个线程传入一个Socket对象。。。。。
iihero 2010-06-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 hym3009 的回复:]
顶起。我也有差不多的问题的。不过我的从服务器接收客户端的消息接受一切正常,当我关闭客户端是。程序就报异常。说Connection reset ?为什么啊?希望高手给我看看。下面我代码:
public class ServerConClientThread extends Thread {
Socket s;

public ServerConClientThread(Socket s) {……
[/Quote]
这个是正常的。
你得加一个finally:

主题逻辑像这样子:
try {

ServerSocket server = new ServerSocket(5776);

while (true) {

Socket connection = server.accept( );

try {

Writer out

= new OutputStreamWriter(connection.getOutputStream( ));

out.write("You've connected to this server. Bye-bye now.\r\n");

out.flush( );

connection.close( );

}

catch (IOException ex) {

// This tends to be a transitory error for this one connection;

// e.g. the client broke the connection early. Consequently,

// you don't want to break the loop or print an error message.

// However, you might choose to log this exception in an error log.

}

finally {

// Guarantee that sockets are closed when complete.

try {

if (connection != null) connection.close( );

}

catch (IOException ex) {}

}

}

catch (IOException ex) {

System.err.println(ex);

}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧