I am trying to write a chat application using java sockets... The Concept is Server listens on a port for clients when client comes in as the first step the server will store as username and client's link socket as sockettuple(An object which has username as string and linksocket as socket) .. then after storing it sends the client as welcome you are connected ..Server listens in listenthread and communications happen in separate thread.. for client creation of socket happens in main thread and writing to server happens in separate thread In this the storing of socket tuple happens fine but on sending the message from server to client Socketclosed exception happens on both server write and client's read threads......
// mainserver.java import java.net.*; import java.io.*; class listenthread extends Thread{ ServerSocket mserver; Socket link,targetsocket; String user,targetuser; sockettuple targettuple,starray[]= new sockettuple[10]; int i=0; public listenthread(ServerSocket mserver){ try{ System.out.println("Inside Listenthread constructor"); this.mserver=mserver; } catch (Exception e){ System.out.println("Exception inside listenthread constructor"+e); } } public void run(){ try{ while(true){ System.out.println("Listening....."); link=mserver.accept(); System.out.println("Accepted"); BufferedReader intosockb = new BufferedReader(new InputStreamReader(link.getInputStream())); user= intosockb.readLine(); System.out.println(user + "Accepted"); starray[i] =new sockettuple(link,user); i++; System.out.println(starray[i-1].username); new servwrite(link).start(); intosockb.close(); } } catch(Exception e){ System.out.println("Exception inside listenthread run "+e); e.printStackTrace(); } } } class servwrite extends Thread{ Socket link; public servwrite(Socket link){ this.link=link; } public void run(){ try{ PrintStream fromserv=new PrintStream(link.getOutputStream()); fromserv.print("You are connected...Enter the target user:"); } catch(Exception e){ System.out.println("Exception inside run of serv write thread..."+e); } } } public class mainserver { public static void main(String args[]){ try{ ServerSocket Servermain= new ServerSocket(4579); System.out.println(" Server main Socket created"); new listenthread(Servermain).start(); } catch(Exception e){ System.out.println("Exception inside main "+e); } } } //Clientmenu.java import java.net.*; import java.io.*; class clientread extends Thread{ Socket client; BufferedReader key=null; public clientread(Socket client){ try{ this.client=client; key= new BufferedReader(new InputStreamReader(client.getInputStream())); } catch(Exception e){ System.out.println("Exception inside client read thread's constructor"); e.printStackTrace(); } } public void run(){ try{ System.out.println("First line inside run of client read thread before cmsg readline"); String cmsg= key.readLine(); System.out.println(cmsg); } catch(Exception e){ System.out.println("Exception inside run of client read thread..."+e); e.printStackTrace(); } } } public class Clientmenu { public static void main(String args[]){ String user=" "; String targetuser=""; Socket newclient; BufferedReader fromtheserv; try{ newclient = new Socket("127.0.0.1",4579); System.out.print("Enter username:"); BufferedReader key= new BufferedReader(new InputStreamReader(System.in)); user=key.readLine(); PrintStream fromclientp=new PrintStream(newclient.getOutputStream()); fromclientp.print(user); new clientread(newclient).start(); fromclientp.close(); } catch(Exception e){ System.out.println("Exception inside CLientmenu "+e); } } }