Results 1 to 10 of 10
- 09-11-2012, 11:52 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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
- 09-12-2012, 12:01 AM #2
Re: One server, multiple clients question
This is backwards, clients connect to a server.I'm trying to make a server connect to multiple clients
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.how does everyone make a one-to-many connection using JavaIf you don't understand my response, don't ignore it, ask a question.
- 09-12-2012, 12:04 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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.
- 09-12-2012, 12:30 AM #4
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.
- 09-12-2012, 01:34 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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
- 09-12-2012, 01:48 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Re: One server, multiple clients question
How do you think servers work if not using threads?
Please do not ask for code as refusal often offends.
- 09-12-2012, 02:50 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Re: One server, multiple clients question
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:
But since the two methods mentioned above block the program, it would freeze at "acceptNewConnections()" or at "handleRequests(client)".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 } }Last edited by orestis125; 09-12-2012 at 02:55 PM.
- 09-12-2012, 05:38 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
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.
- 09-13-2012, 02:09 PM #9
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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.
- 09-13-2012, 02:34 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
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.
Similar Threads
-
Have server echo output to multiple clients
By Wnt2bsleepin in forum New To JavaReplies: 19Last Post: 05-07-2012, 02:26 AM -
multiple clients single server via sockets
By acks in forum NetworkingReplies: 3Last Post: 04-12-2011, 07:44 AM -
how to connect a server to multiple clients
By yontan8888 in forum NetworkingReplies: 1Last Post: 02-02-2011, 11:42 AM -
Single server, multiple clients - JAVA sockets, No threads
By sadishm in forum NetworkingReplies: 1Last Post: 10-25-2010, 11:21 PM -
how to connect a server to multiple clients?
By azhar in forum NetworkingReplies: 15Last Post: 03-22-2010, 12:54 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks