5

I have to send a UDP packet and get a response back from UDP server. I though UDP was analogous with a java.net.DatagramPacket in Java, but the documentation for DatagramPacket seems to be that you send a packet but don't get anything back, is this the right thing to use or should I be using java.net.Socket

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 2
    UDP is datagram. You don't get anything back unless the receiver sends something back. It is the same with a tcp socket. If you tell us what you want to do, maybe? – Erik Dec 19 '11 at 14:24
  • Ok the server already exists(not written by me) it should send something back when I send it a packet but how do i get the packet it sends back ? – Paul Taylor Dec 19 '11 at 14:29
  • first question is: does the server send udp back? If so, you need to read from you own UDP socket through the Datagram.receive() method. – Erik Dec 19 '11 at 14:39
  • Max has anwered below thanks. – Paul Taylor Dec 19 '11 at 15:31

4 Answers4

11

Example of UDP datagram sending and receiving (source):

import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}
bezmax
  • 25,562
  • 10
  • 53
  • 84
  • This looks good, but one there doesnt seem to be a packet length defined for the server, i.e I don't think the packet length of the packet returned by the server is always the same so do I just need to make sure the receiveData buffer is big enough to handle the biggest possible packet size. – Paul Taylor Dec 19 '11 at 14:42
  • Yes. UDP is unreliable protocol, and is rarely used to send big chunks of data over it. However, after the packet is received, you can read the actual amount of bytes contained from `DatagramPacket.getLength()`. – bezmax Dec 19 '11 at 15:02
  • 2
    `receivePacket.getData()` does not only contain the received data. It contains the unused portion of the buffer, too. To get a String with the received data use `new String(receivePacket.getData(), 0, receivePacket.getLength(), StandardCharsets.UTF_8)` and adjust the charset to what you expect. – Augustus Kling Apr 19 '15 at 12:23
  • Just wanted to confirm that it's acceptable to send/receive using the same socket object? I was reading another question where a user was adamant that there must be one socket to receive and another to send, although that seems impossible if they need to use the same port (you'd get an exception). – The Unknown Dev Jun 16 '17 at 22:49
2

You have to use a DatagramPacket and a DatagramSocket. When you send a packet you just send a packet. However when you receive a packet you can get a packet which was sent from another program (e.g. the servers reply)

http://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html

Socket is only for TCP connections.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

The Java documentation does cover how to write a client and a server.

http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

You want to look at DatagramSocket#receive

ptomli
  • 11,730
  • 4
  • 40
  • 68
1

That's precisely the distinction between UDP and TCP sockets.

UDP is broadcast, whereas TCP with java.net.Socket is point to point. UDP is fire-and-forget, analogous to publishing a message on a JMS Topic.

See: http://docs.oracle.com/javase/tutorial/networking/datagrams/index.html

wrschneider
  • 17,913
  • 16
  • 96
  • 176
  • UDP is not broadcast. Broadcast is an IP-level thing. – OrangeDog Jul 14 '17 at 16:28
  • May be more accurate to say that TCP can only be sent to a single destination, while UDP can be sent to multiple. See also: https://stackoverflow.com/questions/21266008/can-i-use-broadcast-or-multicast-for-tcp/21267149 – wrschneider Nov 06 '18 at 15:08