# Receive UDP packets

**URL:** <https://discourse.processing.org/t/receive-udp-packets/19832>\
**Category:** Libraries\
**Created:** [April 16, 2020, 2:15pm UTC](https://discourse.processing.org/t/receive-udp-packets/19832 "2020-04-16T14:15:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [April 16, 2020, 2:15pm UTC](https://discourse.processing.org/t/receive-udp-packets/19832/1 "2020-04-16T14:15:47Z")

</div>

Hi everyone,

I am currently trying to retrieve UDP packets from an ethernet module connected to my computer. I know that the module is transmitting the UDP packets and my laptop is receiving them (I used Wireshark to confirm it)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0399c2ee45dd5fdf326a65245a9820de22f4f060.png)  
However, when trying to run the following processing sketch to receive the UDP packets, I can’t receive them. Am I doing something wrong?

Thanks in advance,

```auto
import hypermedia.net.*;
int PORT_RX=8234;
String HOST_IP = "192.168.1.2";//IP Address of the PC in which this App is running
UDP udp;//Create UDP object for receiving

void setup(){
  udp= new UDP(this, PORT_RX, HOST_IP);
  udp.log(true);
  udp.listen(true);
  noLoop();
}

void draw(){
 
} 

void receive(byte[] data, String HOST_IP, int PORT_RX){
 
  String value=new String(data);
  println(value);
    
}

```

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [April 21, 2020, 9:16am UTC](https://discourse.processing.org/t/receive-udp-packets/19832/2 "2020-04-21T09:16:57Z")

</div>

If anyone is looking for an answer, I came with the following code to use as UDP server:

```auto
import java.net.*;
import java.io.*;
import java.util.Arrays;

DatagramSocket socket;
DatagramPacket packet;

byte[] buf = new byte[12]; //Set your buffer size as desired

void setup() {
  try {
    socket = new DatagramSocket(5005); // Set your port here
  }
  catch (Exception e) {
    e.printStackTrace(); 
    println(e.getMessage());
  }
}

void draw() {
  try {
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    InetAddress address = packet.getAddress();
    int port = packet.getPort();
    packet = new DatagramPacket(buf, buf.length, address, port);

    //Received as bytes:
    println(Arrays.toString(buf));
    
    //If you wish to receive as String:
    String received = new String(packet.getData(), 0, packet.getLength());
    println(received);
  }
  catch (IOException e) {
    e.printStackTrace(); 
    println(e.getMessage());
  }
}

```

Hope it helps if someone needs

---

<div class="post-metadata">

**Author:** ![Yates](https://avatars.discourse-cdn.com/v4/letter/y/58956e/32.png) [@Yates](https://discourse.processing.org/u/Yates)\
**Post date:** [October 25, 2022, 5:43am UTC](https://discourse.processing.org/t/receive-udp-packets/19832/3 "2022-10-25T05:43:16Z")

</div>

This is great, its the only UDP processing code that works for me. But I am looking for a UDP Client not a Server in Processing. Any chance you could show me a Client code?

---

<div class="post-metadata">

**Author:** ![KLRico](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@KLRico](https://discourse.processing.org/u/KLRico)\
**Post date:** [November 9, 2022, 12:50am UTC](https://discourse.processing.org/t/receive-udp-packets/19832/4 "2022-11-09T00:50:11Z")

</div>

That seems to be a client implementation, but it turns out that you can cut out some of that stuff and it still works as a client.

After looking at the javadoc for [DatagramPacket](https://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html) I noticed that recreating the packet with the address and port is unnecessary if you just want to receive data, so I cut it back to this and it seems to work just fine.

```auto
import java.net.*;
import java.io.*;
import java.util.Arrays;

DatagramSocket socket;
DatagramPacket packet;

byte[] buf = new byte[12]; //Set your buffer size as desired

void setup() {
  try {
    socket = new DatagramSocket(5005); // Set your port here
  }
  catch (Exception e) {
    e.printStackTrace(); 
    println(e.getMessage());
  }
}

void draw() {
  try {
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    
    //Received as bytes:
    println(Arrays.toString(buf));
    
    //If you wish to receive as String:
    String received = new String(packet.getData(), 0, packet.getLength());
    println(received);
  }
  catch (IOException e) {
    e.printStackTrace(); 
    println(e.getMessage());
  }
}

```
