1

I have written UDP server client program. The problem that i am facing is that when i am running server program, it is not waiting for client to connect. Whole code is executing after running till the end. And when i am running client side in between of execution of server side, client side is receiving data from its point of execution. Here is my server code-

public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(4321); byte[] sendData; String sentence = null; FileInputStream file = new FileInputStream(new File("E:\\Deepak.txt")); InetAddress IPAddress=InetAddress.getByName("localhost"); BufferedReader in = new BufferedReader(new InputStreamReader(file)); do{ while((sentence = in.readLine()) != null) { Thread.sleep(3000); System.out.println(sentence); sendData = sentence.getBytes(); DatagramPacket sendPacket =new DatagramPacket(sendData, sendData.length,IPAddress,9876); serverSocket.send(sendPacket); } }while(true); } 

Here is my client side code-

public static void main(String args[]) throws SocketException, UnknownHostException, IOException { DatagramSocket clientSocket = new DatagramSocket(); byte[] receiveData = new byte[1024]; String sentence ; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); receivePacket.getLength(); System.out.println(receivePacket.getLength()); clientSocket.receive(receivePacket); sentence = new String( receivePacket.getData()); } } 

1 Answer 1

1

UDP is connectionless, so there is no such thing as a connection in it. You can only send a packet (in a fire and forget way, send in java will send the data to the port specified in the Datagram) or receive packet on the port (receive in java will block until there is a packet received).

So you would need to implement your own connection on top of UDP if you want to have a server that only sends data if client "connects" to it.

So summing up:

  • send won't wait for anything, it will just throw the datagram on the wire
  • receive will wait for a datagram to arrive

Having the above information you would need to write your own protocol to keep the connection.

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please tell me how i can implement my own connection on top of UDP if I want to have a server that only sends data if client "connect " to it. I am new to socket programming.
Wouldn't it be possible to change the server to do the receive? It is the easiest way.
No. My specific application needs to be implemented server as sender and client as receiver.
OK, so your client would need to have 2 threads, one will be doing receive and the second one will send in a loop (until the receive receives a datagram). Server will do the opposite, it will wait for receive, and only after it gets the initial datagram it will start doing send.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.