views:

57

answers:

1

Learning java server technologies, trying to clarify some things. There are few technologies that allow java applications to communicate with each other.

1) Web services (REST/SOAP) over http

2) Simple POST/GET using URLConnection over http.

3) Sockets

4) RMI = Sockets + Object Serialization + Some Utilities

5) Different media servers like red5 = rtmp or rtmpt

All this technologies assume that there is a server application and client application. Client application know server address and should be initiator of call. And as far as I understand there are noway for server to push data to client, he can only send data back as response to client. And It is possible, if only they change their roles, in this case server should know client IP.

So I wondering how does network games work ? Is it possible to open connection and exchange data between server and client for all time without pulling request from client every 1-2 seconds and server would not know client IP. I’m not talking about Comet and other hacks.

Thank you

+3  A: 

Most two-way, persistent applications open a network port and keep it open. The server listens on a well-known port, and when a client connects to that port, it gets a persistent TCP connection to the server. A connection like that is basically a bi-directional "pipe", data can flow serially in both directions simultaneously. As long as that pipe is open, the server listens for messages from the client and the client listens for messages from the server.

The common I/O API's used for TCP pipes allow either end of the connection to be "woken up": An asynchronous I/O request executes a registered callback function from a thread managed by the OS or I/O library or the client/server ties up its own thread on a "blocking" call, which returns when data is available. No polling or re-connection attempts are necessary with this model, the client and server both "know" when data is available and can act on it immediately.

David Gladfelter
Also realtime network games often do actually "cheat" a bit. Every client updates the world locally in realtime, and does some trick to resync the shared simulation when a new update from the server arrives.When you shoot at your enemy may be he'll dodge your shot getting only a small damage or may be not. Of course it is the server that will decide fairly depending on if the dodge command from that client arrived before or after the shot... but the attacking player probably won't notice an eventual incongruence between the 3d animation shown and the number of energy bars subtracted.
6502