Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am designing a game server which would be used for Android terminals. I've been searching for products or frameworks to use for two-way socket communication but haven't found anything worth mentioning.

Simply, I want to implement the architecture below:

Server architecture

In other words, one TCP connection from the client to the server, and another from the server to the client, in order to avoid always having to be connected.

Just to be clear, my main aim is for the server to be able to send data to the client, without the client having to explicitly request it. I do not want the client to have to poll the server to see if there is any new data.

What combination of design strategies, network protocols, and/or products or frameworks (if any) would be appropriate for implementing this architecture?

share|improve this question

migrated from stackoverflow.com Nov 5 '11 at 15:38

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 8 down vote accepted

The problem is that the client will often be behind a NAT/firewall, which means that the server can't initiate a TCP connection to the client, because the client doesn't have a globally routable IP address and/or because the firewall will block the unexpected incoming TCP packets coming from the server.

So that means that any TCP connection will have to be initiated from the client side. Once that is done, you have the option of leaving the TCP connection open indefinitely (which you don't want to do) or closing the TCP connection once any immediate business has been attended to (but that means that the client will need to connect again later to see if more updates have arrived in the meantime, which is polling, which you also don't want to do).

So you're kind of stuck. The only possible solution I can think of is to set up a number of proxy servers that are not behind firewalls/NATs, and have your Android clients persistently connect to them. The proxy servers can then forward requests on to your main server, and since you control the proxies, you can make sure the server is able to connect back to them as necessary. If you're lucky, somebody else has already set up a system like this and you can simply piggyback on it... perhaps this will do what you want.

share|improve this answer
    
Thanks , this is the answer i was waiting for. –  A.Quiroga Nov 5 '11 at 16:39
    
+1 This is pretty much how games requiring this kind of connection work. –  Patrick Hughes Nov 6 '11 at 6:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.