Artem Moskalev
Artem Moskalev

Reputation: 5828

Java sockets (Android to Java server)

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?

Upvotes: 5

Views: 616

Answers (3)

ja_mesa
ja_mesa

Reputation: 1969

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.

Upvotes: 0

Vishal K
Vishal K

Reputation: 13066

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.

Upvotes: 1

Joni
Joni

Reputation: 111389

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.

Upvotes: 3

Related Questions