5

I have a java program which runs as a server accepting connections, and I connect Android clients to it. Clients are connected for a long time to their sockets.

1) How many clients can I server concurrently have (in practice - im not talking about the number of ports now) on an average machine with 3 Gb RAM?

2) If the phone uses 3G for the connection, is it possible that the socket is broken? If it is, how do I recover it or should it be done from the client side? Or is it done automatically? Does it happen often?

1
  • probably you should take a look at java nio or a project like netty if you are more interested in point 1. Commented Jun 27, 2013 at 20:05

3 Answers 3

3

1) Depends on the what the server does for the clients. If the server just accepts the connection and does nothing more it can probably serve tens of thousands of clients. If the server does something that requires CPU, memory or I/O it can serve fewer clients.

2) Yes, TCP connections can break, even over wired networks. If the link comes back the socket connection is not broken; TCP handles the retransmission of lost data. The problem is, what happens if the link doesn't come back? If you use SO_KEEPALIVE the connection will be closed eventually, but since the default timeout is 2 hours applications sensitive to this issue implement their own timeout mechanisms.

Sign up to request clarification or add additional context in comments.

3 Comments

As of number 2, does it mean that the socket will recover under the hood if there was a socket breakdown? And the issue is how to detect if the socket has been broken for too long? (Yes, the socket is TCP)
A permanent connection is an illusion; TCP works by sending packets. A broken link means that packets go missing. If the link comes back, packets start arriving again. You can read here how TCP deals with packet loss: en.wikipedia.org/wiki/Transmission_Control_Protocol
@ArtemMoskalev The socket will not recover. TCP will tolerate intermitten connectivity issues and packet loss - and if you don't do any communication, it can tolerate connectivity loss for months. There are however many, very many, cases where it does not recover - so you have to handle that anyway (e.g. a NAT/firewall timing out your connection, network failures that causes TCP to time out, and many other situations). You have to detect this on the client so it can reconnect, and on the server so it can get rid of stale/dead sockets.
1

1) How many clients can I server concurrently have (in practice - im not talking about the number of ports now) on an average machine with 3 Gb RAM?

It depends upon how much resources are consumed by each connection (one thread per connection) at server side and for how long. What operations (CPU bound and IO bounds) are you performing for each request per connection. And if you are using Database to read data at server side then the number of connections can be decreased drastically.

2) If the phone uses 3G for the connection, is it possible that the socket is broken? If it is, how do I recover it or should it be done from the client side? Or is it done automatically? Does it happen often?

There might be many reasons for breaking of socket which includes : Server crash, Network failures, Socket Timeout or many more. If a Socket is broken then there is no way to recover it back. It can't be done automatically by the TCP. You will have to reconnect with server in that case. As specified in oracle official documentation of Socket#getInputStream()

Under abnormal conditions the underlying connection may be broken by the remote host or the network software (for example a connection reset in the case of TCP connections). When a broken connection is detected by the network software the following applies to the returned input stream :-

  • The network software may discard bytes that are buffered by the socket. Bytes that aren't discarded by the network software can be read using read.
  • If there are no bytes buffered on the socket, or all buffered bytes have been consumed by read, then all subsequent calls to read will throw an IOException.
  • If there are no bytes buffered on the socket, and the socket has not been closed using close, then available will return 0.

Comments

0

Need some more information, for example when the clients connect what do they do, wait for data send from the server o vice versa? Do they keep a dialog o just wait for the data transmitted from the other side without any dialog?

I suppose when a client connects you start a thread to dispatch that client, in that thread you may put a timeout, something like 15-30 minutes, if no data is received during that time you just close the socket and terminates the thread. Also, could the same IP establish more than 1 connection? If it is not so, you need to keep track of who is connected and terminate its prior session in case the IP is already in the list.

If it's important to keep track of the connections progress, I mean, you need to continue where it left off, then a precise dialog need to be implemented, to avoid duplicates transactions and or messages. And there is a long etcetera, but, as I said before, more information about the project is needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.