How to Send and Receive UDP packets (in C)
HTML-код
- Опубликовано: 7 фев 2025
- Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.th...
Website ➤ www.jacobsorbe...
---
How to Send and Receive UDP packets (in C) // Most socket videos use stream sockets. In this one, I decided to look at how we send and receive UDP datagrams for those projects when you need a bit more control over how things flow through the network.
Related Videos:
Two types of socket: • The two main types of ...
My other network programming videos: • Network Programming
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorbe...
people.cs.clem...
persist.cs.clem...
To Support the Channel:
like, subscribe, spread the word
contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtub...]
Nice vid! A raw sockets video would be super!
Also, it would be awesome to do a deep dive into the network stack, perhaps sk_buff?
Legend, literally posted this the day my networking course starts and there's an assignment with UDP lol
nice video bro, your channel is gold!!
Very nice repeating so keep formula solid in your mind with time .
Testing can also be done with socat or nc, both sending and receiving.
Would love a video about unix and raw sockets.
I think it is useful to include continuous receiving packets in the tutorial. Because it is used more often than receiving a single packet.
Super, is TCP coming up?
I have a few stream socket examples that use TCP. Or are you hoping for a deep dive into the details of TCP?
@@JacobSorber TCP demystified
What version of C are you using?
That not equal sign on line 10 at 3:51 is unusual.
Cool video, but I've been always curious about the different ways to handle data coming through the network when it is larger than the defined the buffer. I do have an idea about maybe using fgets whith stdin for example to handle undefined data size but idk if it's the same case with data coming through a network socket.
Okay, so since connect isn't really a thing with UDP, how do you handle multiple connections without using threads/forks? Or are those the only way?
Well, if you are using UDP, then there are no connections, at least not provided by the underlying protocol. You could set up your own connection abstraction between two end-points that you manage in your code. And, in that case, yes you could handle multiple concurrent connections like you do with TCP, using threads or processes.
Thanks for your videos. My current main project is a network simulator so I'm generally interested in socket examples.
I'm wondering what happened to the datagram sent around 11:10? It wasn't received by the receiver program.
I was wondering the same thing. Perhaps they were on different instances of virtual machines?
That's the beauty of UDP, it's "fire-and-forget". So you can just blast UDP pakets into the void, if theres no one listening, the message is just discarded by any connected node. Even though, as a sender you'll never know if someone or anyone has received the UDP paket.
While TCP is doing a lot of handshakes such like regular heartbeats to check if the peer is still alive and connected, an acknowledge for each sent packet, packet resend features etc. which cause some overhead but UDP does none of those things on UDP-Protocol level.
Many Applications that use UDP do have some counter included in their procotol so the receiving application can notice if there was a packet dropped.
@Gr4cer Ah! If there is no "active" listener bound to the port, the datagram is just discarded.
@@CosmicCoder Thank you very much for your response
@@Gr4cer Thanks for your response and excellent explanation.
Need socket timeout also other method to remove blocking functionality
I seem to remember reading somewhere that connect can still be called for datagram sockets and that it has the effect of setting the default address for sending on that socket. Is that true?
It is, indeed. I personally don't love using connect with UDP sockets, because it seems (at least to me) to imply a connection (like you have with TCP) where there isn't one. Could confuse some people reading the code, but yes, it does work that way.
++(wire shark)
The check in this example needs to close the socket.
when http on stm32