高手帮忙看一下:SocketException: Connection reset

langqianqian 2009-08-21 10:40:14
在写一个聊天软件,先运行Server端以后,再运行Client端的时候出现
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:307)
at java.io.DataInputStream.readUTF(DataInputStream.java:545)
at java.io.DataInputStream.readUTF(DataInputStream.java:522)
at ChatClient$RecvThread.run(ChatClient.java:87)
at java.lang.Thread.run(Thread.java:595)
ChatClient.java
ChatServer.java
...全文
165 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
langqianqian 2009-08-24
  • 打赏
  • 举报
回复
Server端
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List clients = java.util.Collections.emptyList();
//List<Client> clients = new ArrayList()

public static void main(String[] args) {

new ChatServer().start();

}

public void start(){
try {
ss = new ServerSocket(8888);
}catch(BindException e){
System.out.println("端口使用中。。。");
System.out.println("请关掉相关程序并重新运行服务器");
System.exit(0);
}catch(IOException e){
e.printStackTrace();
}
try{
started = true;
while(started){
boolean connect = false;
Socket s = ss.accept();
Client c = new Client( s);
clients.add(c);
new Thread(c).start();

}
}catch (IOException e) {
e.printStackTrace();
}finally{
try{
ss.close();
}catch(Exception e){

e.printStackTrace();
}

}
}

class Client implements Runnable{
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean connect = false;
public Client(Socket s){
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
connect = true;
} catch (IOException e) {

e.printStackTrace();
}

}

public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
try{
while (connect){

String str = dis.readUTF();
System.out.println(str);
for(int i = 0;i < clients.size();i++){
Client c = (Client) clients.get(i);
c.send(str);
}
}

}catch(EOFException e){
System.out.println("Client closed!");
}
catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(dis != null){dis.close();}
if(dos != null){dos.close();}
if(s != null){s.close();}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}

}

}
langqianqian 2009-08-24
  • 打赏
  • 举报
回复
Client端
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame{

TextField tf = new TextField();
TextArea ta = new TextArea();
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;

public static void main(String[] args) {
new ChatClient().lauchFrame();
}
public void lauchFrame(){
this.setLocation(400,300);
this.setSize(300, 300);
this.add(tf,BorderLayout.SOUTH);
this.add(ta,BorderLayout.NORTH);
this.pack();
tf.addActionListener(new TFListener());
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}

}

);
connect();
new Thread(new RecvThread()).start();

}
public void connect(){
try {
s = new Socket("172.21.204.212",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
s.close();
} catch (IOException e) {

e.printStackTrace();
}

}
private class TFListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
ta.setText(str);
tf.setText("");
try {

dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}

private class RecvThread implements Runnable{

public void run() {
try{
while(bConnected){
String str = dis.readUTF();
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}

}

}

}
langqianqian 2009-08-24
  • 打赏
  • 举报
回复
传代码怎么传啊
嘿嘿不好意思,我第一次发帖子
feishare 2009-08-21
  • 打赏
  • 举报
回复
楼主,代码啊~~
dz007 2009-08-21
  • 打赏
  • 举报
回复
socket关闭了自然就断开啦

只要有客户端断开就会报这个
dbsshida 2009-08-21
  • 打赏
  • 举报
回复
有沒有寫個死循環執行recieve()方法來接收客戶端發起的連接?
langqianqian 2009-08-21
  • 打赏
  • 举报
回复
SOCKET关闭注了以后还是这样
langqianqian 2009-08-21
  • 打赏
  • 举报
回复
SOCKET关闭注了以后还是这样
lcj_up 2009-08-21
  • 打赏
  • 举报
回复
Connection reset
连接已经断开。
看看Socket是否关闭

62,614

社区成员

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

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