0
votes

When I close client it gives me error on server side

Like : java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:196) at java.net.SocketInputStream.read(SocketInputStream.java:122) at java.net.SocketInputStream.read(SocketInputStream.java:210) at java.io.DataInputStream.readLine(DataInputStream.java:513) at HServer.run(HServer.java:28)

Checkout the code and please tell me how can I send one message of client to all other clients using threads please help me guys :)

ClientDemo.java

import java.io.*;
import java.net.Socket;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author zain-mughal
*/
public class ClientDemo extends javax.swing.JFrame {
DataInputStream input;
PrintWriter output;
public void Connect(){
    try {
        Socket s = new Socket("127.0.0.1", 2111);
        input=new DataInputStream(s.getInputStream());
        output=new PrintWriter(s.getOutputStream());
        boolean more_data=true;
        while(more_data){
            String in=input.readLine();
            if(in==null){
                more_data=false;
            }
            else{
                System.out.println(in);
            }
        }
        s.close();
        input.close();
        output.close();
    } catch (Exception ex) {
       ex.printStackTrace();
    }
}
public static void main(String args[]){
   ClientDemo obj=new ClientDemo();
   obj.Connect();

}

}

HServer.java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author zain-mughal
*/
public class HServer extends Thread {
Socket AcceptReQ;
public HServer(Socket reqAccept) {
    this.AcceptReQ=reqAccept;
}
@Override
public void run(){
    try {
        DataInputStream input = new DataInputStream(AcceptReQ.getInputStream());
        PrintWriter output = new PrintWriter(AcceptReQ.getOutputStream());
        String in=null;
        while((in=input.readLine())!=null){
                System.out.println(in);
        }

        AcceptReQ.close();
        input.close();
        output.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }
 public static void main(String args[]){
    try {
        ServerSocket req = new ServerSocket(2111);

        while(true){
            Socket reqAccept=req.accept();
            Thread t=new HServer(reqAccept);
            t.start();
        }
    } catch (Exception ex) {
        Logger.getLogger(HServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}
1
Your client connects to the server, and waits for it to send some data (input.readLine()). The server meanwhile accepts the connection, and waits for the client to send something (also input.readLine()). So, they are both just sitting there doing nothing, and waiting on each other. What do you expect to happen?Dima
Using one thread per connection is not going to scale well - read up on java.nio.channels.SelectorBarrySW19

1 Answers

0
votes

Try this:

Build a robust Java server

Building a basic Echo Server and A ChatServer

I used this as base for my own server. It's a little bit old and many Java features are not used but it works great.

Take some time to comprehend it.