Results 1 to 16 of 16
  1. #1
    azhar is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default how to connect a server to multiple clients?

    hi all ,
    can someone tell me how to connect a server from multiple clients,if possible please provide me the code

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually multiple clients are connected to the server, while server listen through it's socket.

    Anyway, this is a forum not a code bank to just give codes away. You have to make an attempt and if you stuck with something ask your questions, lots of members are here ready to help you.

  3. #3
    azhar is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    thanx a lot for ur suggestions.........

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    very9music is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by azhar View Post
    hi all ,
    can someone tell me how to connect a server from multiple clients,if possible please provide me the code

    The first you need to create a ServerSocket object. This object will wait for connections. When a client connect to this server, a new connection is generated by
    Java Code:
    ServerSocket ss = new ServerSocket(2008);
    while(true) {
            Socket s = ss.accept();
            //You handle socket s here, for example, you create a new Thread which will operate this socket
    }
    ///
    The code so long. You can refer here java2s.com/Code/Java/Network-Protocol/Agenericframeworkforaflexiblemultithreadedserver.h tm

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Also, you probably look at avoiding creating a brand new Thread for each connection, either for performance if you have a moderate number of requests per second, or just to control the number of concurrent threads. Take a look at ThreadPoolExecutor class (link is to a web page of mine with an example in case its useful).

    If you have a high volume of requests (say, more than 100 simultaneous connections), look at Selector class in the NIO package, which allows you to interleave connection processing in a single thread.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    But I don't think working on with multiple threads is a good solution. If you can schedule on single worker thread it's much better. Major reason I've see on multiple threads is, thread killing, in case of large number of client. How about millions of customers at a second, like Skype does....

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    The major reasons for the thread-per-connection paradigm are:
    - historical -- before the widespread availability of readiness selection, that's just "how it was done";
    - nowadays, ease of programming -- conceptually, having a thread per connection is "the thing you want to do"; it's just unfortunately not very scalable.

    Actually, a thread per connection isn't so bad if you pool threads and know that you won't have too many simultaneous connections. Most web servers running in the world today are boring old thread-per-connection servers. On a reasonable-spec server, you can serve hundreds of simultaneous connections that way.

    As I say, a Selector will get you up to thousands of simultaneous connections on a single server.

    Peer-to-peer services like Skype handle a huge volume of connections, but not from a single server!!!
    Last edited by neilcoffey; 12-17-2008 at 04:38 AM. Reason: typo

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed with you. But I have a little doubt here, how the selector select a thread you talking about. Just forget about multiple servers, respect to one?

  10. #10
    fabian is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi, I am looking for any links that can help me out. I have searched for the last few days and cant figure this out. I was able to connect 1 server and 1 client but cant get multiple clients. Here is exactly what I am trying to do.

    -1 server, 10 clients
    -each client enters a name
    -there are 10 trials
    -for each trial, each client enters a number, that number is sent to the server.
    -Once all the numbers for the trial have been recieved, the server generates a random number and then selects the winner based on the clients numbers. It then sends the name of the winner to all the clients
    -then the next trial begins and we repeat

    I was having trouble determining which client the number was from and also in the examples ive seen I cant figure out if u can leave the connection open for a while or should you close it. Is 5 minutes too long?

    Thanks!

  11. #11
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    - To get multiple clients, as mentioned now in this thread, the easiest way is to swpan one thread per client. So your "main" thread sits waiting for connections; when one comes in, it spawns a new wone to handle the connection. Off t'top of my head, something like this:

    Java Code:
    ServerSocket ss = new ServerSocket(portNo);
    while (!shutDownRequested) {
      Socket s = ss.accept();
      Thread thr = new HandlerThread(s);
      thr.start();
    }
    ...
    private class HandlerThread extends Thread {
      private Socket s;
      HandlerThread(Socket s) {
        this.s = s;
      }
      public void run() {
        //  handle connection, reading and writing to s
      }
    }
    - to know which client a connection/request comes from, just allocate a number from your server the first time you hear from a given client and pass it up to the client; whenever a client connects, it has to tell you its previously-allocated number (or say "I have no number -- please tell me my allocation");
    - in a real production environment, you'd have to worry about clients not being able to guess each others' numbers, but I guess that doesn't matter too much for you now (if it does, a starting point is SecureRandom over an HTTPS connection);
    - you probably want to allocate each game a number too, or at least one to each client/game pair -- then a client can ask the server "what was the result of game X, for which I am client Y" and the server can respond with "it was Z", or "sorry, I don't recognise that game number" etc;
    - you can TRY and keep the connection open, but you shouldn't RELY on it; when you want to keep the connection open it's good practice for the server to periodically send the client a "PING" message, and the client reply with some response to say "I'm still alive" -- that way, the the connection doesn't time out
    - but either way, you should assume that a connection can randomly get closed at any minute for some spurious unknown reason -- that is what the real Internet is like...

  12. #12
    jack81 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    thaks for giving source code

  13. #13
    Rakesh Gupta is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    IS any one can tell me that how to count the serverside response through Socket programming.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you mean counting response from the socket? You want to count number of response in ordinary ways, or mean something different?

  15. #15
    rahulgopal is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default multiple client connect to single server?

    hi. the clients are connecting to the server but the server interact with the first client which connect ........ so how can i pass the message it the same time to all the client which get connected.......

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, welcome to our community. :)

    Please don't interact with un-related thread next time. I didn't see any relation with the original question. Anyway, did you know about synchronization and stuff?

Similar Threads

  1. SSL Server with authentication of clients
    By zoltan in forum Networking
    Replies: 0
    Last Post: 10-15-2008, 12:27 AM
  2. Replies: 0
    Last Post: 08-25-2008, 03:13 PM
  3. Server not able to connect client
    By Kapil Gupta in forum New To Java
    Replies: 15
    Last Post: 07-22-2008, 05:09 AM
  4. blocked between server and clients
    By ibtehal in forum Networking
    Replies: 6
    Last Post: 07-17-2008, 01:30 AM
  5. connect to web server from mobile using java app.
    By jankrishnan in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2008, 05:12 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
  •