This discussion is archived
10 Replies Latest reply: Jul 4, 2012 10:19 AM by 931685 RSS

Multi-clients server on one Socket

tmazight Newbie
Currently Being Moderated
Hi,

I'm writing a server which have to handle many client, one thread for each client. all clients will connect to one serverSocket binded to a specific port and will send different messages.

My question is : Is the isolation between client automatically handled or i have to do something to ensure that the messages of each client are separately read ? (one socket, multiple clients)

Thanks.
  • 1. Re: Multi-clients server on one Socket
    gimbal2 Guru
    Currently Being Moderated
    There is not "one socket". Each client will create a unique socket connection to the one server. Check out the echo server example to see that in action:

    http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
  • 2. Re: Multi-clients server on one Socket
    931685 Newbie
    Currently Being Moderated
    Yeah gimbal as told you the fact. From my little experience with socket each time a client connects to a server the client is making a uniques socket connection.

    from the this statement


    Socket socket = serversocket.accept(); // The client would be connecting to the server uniquely on its own socket.


    So it you have 3 clients connected to the server, that means you have 3 client socket that have connected to the server.
    That is why you have to make your network server multi-threaded so that it can handle all clients socket connection on individual thread. This would support multi-threading i.e multi processing
  • 3. Re: Multi-clients server on one Socket
    931685 Newbie
    Currently Being Moderated
    All you need to do is to make your server a multi-threaded server. You could even specify the number of connection that you want your server to be able to make at once and create a pool connection.
    I mean create a senerio whereby when your server starts running it creates threads waiting thread expecting to accept client connection. Immediately the client disconnects his socket is automatically deleted from the pool of thread connection that is specified.


    To cut the long story short, make your server multi-threaded by extending the Thread class or implementing the Runnable interface. With that all client socket connection would be accepted on a single thread. So individual message would be sent to the clients socket uniquely through the thread
  • 4. Re: Multi-clients server on one Socket
    EJP Guru
    Currently Being Moderated
    You could even specify the number of connection that you want your server to be able to make at once and create a pool connection.
    No you can't. Connections are accepted from the ServerSocket. There is no way to pool them at the server.
    I mean create a senerio whereby when your server starts running it creates threads waiting thread expecting to accept client connection.
    Now you are talking about a form of thread pooling.
    Immediately the client disconnects his socket is automatically deleted from the pool of thread connection that is specified.
    No it isn't. Your code has to read the EOS condition, close the socket, and remove it from whatever data structures it is held in. There is nothing automatic about this process whatsoever.
    To cut the long story short, make your server multi-threaded by extending the Thread class or implementing the Runnable interface. With that all client socket connection would be accepted on a single thread. So individual message would be sent to the clients socket uniquely through the thread.
    This barely makes any sense at all. Multi-threading results in a single thread?

    There was no real need to continue the thread at all, but if you do please try to make a positive contribution, instead of all this nonsense.
  • 5. Re: Multi-clients server on one Socket
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    To cut the long story short, make your server multi-threaded by extending the Thread class or implementing the Runnable interface. With that all client socket connection would be accepted on a single thread. So individual message would be sent to the clients socket uniquely through the thread.
    This barely makes any sense at all. Multi-threading results in a single thread?
    He he, deja vu. After more than a decade of people not expressing themselves properly you still don't fill in the blanks eh ;) You should get a crystal ball like the rest of us.
    To cut the long story short, make your server multi-threaded by extending the Thread class or implementing the Runnable interface. WithOUT that all client socket connection would be accepted on a single thread. So individual message would be sent to the clients socket uniquely through the thread.
    Fixed. Not that it does anything at all, it remains a very poor explanation. Socket connections ARE accepted on a single thread - its the communication with such a socket after accepting it that should be handled on isolated threads.
  • 6. Re: Multi-clients server on one Socket
    EJP Guru
    Currently Being Moderated
    After more than a decade
    After 25 years, if you start from when I first began on Usenet.

    My attitude is that if the compiler these people are supposed to use all day won't accept mis-spelling, bad punctuation, or incorrect syntax, or read their minds, why should I?
  • 7. Re: Multi-clients server on one Socket
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    After more than a decade
    My attitude is that if the compiler these people are supposed to use all day won't accept mis-spelling, bad punctuation, or incorrect syntax, or read their minds, why should I?
    Crystal ball dude, get one. You can probably find one cheap on fleabay. Its the path to that coveted "ace" forum status.
  • 8. Re: Multi-clients server on one Socket
    sabre150 Expert
    Currently Being Moderated
    gimbal2 wrote:
    EJP wrote:
    After more than a decade
    My attitude is that if the compiler these people are supposed to use all day won't accept mis-spelling, bad punctuation, or incorrect syntax, or read their minds, why should I?
    Crystal ball dude, get one. You can probably find one cheap on fleabay. Its the path to that coveted "ace" forum status.
    My observation of EJP's responses over the last decade indicate that he already has a very good crystal ball.
  • 9. Re: Multi-clients server on one Socket
    931685 Newbie
    Currently Being Moderated
    wooooow!!!! Ejb, I understand were you coming from. I guess i did not explain myself well enough. When I said single connection. I did not actually mean all the connections would be on a single thread. I simply meant every socket connection to a multi-threaded server would have its own unique connection that means if they are 3 client connections they are 3 threads.

    As for the pooling, yeap i know the socket don't just get disposed from the multi-threaded server. It must be removed from the data structure that it was stored in and also closed before it removal.
    I accept your corrections on this, I should have explained this in details before the post.


    Well thanks for the correction. I guess I would take more time to explain before making a post. I would also make proper perusal to avoid such errors.
  • 10. Re: Multi-clients server on one Socket
    931685 Newbie
    Currently Being Moderated
    Yeah concerning the connection issue.

    I meant the maximum number of connection that the server could accept from client sockets. I know quite well that the server listens and wait for an incoming client socket connection to accept.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points