在写一个聊天软件,先运行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

解决方案 »

  1.   

    Connection reset 
    连接已经断开。
    看看Socket是否关闭
      

  2.   

    有沒有寫個死循環執行recieve()方法來接收客戶端發起的連接?
      

  3.   

    socket关闭了自然就断开啦只要有客户端断开就会报这个
      

  4.   

    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();
    }

    }
       
       }}
      

  5.   

    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();
    }

    }
    }

    }

    }