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 3 of 3

Thread: thread

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thread

    how to code for below home work question
    Multiple Clients Connect to a server and communicate among the themselves. When a new Client is connected to the server, all other clients get the notification and they can chat with each other. Similarly when a client quits all other clients get the notification and end the corresponding client.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: thread

    1. Open an IDE
    2. Start typing

    What exactly are you expecting people to post? This is not simple code and apparently it is your job to write it not ours. If you have written code then post it here and ask a specific question.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread

    i wrote the following code..but i am not able to create multi client code
    import java.io.*;
    import java.net.*;
    class ThreadClient
    {
    public static void main(String args[]) throws IOException
    {
    Socket soc=new Socket("127.0.0.1",1445);
    System.out.println("Client connected to the server");
    //thread creation
    SendThread sendThread=new SendThread(soc);//creating object
    Thread thread=new Thread(sendThread);//creating thread object
    thread.start();//starting thread
    RecieveThread recieveThread=new RecieveThread(soc);//creating object
    Thread thread1=new Thread(recieveThread);//thread object creation
    thread1.start();//starting of thread1
    }
    }
    class RecieveThread implements Runnable
    {
    Socket soc=null;
    BufferedReader recieve=null;
    public RecieveThread(Socket soc)//constructor
    {
    this.soc=soc;
    }
    public void run()
    {
    try
    {
    recieve=new BufferedReader(new InputStreamReader(this.soc.getInputStream()));//meessage from client
    String msgRecieved=null;
    while((msgRecieved=recieve.readLine())!=null)
    {
    System.out.println("From Server: "+msgRecieved);//message recieved from client
    }
    }
    //catch Exception
    catch(IOException ex)
    {
    System.out.println(ex.getMessage());
    }
    }
    }
    class SendThread implements Runnable
    {
    Socket soc=null;
    PrintWriter print=null;
    BufferedReader input=null;
    //SendThread Constructor
    public SendThread(Socket soc)
    {
    this.soc=soc;
    }
    public void run()
    {
    try
    {
    if(soc.isConnected())
    {
    System.out.println("client connected to server "+soc.getInetAddress());
    this.print=new PrintWriter(soc.getOutputStream(),true);
    System.out.println("Start the chat");
    while(true)
    {
    System.out.println("press EXIT to close");
    input=new BufferedReader(new InputStreamReader(System.in));//send to server
    String msgtoServerString=null;
    msgtoServerString=input.readLine();//send message to server
    this.print.println(msgtoServerString);
    this.print.flush();//flush the buffer
    if(msgtoServerString.equals("EXIT"))
    break;//close socket if
    }//end while
    soc.close();//close the socket
    }
    }
    //catch exception
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }//end run method
    }//end class

Similar Threads

  1. Thread
    By Yogeshwari in forum Threads
    Replies: 4
    Last Post: February 7th, 2014, 05:34 AM
  2. Creating a Thread from a Thread
    By angstrem in forum Threads
    Replies: 11
    Last Post: May 29th, 2013, 09:31 AM
  3. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  4. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  5. What is this thread for exactly?
    By javapenguin in forum Computer Support
    Replies: 2
    Last Post: July 2nd, 2011, 10:37 AM

Tags for this Thread