Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
socket based chat system Posted by carnage666 on 4 Nov 2005 at 2:56 AM
I have been given an assignment to write a simple socket based chat server/client application. The client applet needs to connect to the server app on startup, the server needs to be able to deal with multiple clients, with no limit on the number of clients, and the hostname/IP address of the server must not be hardcoded into the client.
My main problem is that I am not sure where to start on the server!!
Any links or help that you could provide me with would be extermely helpful...thanks in advance!!
Report
Re: socket based chat system Posted by Vilanye on 5 Nov 2005 at 2:45 AM
: I have been given an assignment to write a simple socket based chat server/client application. The client applet needs to connect to the server app on startup, the server needs to be able to deal with multiple clients, with no limit on the number of clients, and the hostname/IP address of the server must not be hardcoded into the client.
: My main problem is that I am not sure where to start on the server!!
: Any links or help that you could provide me with would be extermely helpful...thanks in advance!!
:

Where are you supposed to get the IP and port? Enter it through command line arguments?

I think the easiest way is to have one class that accepts connections and then another class that either extends Thread or implements Runnable, and start a new thread for each user, and also have a vector that holds each socket object, and make it static and create a static method that will send out the message to everyone connected.

Something like this:
public class Chat implements Runnable
{
   private static Vector sockets;
   //whatever else you need, probably a string to hold the users name

   public Chat(Socket sock, ...)
   {
     //add socket to vector
   }

   public void run()
   {
     //recieve messages, and disconnect handling here
     //pass message to broadcast
   }

   public static void broadcast(String str)
   {
     //loop through vector and send message to everyone
   }
}


On second thought, all you would need is a vector of whatever output stream class you are using in the vector, and not the socket objects. You might still need to pass the socket objects so you can close it properly and let the thread terminate, whether the cleint disconnects properly or unexpectedly.


Just my 2 bits




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.