I am trying to develop client/server application using nonblocking nio, i have found example in the book "TCP/IP Sockets in Java Practical Guide for Programmers
Second Edition Kenneth L. Calvert - University of Kentucky Michael J. Donahoo - Baylor University "
the code as the following:
the example working fine , as the following
1)I have started running the server application "TCPServerSelector.java"
2)I have run 2 clients using the application "TCPEchoClient.java"
as i know that the The client initiates communication with a server
but sometimes want to send a message from server to a particular client or we want to check whether xyz client is one of active client connected to the server or not
so how i do achieve that in the above examples
Joined: Oct 22, 2007
Hi All , Any reply ................
Joined: Mar 22, 2005
Taking a quick look at the client, it seems that the client sends something to the server, receives a response, and then shuts down. If the server is supposed to send something to the client, then that implies that the client needs to be running - which it won't under the current setup. So for starters, you need to set up the client to keep running, and you need to change it so that it can receive multiple message from the server. That means it must be able to recognize if a server message is done, and when the next one starts. And, obviously, the server needs to be able to send messages at its discretion - which it currently can't, since it only responds to client requests.
Thanks alot i have fixed the issue but my question now is that
if the client establishes socket connection to the server and the server replies to this client . after that the client stays idle for long time let say 2 hours but the socket connection still alive , is it possible for the server now to send something to the client ( i mean the server will send request and the reply will from the client) ......
If the connection is still active then yes, it should be possible to send messages to the client. If the client is not reading from its input stream that data will not be processed though, so make sure the client keeps reading data from the server.