vjk
vjk

Reputation: 2293

problems with calling server socket accept method with an infinite timeout

I created a server socket on my side. I don't know when client connects to me.

Calling a serverSocket.accept() in my application without setting any socket timeout would solve my problem of accepting the client at any point of time. I am doing this in a separate thread, so this wouldn't block another part of my application.

My concern is since accept() blocks till it gets a call from a client wouldn't this consume any resources, may be like if I don't get a call from the client side program for about a week.

Is there any other way of accepting the client side connection. I don't have any information on when client calls for a socket connection, except that it calls at some point.

Upvotes: 2

Views: 1040

Answers (1)

Tudor
Tudor

Reputation: 62479

There is no problem with calling accept on a different thread. The thread will be blocked in the call to accept and will not be scheduled by the operating system until a connection in inbound. Practically, if you don't get a connection the entire week your thread will not run on the CPU during this time.

Therefore, it will not consume any CPU resources and only a bit of memory.

Upvotes: 1

Related Questions