Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: multtithreaded chat server

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile multtithreaded chat server

    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"));
    }
    }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: multtithreaded chat server

    s = new Socket("local host",10);
    Its localhost


    i am sure now it will work fine.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: multtithreaded chat server

    localhost can also be 127.0.0.1
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: multtithreaded chat server

    thanks a lot ......

  5. #5
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: multtithreaded chat server

    If your problem is solved , please mark this thread as solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. TCP chat attaching cipher is not running
    By Koren3 in forum Java Networking
    Replies: 4
    Last Post: May 19th, 2009, 02:52 AM
  2. Replies: 10
    Last Post: May 8th, 2009, 10:49 AM
  3. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  4. SSL Chat implementation
    By Koren3 in forum Java Networking
    Replies: 6
    Last Post: April 24th, 2009, 08:20 AM
  5. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM

Tags for this Thread