socket based chat system - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

socket based chat system

carnage666carnage666 Posts: 9Member
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!!

Comments

  • VilanyeVilanye Posts: 684Member
    : 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:
    [code]
    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
    }
    }
    [/code]

    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.


    [italic][blue]Just my 2 bits[/blue][/italic]

Sign In or Register to comment.