Reputation: 7327
I'm building a client server application from an android device to a server over 3G. Because of the network operator that is providing 3G, the client device is behind a NAT. Therefore, it is impossible for the server to make a connection to the client. I could adopt an approach of polling, however before doing so, I want to exhaust all other options. Is there some way ( I haven't yet found one ) to keep a TCP Socket connection open between the client and server, such that the server would be able to initiate communications to the client?
The scenario is that there will be very regular client/server communication based on server state that updates on an extremely regular basis - we would be talking every couple of seconds. Would UDP be a better option here? Maybe not as the NAT issue still knocks it on the head.
Are there any other options available to me?
Many thanks
Upvotes: 1
Views: 2450
Reputation: 310893
Just open a socket and use it, including the ObjectOutputStream. If the intermediate systems drop the connection you will get an exception, whereupon you close and repeat.
Upvotes: 2
Reputation: 287835
NAT does not mean that the server can not send anything. Just open a connection from the client and let the server send whatever events it records. You may also want to include a simple heartbeat protocol to make client and server register a broken connection and to make the NAT aware that the connection is still being used.
Upvotes: 4