简体   繁体   中英

socket write error using threads

I have the following classes

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



}

}

the code executes perfectly once,after that i get the following error

In Client Console

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

The server starts a thread on the socket which reads a message from the socket input stream, then writes it to the consoler, then stops running. The socket and its stream thus go out of scope and are probably garbage-collected, causing the socket to be closed. And since the socket is closed, the client can't write anything anymore.

If you expect the client to be able to send an infinity of messages, then the server should loop and read an infinity of messages.

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



}

The execution halts after a single line is read from client. Hence server code exits.Thus you are getting the exception. So what you can do is:

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



    }

Here, we have enclosed the code inside the run method in a while loop which keeps the server running for ever. You can put your own logic.

Hope that helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM