套接字使用线程写错误

时间:2013-07-20 10:10:55

标签: java sockets

我有以下课程

ClientDemo.java

ClientTread.java

ServerDemo.java

ServerThread.java

 public class ClientDemo {
   public static void main(String[] args) throws InterruptedException {
     try {
        Socket client=new Socket("localhost", 6666);
        while(true)
        {
            System.out.println("Hello");
            Thread th=new Thread(new ClientThread(client));
            th.start();
            System.out.println("Thread started........");
            th.sleep(1000*30);

        }

     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
    }

   }

ClientThread.java

 public class ClientThread implements Runnable {
   Socket c;
    public ClientThread(Socket client) {
    this.c=client;
}

@Override
public void run() {
    DataOutputStream dos=null;
    try {
        System.out.println("Client thread is going to write.......");
        dos = new DataOutputStream(c.getOutputStream());
        dos.writeUTF("Hello From Client");
        System.out.println("Data written by client............");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        System.out.println(e+"");
    }


      }

    }

SeverDemo.java

      public class ServerDemo {
        public static void main(String[] args) {
      try {

    ServerSocket serversocket=new ServerSocket(6666);
    System.out.println("server listening..........");
    Thread ts=new Thread( new ServerThread(serversocket.accept()));
    ts.start();
    System.out.println("server thread started.........");
            } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        }
     }
    }

ServerThread.java

 public class ServerThread implements Runnable {
Socket s;

public ServerThread(Socket server) {
    this.s=server;
}

@Override
public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

代码执行完美一次,之后我得到以下错误

在客户端控制台

Hello

Thread started........

Client thread is going to write.......

Data written by client............

Hello

Thread started........

Client thread is going to write.......

java.net.SocketException: Connection reset by peer: socket write error

2 个答案:

答案 0 :(得分:0)

服务器在套接字上启动一个线程,该线程从套接字输入流中读取消息,然后将其写入控制器,然后停止运行。套接字及其流因此超出范围,可能是垃圾收集,导致套接字关闭。由于套接字已关闭,客户端无法再写任何内容了。

如果您希望客户端能够发送无限的消息,那么服务器应该循环并读取无限的消息。

答案 1 :(得分:0)

在ServerThread中,

public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

从客户端读取一行后,执行暂停。因此服务器代码退出。因此你得到了例外。 所以你能做的是:

public void run() {
        DataInputStream dis;
        try {
             while(true)
             {
                dis = new DataInputStream(s.getInputStream());
                String message=dis.readUTF();
                System.out.println(message);
             }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

在这里,我们将run方法中的代码包含在while循环中,以使服务器永远运行。你可以把自己的逻辑。

希望有所帮助!!