Results 1 to 9 of 9
  1. #1
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default Multi Client TCP or UDP

    Hi, I want to make a messenger feature on one of my programs.

    one thing I came across is that while I can connect one client to my server, I don't know how to make the server respond to multiple clients.

    what is the best way to make a multi client feature?

    I don't care whether it's tcp or udp, because I have made a version of my program for both tcp networking and udp.

    thanks

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    What have you tried?

    If the interaction with the server is "quick enough" you can just let one server handle it.

    wait for command, Accept it, do it, format a response, send it, close the connection, repeat.

    If you take "too long" then the usual approach is to setup a thread pool, and kick off a thread for each request.

    UDP or TCP has nothing to do with this.

  3. #3
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default

    //------------------------------------------------------------------------
    public void run() {

    socket = new DatagramSocket(1985);
    while(b)
    {

    //receive message from client.
    byte[] buf = new byte[100];
    DatagramPacket packet1 = new DatagramPacket(buf,buf.length);
    socket.receive(packet1);


    //declare string message.
    String message = new String(packet1.getData(),0 ,packet.getLength());

    //declare bytes for return message.
    byte[] buf2 = new byte[100];
    buf2 = message.getBytes();

    //get address and port from packet1.
    InetAddress address = packet1.getAddress();
    int port = packet1.getPort();
    DatagramPacket packet2 = new DatagramPacket(buf2,buf2 .length,address,port);

    //send packet2.
    socket.send(packet2);



    }
    socket.close();


    }
    //------------------------------------------------------------------------

    This is my server- with the try and catch statements omitted, because they make it messy and confusing to analyze.
    It is in a thread.

    yes, basic...but its what I need for my messenger at the moment.

    any ideas on how I would make this multi client?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    What happens when a second client connects to your server?

  5. #5
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default

    the script above, which is my server, only returns the message to the sender.

    a thousand clients can be using this server, but my server will only return the message to the client that sends a message to the server.

    as you can see, it uses the getAddress() method to get the address of the sender.



    So more accurately, my question is: how can I make the server get a message from one single client, and send it to all clients that have connected?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    my server will only return the message to the client that sends a message to the server.
    How will the server get the addresses of the other clients?
    If the server does have the addresses of the others, say in a table, then it will need to connect to them. They will each have to be servers to THE server who would be acting like a client. Ie the clients will wait for THE server to connect to them and send them a message.

    I'd think you'd want it so that each client connects to the server(logins) and then can receive messages from the server. A client after logging in would send a message to the server that was to be sent to other clients and the server would have connections to those clients and be able to send them the message.

  7. #7
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default

    //------------------------------------------------------------------------
    public void run()
    {
    //create socket

    socket = new DatagramSocket(5000);

    //create arrays: one for addresses, 50
    //slots long, which means the server can serve 50 users.
    //slot 0 is not used.

    address = new InetAddress[51];
    port = new int[51];
    int k;
    for(k = 0; k<=50; ++k)//set the arrays to default values
    {

    address[k] = null;


    }


    //this is how much space has been taken in the arrays.


    //listen for packet, receive it, then do stuff.
    while(b)
    {
    //receive message from client.
    byte[] buf = new byte[3];
    DatagramPacket packet = new DatagramPacket(buf,buf.length);

    socket.receive(packet);

    //get the address of the client that sent the message.
    InetAddress current_address = packet.getAddress();

    //get string from packet.
    String message_receive = new String(packet.getData(),0 ,packet.getLength());

    //test the packet's message.

    if(message_receive.equals("add"))
    {
    boolean bool = true;
    int c;
    for(c=0; c<=50;++c)
    {
    if((address[c] == null) && (bool = true))
    {

    address[c] = current_address;
    bool = false;
    }
    }

    }
    else if(message_receive.equals("sub"))
    {
    int c;
    for(c=0; c<=50;++c)
    {
    if(address[c] == current_address)
    {
    address[c] = null;

    }

    }
    }



    }
    socket.close();

    }//end while loop/listener
    }//end method run().
    //------------------------------------------------------------------------
    what you see above is a second server I made to run simultaneously with the first.

    what it does is, when the client first logs on, it sends a message to this second server, and the second server puts the address into an array.

    when i start the two servers, they work fine. however, when i start the client, and try to access the array of addresses, I get this error:

    "
    Exception in thread "main" java.lang.NullPointerException

    "

    pretty much what this second server is supposed to do is store the addresses so that my first server, when add the feature to do so, will be able to use the array of Inetaddresses from the second server,sending a message to every address stored in the array.

    i plan to do this by writing the firstserver to make a datagram for all 50 addresses in the second server's array.

    however, because of the error above, am I doing something wrong in accessing my array from the second server?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    There is an object that is NULL in your code.
    Copy and post the FULL text of the error message. The error message will have a line number in your program that you can look at to see what object was null.

    I'd change the design to have only one server. When it receives a login message from a client, it should create an object for that client(Define a new class here) and save it in some kind of collection, like an ArrayList. Then when the server gets a message to send to all clients, it can go thru the collection of logged in clients, get their addresses and send them the message.

  9. #9
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default it works!

    I fixed it! WAHOO!

    thankyou very much, norm and fishtoprecords for your input.




    WAHOO!

Similar Threads

  1. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  2. Help me to do Multi View from one Document
    By Nicz in forum AWT / Swing
    Replies: 4
    Last Post: 06-08-2008, 02:23 PM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  4. How come multi thread don't look like it?
    By jkhoa in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-22-2007, 04:25 AM
  5. Axiomatic Multi-Platform C 1.6.4
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-21-2007, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •