11 Replies - 1257 Views - Last Post: 29 November 2012 - 04:21 AM Rate Topic: -----

#1 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Multithreaded Chatroom Server

Posted 28 November 2012 - 12:06 PM

Hey there, i am stuck on trying to implement a multithreaded chat room server. the program should use 1 main thread and then when a chatter(client) connects it will create a new thread instance, a chatter should be able to broadcast a message and it will send to every chatter in the linked list.
i am having trouble trying to add a chatter to list and then send a message to everyone in the list. in other words i am having trouble getting the chatter to point to the next chatter. not very good at programming so sorry for all the wrong indentation and comments thanks.


import java.io.*;
import java.net.*;


public class ChatroomServer
{
   // port number for the server
   private static final int PORT_NUM = 8189;

   // the server socket:
   private static ServerSocket ss;

   // set when shutDown() is called to stop the server:
   private static boolean shutDownCalled = false;
   private static Chatter c;

   // shut the server down by closing the server socket
   public static void shutDown()
   {
      // flag that the server socket has been closed
      shutDownCalled = true;

      try 
      {
         ss.close();
      }
      catch (Exception e)
      {
         // something went wrong; give data:
         System.err.println("problem shutting down:");
         System.err.println(e.getMessage());
         // and trust the JVM to clean up:
         System.exit(1);
      }
   } 
   
   
   public static void main(String[] args ) 
   {
    // for client connections
      Socket incoming;
      ChatterList cl = new ChatterList();
      
      // session-handling thread
      Thread t;
      //Chatter c = null;

      try 
      {
         // set up server socket
         ss = new ServerSocket(PORT_NUM);

         while (true)
         {
            incoming = ss.accept();

            // start session-handler in new thread
            
            //ss  = new Chatter();
             //t = new Thread(c, "dan");  
            
            t = new Thread(new Chatter(incoming, cl, c));
            t.start();
            
      }}
 
      catch (SocketException se)
      {
         /*
          * will be thrown when accept() is called after closing
          * the server socket, in method shutDown().
          * If shutDownCalled, then simply exit; otherwise,
          * something else has happened:
          */
         if (! shutDownCalled)
         {
            System.err.println("socket problem:");
            System.err.println(se.getMessage());
            // trust the JVM to clean up
            System.exit(1);
         }
      }
      catch (IOException ioe)
      {
         System.err.println("I/O error:");
         System.err.println(ioe.getMessage());
         System.exit(1);
      }
      finally
      {
         if (ss != null)
         {
            try 
            {
               ss.close();
            }
            catch (Exception e)
            {
               System.err.println("closing: " + e.getMessage());
            }
         }
      }
   }
}

/*
 *  Session-handler class to handle one remote client
 *  in a separate thread.
 */











class Chatter implements Runnable
{
   // the connection to the remote client 
   BufferedReader in = null;
   PrintWriter out = null;
   private Socket client;
   private ChatterList chatterlist;
   private  Chatter chatter;
   String line;
   ChatterList cl = new ChatterList();
   String username;
   Tail next;
   Chatter(Socket s, ChatterList cl, Chatter c)
   {
      client = s;
      chatterlist = cl;
      chatter = c;
   }
   public String getname()
   {
       return username;
   }
   public void sendToUser(String line)
   {  
        out.print(line);
        out.flush();    
   }
   public void run()
   {
      // for I/O
      
      //chatterlist.addConnection();
      
      
      try 
      {
         // set up I/O
         in = new BufferedReader
           (new InputStreamReader(client.getInputStream()));
         out = new PrintWriter
           (new OutputStreamWriter(client.getOutputStream()));
         
         // enter USERNAME
         out.println("Hello! Enter name");
         out.flush();
         
         username = in.readLine(); //type user name
        
         out.println("Welcome "+username);//print user name
         out.flush();
         cl.connect(this);
                 
         // for client input
         
         boolean done = false;
         while (!done)
         {
             line = in.readLine(); 
            
             
            
          
            //sendToUser(line);
            
            cl.broadcast(line);

            if ((line == null)
                || (line.trim().equals("BYE")))
            {
               // quit
               done = true;
            }
            
            
         
      }}
      catch (IOException e)
      {
         // fatal error for this session
         System.err.println(e.getMessage());
      }
      finally
      {  // close connections
         try 
         {
            in.close();
         }
         catch(IOException e)
         {
         }
         if (out != null)
         {
            out.close();
         }
         if (client != null)
         {
            try 
            {
               client.close();
            }
            catch (IOException e)
            {
            }
         }
      }
   }

}



class Tail{
    Chatter theChatter;
    Tail next;
  Tail(Chatter c, Tail n)
   {
      theChatter = c;
      next = n;
      //c.next = next;
   }
}











class ChatterList 
{


   private Tail thelist = null;
  
   
   public ChatterList()
   {
   }

   private void add(Chatter c)
   {
      
      
     
      thelist = new Tail(c, thelist);
      System.out.println("new user: "+c.getname());
      c.next = thelist;
    
      
   }

   
   public void broadcast(String line)
   {
      // variable for traversing the linked list
      Tail tmp = thelist;

      /*
       * traverse the list
       */
      while (tmp != null) // exit loop at the end of the list
      {
         // send the message
         tmp.theChatter.sendToUser(line);
         // and move on to the next chatter in the list
         tmp = tmp.next;
      }
   }

   public void connect(Chatter c)
   {
      // inform all other chatters
      broadcast(c.getname());
      // and add the new Chatter to the list
      add(c);
   }

   
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Multithreaded Chatroom Server

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 12:40 PM

eed to save icoming in an ArrayList as i do in the ArrayList al in my tutorial

           while(keepGoing)   
             {  
                 // format message saying we are waiting  
                 display("Server waiting for Clients on port " + port + ".");  
                    
                 Socket socket = serverSocket.accept();      // accept connection  
                 // if I was asked to stop  
                 if(!keepGoing)  
                     break;  
                 ClientThread t = new ClientThread(socket);  // make a thread of it  
                 al.add(t);                                  // save it in the ArrayList  
                 t.start();  
             }  


Was This Post Helpful? 0
  • +
  • -

#3 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 12:51 PM

thanks for the quick reply but the assignment asks for the chatter list to be implemented with a linked lists. after some testing on my code i think the chatter is being stored in the first node of the linked list, however it won't store the next chatter in the next node i think it just overwrites the first node.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 12:56 PM

So make it a linked list instead of an ArrayList, the principle stays the same
Was This Post Helpful? 0
  • +
  • -

#5 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 86
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 01:02 PM

Then the problem would lie in your list class, and more specifically in the add method. I am having a bit of trouble understanding your code, but what you have to do is make a new node, make the nextnode variable of the tail point to the new node, and make tail point to the new node.
Was This Post Helpful? 0
  • +
  • -

#6 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 01:07 PM

how do you mean? as the new chatter is added to the linked list via the call to cl.connect.(this) which the connect method calls the add method which adds a chatter to the list. but i can't get multiple chatters adding, because when i print sytem.out.println("new user: "+thelist.theChatter.next); it returns null when it should return the second chatter.?

hey Limecore, thank you for your help, i agree with you i am trying to make the new next node in the tail class in the chatter class but am stuck how any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 01:09 PM

And you can't use the stand API LinkedList class ?
Seems to me like an hell of an assigment: mastering Java linked list concept, multithread, and TCP/IP all in one single homework. Your teacher should have give a break at least on one of these concepts :)
Was This Post Helpful? 0
  • +
  • -

#8 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 01:12 PM

View Postpbl, on 28 November 2012 - 01:09 PM, said:

And you can't use the stand API LinkedList class ?
Seems to me like an hell of an assigment: mastering Java linked list concept, multithread, and TCP/IP all in one single homework. Your teacher should have give a break at least on one of these concepts :)/>

i know tell me about it i can send the requirements brief if that helps, i am not looking for you to do it of course just a bit of help on the bit that Limecore pointed out.
cheers

View Postpbl, on 28 November 2012 - 01:09 PM, said:

And you can't use the stand API LinkedList class ?
Seems to me like an hell of an assigment: mastering Java linked list concept, multithread, and TCP/IP all in one single homework. Your teacher should have give a break at least on one of these concepts :)/>

i know tell me about it i can send the requirements brief if that helps, i am not looking for you to do it of course just a bit of help on the bit that Limecore pointed out.
cheers
Was This Post Helpful? 0
  • +
  • -

#9 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 01:18 PM

View Postdandansmi, on 28 November 2012 - 01:12 PM, said:

View Postpbl, on 28 November 2012 - 01:09 PM, said:

And you can't use the stand API LinkedList class ?
Seems to me like an hell of an assigment: mastering Java linked list concept, multithread, and TCP/IP all in one single homework. Your teacher should have give a break at least on one of these concepts :)/>/>

i know tell me about it i can send the requirements brief if that helps, i am not looking for you to do it of course just a bit of help on the bit that Limecore pointed out.
cheers

View Postpbl, on 28 November 2012 - 01:09 PM, said:

And you can't use the stand API LinkedList class ?
Seems to me like an hell of an assigment: mastering Java linked list concept, multithread, and TCP/IP all in one single homework. Your teacher should have give a break at least on one of these concepts :)/>/>

i know tell me about it i can send the requirements brief if that helps, i am not looking for you to do it of course just a bit of help on the bit that Limecore pointed out.
cheers


sorry added post twice, yer we can use the standard api linked lists, e.g.. LinkedList ll = new LinkedLint(); but i used the way the lecturer showed us like i have done on chatter list.
to be honest i would rather use standard Linked list api but couldn't get it to work. cheers
Was This Post Helpful? 0
  • +
  • -

#10 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 04:37 PM

any susgestions ?
thanks
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Multithreaded Chatroom Server

Posted 28 November 2012 - 07:26 PM

Suggestions about what ?
Fix your LinkedList problem in a console mode application having nothing to do with your Thread and TCP connections.
When you'll masterized that way of organizing data then incorporate it to your application.

Divide ut reg
as old Julius Caesar said. No reason to debug two different threads at the same time
Was This Post Helpful? 0
  • +
  • -

#12 dandansmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-November 12

Re: Multithreaded Chatroom Server

Posted 29 November 2012 - 04:21 AM

right got it adding to the list however can't work out how to add a second chatter, i know how to use liked lists it incorporating a new thread chatter into the tail end of the list i am stuck on. people have pointed out the problem i have already pointed out, i know the problem just cant get past it, how are we young generation meant to learn if no knowledge is passed down.? what i need is in the list class, and more specifically in the add method. but what i need to do is make a new node, make the nextnode variable of the tail point to the new node, and make tail point to the new node. that way if i have two clients in the server it will add the first one to the start of the list and the second to the tail of the list. but don't know how.
???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1