Hi,
I'm wondering can anyone help with a problem I'm having. Basically I'm making a Chat applet that allows clients to connect to a server and exchange messages. I have the messages being exchanged fine but whenever a client closes the connection I keep getting a
java.net.SocketException: Connection reset Error
The program is meant to keep track of the number of clients connected and update the the command line every time a client joins or leaves the Server.
My code is attached below. Any help would be greatly appreciated!
Thanks!
import java.net.*; import java.io.*; import java.util.*; public class ChatServerThread extends Thread { private Socket socket = null; private String name; private String inputLine; private ArrayList <ChatServerThread> clientList; private PrintWriter out; private BufferedReader in; //Constructor takes a socket and Arraylist of connected clients //as parameters public ChatServerThread(Socket socket,ArrayList <ChatServerThread> clientList) { super("ChatServerThread"); this.socket = socket; this.clientList = clientList; } //Broadcast the message to all connected clients //Not synchronized because I synchronized blocks where it is used public void sendToAll(String message){ for(ChatServerThread c: clientList){ c.out.println(message); } } public void run(){ try { //Create input/output streams out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //Read in client name name = in.readLine(); //let everyone know user has joined synchronized(this){ sendToAll( name + " has joined the chatroom ....\n"); } //If user wants to leave break and close thread while ((inputLine = in.readLine()) != null) { //broadcast message to all synchronized(this){ sendToAll(name+" says: "+ inputLine); } } sendToAll( name + " has left the chatroom ....\n"); //Tidy up synchronized(this){ clientList.remove(currentThread()); } System.out.println("Clients :" +clientList.size() + "\n"); out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } //-------------------------------------------------------------------------------------//import java.net.*; import java.io.*; import java.util.*; public class ChatServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; Socket clientSocket = null; //Arraylist to hold all connected clients ArrayList <ChatServerThread> clientList = new ArrayList<>(); final int PORT = 7777; boolean listening = true; try { //Set up server socket serverSocket = new ServerSocket(PORT); System.out.println("Server now listening on port: " + PORT); } catch (IOException e) { System.err.println("Could not listen on port:" + PORT); System.exit(-1); } //Loop to listen for and connect new clients, while (listening){ try{ clientSocket = serverSocket.accept(); ChatServerThread client = new ChatServerThread (clientSocket,clientList); //add client to clientList and start the thread clientList.add(client); System.out.println("Clients :" + clientList.size() + "\n"); client.start(); } catch (IOException e) {} } serverSocket.close(); } } //-------------------------------------------------------------------------------------//