my dog learned polymorphism
The moose likes Sockets and Internet Protocols and the fly likes socket programming : connect server to client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket programming : connect server to client" Watch "socket programming : connect server to client" New topic

socket programming : connect server to client

anitha meena
Ranch Hand

Joined: Oct 22, 2007
Posts: 40
Hi All

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 ""
2)I have run 2 clients using the application ""

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
anitha meena
Ranch Hand

Joined: Oct 22, 2007
Posts: 40
Hi All , Any reply ................
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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.
anitha meena
Ranch Hand

Joined: Oct 22, 2007
Posts: 40
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) ......
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
lianchi gao

Joined: May 12, 2007
Posts: 7
hi Dear:

My method is to send XX to the client whick you want to send msg to and you don't know if it is alive.

if execute the send XX method ,if the client is inalive, the server must catch a excepthion.

Notice: XX is your protocol like a Heartbeat message between server and client.

I agree. Here's the link:
subject: socket programming : connect server to client
It's not a secret anymore!