Home arrow static arrow Java Programming [Archive] - Client/Server API with push?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Client/Server API with push?
This topic has 8 replies on 1 page.

Posts:3,369
Registered: 24.10.97
Client/Server API with push?  
Aug 9, 2004 7:52 AM



 
I set up a simple Socket client server architecture with the Java socket API. However, my clients sometimes are initiators or requests, and sometimes my server is the initiator of infos. Example: client want to get infos from server but also want to tell the server something. The server tells all registered clients something but also can accept requests from clients. So both sides are client AND server (depending on what they currently want to do).

The socket API is just a classic client/server API, i.e. a server listens and a client sends requets and get responses. For my server to send requests to all clients, this is not enough. Is the only way to have all clients offering a socket server on their computer, too? so that this 2-way-request-communication works?

This is basically some kind of "push" technology, but i don't want my clients to "poll" the server just to emulate the feature of sending requests from the server to the client. Is there some other (perhaps non-socket) API that offers this?
 

Posts:13,250
Registered: 24/10/97
Re: Client/Server API with push?  
Aug 9, 2004 8:21 AM (reply 1 of 8)



 
If you use RMI rather than raw sockets then a client can create and then register a remote object with the RMI server. The server can then notify ('tell') all (or even just some) registered clients when necessary by just calling a method on the remote object.

If you want 'multicast' ie any client able to send a message to all other clients then consider JMS.
 

Posts:3,081
Registered: 2/15/99
Re: Client/Server API with push?  
Aug 9, 2004 8:28 AM (reply 2 of 8)



 
A socket is two-way and will handle this just fine. Connecting to a socket is unsymmetrical (server waits, client connects), but after that it's completely symmetrical.

Have a thread at the client listen for server-to-client messages, and a thread at the server listen for client-to-server messages. You'll need one thread per client in the server, each sitting in read() or readLine() for the client's socket.

If you have many clients per server (say, more than a hundred), replace the threads in the server by a NIO selector and a single thread that waits on the selector. Having a couple of hundred threads is a bit much, as thread context switching and such start to drain OS & CPU resources.
 

Posts:3,369
Registered: 24.10.97
Re: Client/Server API with push?  
Aug 9, 2004 8:53 AM (reply 3 of 8)



 
RMI is not what i'm looking for (need to write skeletons, etc.).

@sjasja: how is the socket api symmetrical? The server calls serverSocket.accept() to wait for requests and then can write the response. How can a serverSocket connect to a client? The steps on the server are basically

- accept() //wait for client to send request
- socket.getInputStream() //read request
- socket .getOutputStream() //write response
- socket.close() //request-response cycle finished

i can't see any symmetric communication here ...
 

Posts:458
Registered: 7/1/03
Re: Client/Server API with push?  
Aug 9, 2004 9:03 AM (reply 4 of 8)



 
"Simple socket server" is a contradiction in terms.

RMI, JMS and EJB are there for a reason. Because what seems simple on the surface is actually a pain the keester.
 

Posts:13,250
Registered: 24/10/97
Re: Client/Server API with push?  
Aug 9, 2004 12:12 PM (reply 5 of 8)



 
RMI is not what i'm looking for (need to write
skeletons, etc.).
You obviously have not looked thoroughly enough at RMI! What you are asking for I have done with RMI on two of my last three projects. In both cases I used it to notify clients that a database had been updated. The clients then decided if they needed to update their views.

You don't need to write skeletons! You just need to define interfaces and then use RMIC (I hear rumours, though I have not tried it yet, that in1.5 you don't even have to user RMIC).

It would be well worth putting some more learning effort into RMI.
 

Posts:13,250
Registered: 24/10/97
Re: Client/Server API with push?  
Aug 9, 2004 12:16 PM (reply 6 of 8)



 
- socket .getOutputStream() //write response
- socket.close() //request-response cycle finished

Why are you closing the socket. If you keep it open then you can write anything you like back to the client through the output stream!

In the client you would have a thread waiting on the socket's input stream waiting for notifications to be sent to it.
 

Posts:3,081
Registered: 2/15/99
Re: Client/Server API with push?  
Aug 9, 2004 12:17 PM (reply 7 of 8)



 
@sjasja: how is the socket api symmetrical?

It is, trust me! Connection initiation isn't, but after that it is. (Let's assume connected TCP/IP.)

The server does bind() and accept(), the client does connect(). After that, the connection is symmetrical: either end can issue any number of read() and write() calls in any order. You write() to a socket, it pops out from read() at the other end (though not necessarily as a single packet; TCP/IP is stream oriented, so write() "packets" can and will be glued together, or fragmented at the whimsy of network routers and friends.)

- accept() //wait for client to send request
- socket.getInputStream() //read request
- socket .getOutputStream() //write response

Those don't read or write; they get streams which you can read from and write to.

In the server, when accept() returns a new connection, you'd start a reader thread for that client. The thread would read() (or readLine() if you implement a line-oriented protocol) and handle the incoming messages. Whenever you want to write to the client, write to the stream returned by socket.getOutputStream(). You may need to synchronize writes if you have several threads that do sequences of write()s that need to arrive at the client sequentially.

As to the difficulty of socket programming: I don't think it particularly difficult. But then I've written dozens of socket programs since my first one in '85... The [url=http://java.sun.com/docs/books/tutorial/networking/sockets/index.html]Java Tutorial socket chapter[/url] shows a simple client/server pair; not terribly difficult I think. In particular, check out the KKMultiServer at the bottom of the [url=http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html]sample server chapter[/url].
 

Posts:3,369
Registered: 24.10.97
Re: Client/Server API with push?  
Aug 10, 2004 1:21 AM (reply 8 of 8)



 
thanks. (of course i just used abbreviations when i wrote the comment "read request").
I also first had the idea of reading and writing on the same socket. but this leads to synchronisation problems. in addition, what happens if the client crashes? i need to write server kill threads that kill open socket connections after a specific time, as the timeout of sockets still doesn't work (correctly) up to JDK 1.4.2. so, i thought opening a socket for each request would be safer. but then, the client still can crash.

thanks for the links, i will look into these. i don't need a sophisticated protocol. i just need a crash save server side (timeout of socket connections). it can also lead to a memory problem (OutOfMemoryError) when there are too many sockets ghosting around. any advice about robust socket timeout facilities?
 
This topic has 8 replies on 1 page.