Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: One server, multiple clients question

  1. #1
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default One server, multiple clients question

    Hello everyone,

    Forgive me if this is the wrong place to post this. I'm new to this forum. I'm trying to make a server connect to multiple clients but I've encountered a problem. At first, when the server was accepting new connections I noticed that the method serverSocket.accept() blocks the program until a connection has been made. For that reason, I used a separate thread to handle the acceptance of incoming connections. However, now I've noticed a similar problem. I'm using a BufferedReader to read the data from the clients (which are in an array list) but the readLine() method seems to block the program as well until it reads some data. As a result, If the first client in the list doesn't send anything to the server, the server won't handle the requests of the rest of the clients because it gets blocked.

    I've searched online for solutions but most of them suggest using a separate thread for each client. At first this sounded like a good idea but with some more research I found out that having many separate threads is not good; especially in my case which I plan on having at least a maximum of 10 concurrent connections.

    So my question is, how does everyone make a one-to-many connection using Java? I couldn't find a way of setting the BufferedReader to a non-blocking mode.

    Thank you very much,
    I appreciate your help

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: One server, multiple clients question

    I'm trying to make a server connect to multiple clients
    This is backwards, clients connect to a server.

    how does everyone make a one-to-many connection using Java
    By using threads. When the serversocket connects, create a new thread and pass the socket to the thread to handle the connection from that client. The server code then loops back and waits another call using a serversocket.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: One server, multiple clients question

    Is there any other way to do this without multiple threads? I've read that having multiple threads is not efficient. Is this true?

    Thank you for your reply.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: One server, multiple clients question

    You will need threads, either explicitly in your code or implicitly by using some of the nio classes.
    Otherwise the one thread will block waiting for input.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: One server, multiple clients question

    Thank you, everything is working as expected using threads. Hopefully this won't result in any problems when having many clients.

    Thanks again, I appreciate your help

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: One server, multiple clients question

    How do you think servers work if not using threads?
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: One server, multiple clients question

    Quote Originally Posted by Tolls View Post
    How do you think servers work if not using threads?
    It would easily be done if the methods ServerSocket.accept() and BufferedReader.readLine() were not blocking the main program. The only difference would be that only 1 client at a time would get handled, but there would be no waiting in between. For example:

    Java Code:
    while(true) { //main program loop
    
    acceptNewConnections(); //if no new connections, proceed
    
           for(Client client : clientList) {
           handleRequests(client); //if no requests from this client, proceed to the next one
           }
    }
    But since the two methods mentioned above block the program, it would freeze at "acceptNewConnections()" or at "handleRequests(client)".
    Last edited by orestis125; 09-12-2012 at 02:55 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: One server, multiple clients question

    So you would force everyone to wait in a queue?
    There's a reason threads are used.
    People don't like waiting in a queue if at all possible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: One server, multiple clients question

    I can't see why you think they will be waiting in a queue. Handling the request of a client can take a few hundredths of a millisecond depending on server CPU speed, the waiting will not be noticeable unless there are millions of clients (Like in Skype for example) in which case, a similar problem would occur with threads.
    Last edited by orestis125; 09-13-2012 at 02:19 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: One server, multiple clients question

    Depends what you're doing.
    Our requests take a few seconds for a flight search, significantly more for actually booking.
    Be a bit of a pain if you were stuck behind someone else in a queue.
    No point having multiple processors and cores if you're going to single thread the thing.

    In any case, you never actually said what problems multi-threading was supposed to cause...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Have server echo output to multiple clients
    By Wnt2bsleepin in forum New To Java
    Replies: 19
    Last Post: 05-07-2012, 02:26 AM
  2. multiple clients single server via sockets
    By acks in forum Networking
    Replies: 3
    Last Post: 04-12-2011, 07:44 AM
  3. how to connect a server to multiple clients
    By yontan8888 in forum Networking
    Replies: 1
    Last Post: 02-02-2011, 11:42 AM
  4. Replies: 1
    Last Post: 10-25-2010, 11:21 PM
  5. how to connect a server to multiple clients?
    By azhar in forum Networking
    Replies: 15
    Last Post: 03-22-2010, 12:54 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
  •