• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

socket programming : connect server to client

 
anitha meena
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
TCPEchoClient.java




TCPServerSelector.java



TCPProtocol.java



EchoSelectorProtocol.java




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
 
anitha meena
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All , Any reply ................
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lianchi gao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic