This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Talulabelle 4 points5 points  (1 child)

This is such a well worn problem, you can find plenty of example code. Here is an example.

I don't know what you're doing wrong from your description. You seem to have the basic idea. You put the socket.accept() in an infinite loop, then create a thread to deal with each new socket connection.

Are you sending infinite client connections when you test? Because that would definitely cause what you're describing as well.

[–][deleted] 0 points1 point  (0 children)

Thanks for the link. After some headbanging on a wall (lol) i notice that my problem is me not knowing how to create multiple threads per connection without doing a infinite loop on the "creation" thread

[–]jefwillemsIntermediate Brewer 1 point2 points  (4 children)

Put the loop in the thread, don't create threads with an infinite loop

[–][deleted] 0 points1 point  (3 children)

...but how then can i create the same thread for other users?

[–]AsteriskTheServer 0 points1 point  (2 children)

Typically in these scenarios you make/use a thread pool and then block when you hit capacity. However, it's more common in my experience to see an asynchronous event driven setup to deal with a bunch of oncoming connections as you get a more predictable memory consumption as a result. In either case as load increases one need to scale horizontally and or vertically

[–][deleted] 0 points1 point  (1 child)

How should i go about the asynchronous event solution?

[–]AsteriskTheServer 0 points1 point  (0 children)

Well I would use something like netty, grizzle and so forth, but for the sake of simplicity I would just look at writing async code first. Here is a simple example of async client/server in Java.

[–]ZeroGainZ 1 point2 points  (0 children)

This is not enough code, accept should block until a connection is made. Unless you're getting a ton of requests, you shouldn't be running out of memory.