wood burning stoves 2.0*
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 8 in Action this week in the Java 8 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
Author

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:
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

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

Joined: Mar 22, 2005
Posts: 39577
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
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
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
lianchi gao
Greenhorn

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: http://aspose.com/file-tools
 
subject: socket programming : connect server to client
 
Similar Threads
non blocking socket
server can not accept all the clients connection request
ServerSocketChannel.accept() seems to create a random port to send data to?
NIO Socket weirdnes in Solaris 2.10
java.net.BindException: Address already in use