Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can server push data to client?

Tags:

java

sockets

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

like image 336
user12384512 Avatar asked Jul 01 '10 22:07

user12384512


1 Answers

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.

like image 93
David Gladfelter Avatar answered Nov 13 '22 05:11

David Gladfelter