i am not getting desired output ..still there are not errors !!!!
//class MyServer import java.io.*; import java.net.*; import java.util.*; public class MyServer { ArrayList al = new ArrayList(); ServerSocket ss; Socket s; public MyServer() { try{ ss = new ServerSocket(10); while(true) { s = ss.accept(); al.add(s); Runnable r = new MyThread(s,al); Thread t = new Thread(r); t.start(); } } catch(Exception e) { } } public static void main(String...s) { new MyServer(); } }
class MyThread implements Runnable { Socket s ; ArrayList al; MyThread(Socket s,ArrayList al) { this.s = s; this.al = al; } public void run() { String s1; try { DataInputStream din = new DataInputStream(s.getInputStream()); do{ s1 = din.readUTF(); System.out.println(s1); if(!s1.equals("stop")) tellEveryone(s1); else { DataOutputStream dout = new DataOutputStream(s.getOutputStream()); dout.writeUTF(s1); dout.flush(); } } while(!s1.equals("stop")); } catch(Exception e) {} } public void tellEveryone(String s1) { Iterator i = al.iterator(); while(i.hasNext()) { try {Socket sc = (Socket)i.next(); DataOutputStream dout = new DataOutputStream(sc.getOutputStream()); dout.writeUTF(s1); dout.flush(); System.out.println("client"); }catch(Exception e) {} } } }
//MyClient import java.io.*; import java.net.*; import java.util.*; public class MyClient { Socket s; DataInputStream din; DataOutputStream dout; public MyClient() { try { s = new Socket("local host",10); din = new DataInputStream(s.getInputStream()); dout = new DataOutputStream(s.getOutputStream()); clientChat();} catch (Exception e) {} } public void clientChat() throws IOException { My m = new My(din); Thread t1 = new Thread(m); t1.start(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s1; do{ s1 = br.readLine(); dout.writeUTF(s1); dout.flush(); } while(!s1.equals("stop")); } public static void main(String...s) { new MyClient(); } } class My implements Runnable { DataInputStream din; My(DataInputStream din) { this.din = din; } public void run() { String s2 = " "; do { try { s2 = din.readUTF(); System.out.println(s2); } catch(Exception e) {} } while(!s2.equals("stop")); } }