# Sockets / UDP

**URL:** <https://ziggit.dev/t/sockets-udp/4438>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [May 25, 2024, 12:53pm UTC](https://ziggit.dev/t/sockets-udp/4438 "2024-05-25T12:53:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ga-taylorj](https://ziggit.dev/user_avatar/ziggit.dev/ga-taylorj/32/1430_2.png) [@ga-taylorj](https://ziggit.dev/u/ga-taylorj)\
**Post date:** [May 25, 2024, 12:53pm UTC](https://ziggit.dev/t/sockets-udp/4438/1 "2024-05-25T12:53:13Z")

</div>

I’m trying to figure out how to listen for UDP data. I’ve looked through various examples which all seem to suggest os agnostic approaches using the `std.os` library. I presume this has changed since those examples were written, but I’m not sure how I should be doing this now.

The examples I’m seeing do things like:

```zig
const std = @import("std");

pub fn main() !void {
    const addr = try std.net.Address.parseIp("127.0.0.1", 55555);
    const sock = try std.os.socket(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP);
    defer std.os.close(sock);
    try std.os.bind(sock, &addr.any, addr.getOsSockLen());
    var buf: [1024]u8 = undefined;
    const len = try std.os.recv(sock, &buf, 0);
    std.debug.print("{s}\n", .{buf[0..len]});
}

```

As far as I can see, `std.os.socket` doesn’t exist but there are versions for each os i.e. `std.os.linux.socket`

I’ve tried doing something using the linux specific version

```zig
const sock = std.os.linux.socket(std.os.linux.AF.INET, std.os.linux.SOCK.DGRAM, 0);

```

instead of returning a `socket_t` though this returns a `usize` and as such can’t be passed into `std.os.linux.bind` as that requires an `i32`.

I’m sure I’m mixing things up, but not really sure where to go from here. Any help pointing me in the right direction would be really appreciated.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 25, 2024, 1:47pm UTC](https://ziggit.dev/t/sockets-udp/4438/2 "2024-05-25T13:47:48Z")

</div>

The code is using zig 0.11.0, in 0.12.0 `std.os` became `std.posix`.

The following code waits for a UDP message in port 55555, it displays the message and exits. If you want to implement a server, simply loop on `recvfrom`.

```zig
const std = @import("std");

pub fn main() !void {
    const sock = try std.posix.socket(std.posix.AF.INET, std.posix.SOCK.DGRAM, std.posix.IPPROTO.UDP);
    defer std.posix.close(sock);

    const addr = try std.net.Address.parseIp("127.0.0.1", 55555);
    try std.posix.bind(sock, &addr.any, addr.getOsSockLen());

    var buf: [1024]u8 = undefined;
    const len = try std.posix.recvfrom(sock, &buf, 0, null, null);
    std.debug.print("recv: {s}\n", .{buf[0..len]});
}

```

The following code sends a message to upd port 55555:

```zig
echo "Hello World" | nc -u 127.0.0.1 55555

```

---

<div class="post-metadata">

**Author:** ![ga-taylorj](https://ziggit.dev/user_avatar/ziggit.dev/ga-taylorj/32/1430_2.png) [@ga-taylorj](https://ziggit.dev/u/ga-taylorj)\
**Post date:** [May 25, 2024, 1:53pm UTC](https://ziggit.dev/t/sockets-udp/4438/3 "2024-05-25T13:53:56Z")

</div>

Ah fantastic, thanks, exactly what I needed.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 25, 2024, 1:54pm UTC](https://ziggit.dev/t/sockets-udp/4438/4 "2024-05-25T13:54:37Z")

</div>

That is great.  
By the way, welcome back to the forum.

---

<div class="post-metadata">

**Author:** ![seagin](https://ziggit.dev/user_avatar/ziggit.dev/seagin/32/3649_2.png) [@seagin](https://ziggit.dev/u/seagin)\
**Post date:** [February 6, 2025, 1:53am UTC](https://ziggit.dev/t/sockets-udp/4438/5 "2025-02-06T01:53:48Z")

</div>

Hey @dimdim I posted your answer on [stackoverflow](https://stackoverflow.com/questions/79416607/how-to-setup-udp-server-in-zig-v0-12-0-and-after) and mentioned you in it. Just so it has a bit more visibility.

I’ve made it clear that the answer was from you and not me.

If it’s not okay, I can take it down. Do let me know.

---

<div class="post-metadata">

**Author:** ![g41797](https://ziggit.dev/user_avatar/ziggit.dev/g41797/32/3339_2.png) [@g41797](https://ziggit.dev/u/g41797)\
**Post date:** [February 6, 2025, 11:44am UTC](https://ziggit.dev/t/sockets-udp/4438/6 "2025-02-06T11:44:05Z")

</div>

just use [zig-network](https://github.com/ikskuh/zig-network)
