我写的是一个聊天室程序,但是现在老师出现connection reset这个错误,麻烦哪位高人指点一下。
客户端程序:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class ChatClient extends Frame
{
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bconnected=false;

TextField tfText=new TextField();
TextArea taContent=new TextArea();

public static void main(String[] args)
{
new ChatClient().lanuchFrame();
}
public void lanuchFrame()
{
this.setLocation(400,300);
this.setSize(300,300);
this.add(taContent,BorderLayout.NORTH);
this.add(tfText,BorderLayout.SOUTH);
this.pack();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
disconnect();
System.exit(0);
}
});
tfText.addActionListener(new TFListener());
this.setVisible(true);
connect();
new Thread(new RecvThread()).start();
}
public void connect()
{
try
{
s=new Socket("127.0.0.1",8888);
System.out.println("connected!");
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
bconnected=true;
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void disconnect()
{
try
{
dos.close();
dis.close();
s.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

private class TFListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
String str=tfText.getText().trim();
tfText.setText("");
taContent.setText(str);
try
{
dos.writeUTF(str);
dos.flush();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}

public class RecvThread implements Runnable
{
public void run()
{
try
{
while(bconnected)
{
String str=dis.readUTF();
System.out.println(str);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}

}}
服务器端程序:
mport java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer
{
ServerSocket ss=null;
boolean started=false;

List<Client> clients=new ArrayList<Client>();

public static void main(String[] args)
{
new ChatServer().start();
}
public void start()
{
try
{
ss=new ServerSocket(8888);
started=true;
}
catch(BindException e)
{
System.out.println("端口使用中....");
System.out.println("请关掉相关程序,重新运行服务器!");
System.exit(0);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
while(started)
{
Socket s=ss.accept();
System.out.println("a Client connected!");
Client c=new Client(s);
new Thread(c).start();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
ss.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

class Client implements Runnable
{
private Socket s;
private DataInputStream dis=null;
private DataOutputStream dos=null;

private boolean bconnected=false;

public Client(Socket s)
{
try
{
this.s=s;
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
bconnected=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(bconnected)
{
String str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++)
{
Client c=clients.get(i);
c.send(str);
}
/*
//不建议使用这两种方法,因为他们会进行内部锁定,效率低。在这里没必要。
for(Iterator<Client> it=clients.iterator();it.hasNext();)
{
Client c=it.next();
c.send(str);
}

Iterator<Client> it=clients.iterator();
while(it.hasNext())
{
Client c=it.next();
c.send(str);
}
*/
}
}
catch(EOFException e)
{
System.out.println("Client Closed!");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(dis!=null)
{
dis.close();
dis=null;
}
if(dos!=null)
{
dos.close();
dos=null;
}
if(s!=null)
{
s.close();
s=null;
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
}
错误提示:
a Client connected!
a Client connected!
nihao
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:320)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at ChatServer$Client.run(ChatServer.java:102)
at java.lang.Thread.run(Thread.java:662)
不胜感激!