Results 1 to 3 of 3
  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default identifying a socket exception

    I am writing a simple little chat server and client. The problem I am having is when someone unexpectedly disconnects. The way the server works is it spawns a new thread for each client. Each thread has access to all the writers, and one listener, this way they can listen to one socket, and broadcast to all sockets. The problem comes when someone disconnects, because inside the listener code, there is no way to determine what kind of exception it is. in both cases its a SocketException. I tried adding code to check if the socket is still connected, but once the connection is broken that seems to throw a SocketException. At this point I kill the thread, however, once the socket brakes, its io streams brake too, so when a different listener thread, one with a good socket, tries to write to a broken stream, it generates another SocketException, and then kills that thread as well, even though the socket it listens on is good, so it doesnt recieve anymore input. I posted the code for the server. the client is trivial, it only reads and writes.

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class ChatServer {
    	private ArrayList<Socket> sockets;
    	private ArrayList<Writer> writers;
    	private ArrayList<Reader> readers;
    	private Socket socket;
    	private ServerSocket server;
    	private BufferedReader reader;
    	private Writer writer;
    	private ListenerThread listener;
    	private final int PORT = 1235;
    	
    	public ChatServer() {
    		sockets = new ArrayList<Socket>();
    		writers = new ArrayList<Writer>();
    		readers = new ArrayList<Reader>();
    		try {
    			server = new ServerSocket(PORT);
    		}
    		catch(IOException e) {
    			System.out.println(e);
    			System.exit(0);
    		}
    	}
    	
    	public void listen() {
    		while(true) {
    			try {
    				socket = server.accept();
    				sockets.add(socket);
    				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				writer = new OutputStreamWriter(socket.getOutputStream());
    				readers.add(reader);
    				writers.add(writer);
    				listener = new ListenerThread(reader, writers, socket);
    				listener.start();
    				
    			}
    			catch(IOException e) {
    				System.out.println("ChatServer: "+e);
    				try {
    					for(Reader r : readers)
    						r.close();
    					for(Writer w : writers)
    						w.close();
    					for(Socket s : sockets)
    						s.close();
    				}
    				catch(IOException ex) {
    					System.out.println("ChatServer2): "+ex);
    				}
    			}
    			
    		}
    	}
    
    	public static void main(String[] args) {
    		ChatServer server = new ChatServer();
    		server.listen();
    	}
    	
    }
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class ListenerThread extends Thread {
    	private BufferedReader reader;
    	private ArrayList<Writer> writers;
    	private Socket socket;
    	private boolean finish;
    	
    	public ListenerThread(BufferedReader reader, ArrayList<Writer> writers, Socket socket) {
    		this.reader = reader;
    		this.writers = writers;
    		this.socket = socket;
    		finish = false;
    	}
    	
    	public void run() {
    		while(!finish) {
    			try {
    				if(!socket.isConnected()) {
    					finish = true;
    					continue;
    				}
    		
    				String input = reader.readLine();
    				
    				if(input != null) {
    					for(Writer writer : writers) {
    						writer.write(input+"\r\n");
    						writer.flush();
    					}
    				}
    			}
    			catch(IOException e) {
    				System.out.println("ListenerThread: "+e);
    				
    				if(e instanceof SocketException)
    					finish = true;
    			}
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: identifying a socket exception

    when a different listener thread, one with a good socket, tries to write to a broken stream, it generates another SocketException
    Can you keep a track of the status of the connections: good or bad. Set bad when you detect the exception.
    Test the status of the connection before trying to use it.

  3. #3
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: identifying a socket exception

    ok i recoded it to set a connection to bad when it throws an exception and its working now. thanks!

Similar Threads

  1. How to handle socket Exception
    By mayank0512 in forum Networking
    Replies: 14
    Last Post: 12-21-2010, 11:31 PM
  2. Very strange exception in a socket comunication
    By sangio85 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-05-2010, 10:48 PM
  3. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 10:03 PM
  4. unreported exception (socket connection)
    By Symbiot in forum New To Java
    Replies: 7
    Last Post: 05-28-2010, 11:13 AM
  5. help with identifying components of a gui
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •