views:

308

answers:

2

Hi I am running a simple UDP Java Server, which collects IP and Port of Client when connected, store information in Database.

Client is still listening to server. Server stops.

Later, server want to reuse the database information, to reach the client; and as the client is still listening to same port of server, I guess client should receive the communication.

I am new to UDP, please let me know the way to achieve the above objective. Thank you.

Let me rephrase the question as I did try the ways suggested by members of Stackoverflow.

The client can be contacted by server within a short timespan, but after say 10 mins the client is unreachable; although it appears client is ready to listen to server for all the time but the server cannot reach client even if tried for several time. What could be cause for this? please let me know the way to deal with this

A: 

I think you are a bit confused regarding the UDP protocol (RFC 768). I think it would be helpful to review the UDP protocol to understand the differences between UDP and TCP.

Regarding your specific problem, it is difficult to know what is your exact problem without any type of code. There is a Client-Server in UDP example available in the sun tutorials.

Daniel H.
The client can be contacted by server within a short timespan, but after say 10 mins the client is unreachable; although it appears client is ready to listen to server for all the time but the server cannot reach client even if tried for several time. What could be cause for this? please let me know the way to deal with this
Without the specific code it is difficult to point out which could be the problem. I suggest you to check that the socket the client is using is the same you use to send the first datagram. It may be possible that you are creating two sockets, and the server is sending datagrams to a port the client is no longer listening on.
Daniel H.
A: 

UDP is sessionless, so I guess it should indeed work.

It would go something like that:

// Client:

socket = new DatagramSocket();
DatagramPacket req = new DatagramPacket(data, data.length, serverAddress, serverPort);
socket.send(req);
DatagramPacket resp = new DatagramPacket(new byte[MAX_RESP_SIZE], MAX_RESP_SIZE);
socket.receive(resp);

// Server:

DatagramSocket socket = new DatagramSocket(port);
while (!stopped) {
    DatagramPacket req = new DatagramPacket(new byte[MAX_REQ_SIZE], MAX_REQ_SIZE);
    socket.receive(req);
    saveToDatabase(req.getAddress(), req.getPort());
}
socket.close();

// Then later:

DatagramSocket socket = new DatagramSocket(port);

// retrieve clientAddr and clientPort from database
DatagramPacket resp = new DatagramPacket(data, data.length, clientAddress, clientPort);
socket.send(resp);
socket.close();
Maurice Perry
The only difference is I am creating datagram packet as follows for communicating from server to client later:DatagramPacket resp = new DatagramPacket(data, data.length);resp.setSocketAddress(new InetSocketAddress(clientIp, clientPort));should this matter?
I don't think so
Maurice Perry
The client can be contacted by server within a short timespan, but after say 10 mins the client is unreachable; although it appears client is ready to listen to server for all the time but the server cannot reach client even if tried for several time. What could be cause for this? please let me know the way to deal with this
Do you have a firewall between your server and your client?
Maurice Perry