 Welcome back everyone. This is Brian so in this video. We're going to dive deeper into Python 3 using UDP sockets We've talked about TCP with their three-way handshake. So what is UDP? Well UDP doesn't have concepts like connections and clients and servers think of UDP like as a roomful of people and everyone's just shouting at the same time and you have to kind of Select who you want to pay attention to So UDP is user datagram protocol Notice the first word user that means it's up to the user to determine the protocol There is no three-way handshake or any fancy thing like that So this is a communications protocol that's used for establishing low latency and loss Tolerating communications. Now, what does this mean? Low latency means this is very fast But it's also very dangerous because you can mispack its drop packets You're assuming whatever you're sending out Is a lot like a message in a bottle. You really don't care if it gets there or not You're just sending it out and if somebody grabs it, they grab it So the big thing here is this is used a lot in well lower-level protocols and even embedded devices like think of like a Weather thermometer it may just be sitting there broadcasting out the weather by UDP on your wireless But it doesn't really care if you're listening to the weather or not It's just gonna keep churning it out once a second. So let's dive in and take a look As always the first step is you guessed it import. So we are going to in traditional fashion just copy and paste We're going to use logging multi-processing threading socket Sys and time. I'm not sure if we're gonna use all those but we're gonna figure it out and Logging we're going to go ahead and do a basic config now The main premise here is we're gonna create two processes and we want to Well run one process as a broadcaster and one process as a listener So one of them is going to be speaking and the other one's going to be listening to that But they could both speak if they wanted to remember with UDP. There really are no rules It's up to the user to define what's going on here. All right The bulk of this program is going to be in this little section here where we're gonna actually build up a socket now notice I'm not saying a client or a server or anything like that We're just going to make a socket and this is going to be run on a process and each process is going to treat This a little bit differently. So let's go and say def Make underscore a socket All the IP to be the local host Port and I'm just going to pick a port you could be super fancy and follow the last video where you could actually determine if a port's in use but I'm gonna say port 2045 by default and sender Equals false now senders really what we're going to be working with here because we want one of these to broadcast and one of Them to listen to that broadcast think of it like a radio station and somebody listening to the radio I'm gonna go ahead and say proc equal multi-processing I Want the current process name That way we know what process is actually running this because we're gonna have two processes in each one of them's gonna run This function except for one of them's going to say sender false and one of them's gonna say sender true All right, let's get some screen real estate there Say logging info Go ahead and format that and I want to say whatever the process is is now starting Let's go ahead and get a socket. So I'm gonna say s equal socket dot socket and I want to say the address family is Inet or IP version 4 and this is where you got to kind of pay attention a little bit here because this is how we actually Build e UDP socket. So I'm gonna say socket Dot and instead of sock stream I want sock D gram or datagram Again, if we scroll all the way up here It is a user datagram protocol not a stream of data. So it's just a chunk of data that's flying out on the wire So sock D gram now I'm gonna go ahead and say if We are the sender Logging dot info and I'm just putting this out here. You don't really have to have any of this for to really function I want to know what we're doing in advance. So I'm gonna say F. Whoops F and I want to know the process is starting to send and Let's go ahead and grab this and we're gonna say binding to port So the main thing here is if we are the sender, we're just gonna start sending However, if we want to actually listen to that conversation we need to now bind to that address and Depending on your OS and a whole bunch of other factors. You may have a bad time if you try to do this multiple times We're gonna cover that a little bit more in depth in future videos But I just wanted to throw that out there in case somebody tries to do like 15 different clients on the same bind There might be a different way you have to do it again. We'll cover it And we're gonna say make an address out of the IP and port Go ahead and bind I'm gonna go ahead and bind to that address. Now what bind does here is it really says go out to the network card and Listen on that port for UDP packets. It doesn't really matter what those packets are. I just want to listen to them Now I'm going to say with our socket We want while true and this is why we're doing multiprocessing because we're just going to do a blocking socket And we're just going to do an infinite loop and I want to say if We have the sender Again screen real estate is our buddy here get this Sending just so we can see what's going on out of the hood and I want to send to And there's a whole bunch of different options, but for this one. We're just going to send to you may get super excited and see send file Yeah, UDP is really not the best protocol for doing that. You definitely can but it's really not the best TCP is a better option for sending files We want to say hello UDP and that's really all we're going to send And we're going to go ahead and send that on the IP and port Now I'm going to say time that sleep And we're going to go ahead and sleep for one second Else we're going to say data and the address And we're going to go ahead and receive from and we've covered this in the TCP video very very simple We're just going to say we want an upper limit to how much data we can actually receive in this case 1024 bytes We're not even remotely sending that we're just sitting this right here, but you got to have that out there just to be safe And then let's go ahead and say time dot sleep Actually, don't need time sleep. What was I thinking? There we go logging info and I want the protocol From you definitely could sleep if you wanted to but it would probably not be good from The address there we go, and then we want to know what data we had here All right, lots of typing Any misspellings barred. Let's go ahead and run this make sure we don't have any little gremlins Well, it doesn't look like I really destroyed anything too much But the main takeaway here is We're going to make a socket which is going to run in a process It's going to be on the local host on the default port of 2045 and you can define the ip and port And we're going to be able to determine if we're sending or listening and we're doing that on ip version 4 using a udp socket and then from there we're saying If we are the sender we're going to go ahead and send once a second. Hello udp to that ip and port However, if we're listening we're going to go ahead and receive and this is blocking So it's going to sit there and block the entire time And then once it's received some data and unblocks it's going to say logging info the process from address equals whatever the data was Okay, my favorite part here. Let's see this in action So we're going to take this make socket and put it into a process and then Turn it into a broadcaster and then do the same thing on a listener. So let's go ahead and set up our main function here and let's get some Some area of the screen so we can actually see me type all this out And I want to say broad Up if I could actually spell it broadcaster And I want the multi processing That process We've done this before if you haven't watched the video go back in the playlist and watch the multi processing videos I did We're going to say the target is going to be that make socket function Now we want some keyword args It's not as cool as the word args. I like saying args, but anyways I'm going to say sender is going to be Through And then from here, we're going to say yes, this is a damon which means it's going to shut down when our application shuts down and let's go ahead and set the name to broadcaster Ooh, that is very long. All right. So we're going to grab that do the old coffee and paste action here That way we don't have to retype all that nonsense and say list and Go ahead and down here Don't really need to set this because we have that default But I'm going to set it anyways just to limit any confusion So now we have a broadcaster and a listener Notice how both of these are going to be on the local host on the same port So both of them are using the same port once talking once listening I'm going to go ahead and take our broadcaster. Let's go ahead and start that process Now I'm going to take our listener Now let's go ahead and start that process. So now we've got multiple processes running Now I want our main process or our main thread here to actually just kind of sit in the background And then after five seconds, I want to shut this whole thing down. So I'm going to say time Actually, let's go timer Probably vastly more helpful I'm going to say threading Timer And in five seconds, I want to do a sys Exit And I want to say we are exiting normally that way nobody panics Oh, let's just go ahead and start that Now assuming I typed everything correct, this will just work fine. So let's get some screen there and let's run this Uh-oh, we had a boo boo. Let's see what's going on here Uh-da-da-da-da In make socket. What is going on here? Run soft so file Blah-blah-blah line 40 in socket send to address local variable address the reference before assignment I wonder if anybody watching caught that while I was typing it. So what it's saying here is Scroll through this On line 40 This guy right here address reference before assignment Interesting it's up there. So easy fix. There we go But clear that all right now it's working So you can see we're broadcasters sending the listeners listening and the listeners getting that information And then it shuts everything down after five seconds So minus the little typo that I had up there Everything is working The main concern here is that we're using udp You notice the broadcaster and the listener actually had those backwards the broadcaster and the listener Have no idea the other exists. The broadcaster is just going to send data out doesn't even care if anybody's listening And the listener is going to say hey, is there anybody out there? If so Tell me What am I listening for and that's going to say this is the data So udp. I'm not a big fan of udp. We're not going to Really do a lot of elaborate deep diving. We're going to cover it a little bit more in depth later on But I want you to understand what it is because it is the counterpart to tcp Remember tcp has that Three-way handshake and it guarantees a connection and it guarantees the data sent Otherwise you get a socket error of some kind where udp What we're working with in this video just literally throws it out into the abyss and if you want to listen to it It's up to you to figure out how to get it I hope you enjoyed this video You can find the source code out on github.com if you need additional help Myself and thousands of other developers are hanging out in the void realms facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training I do develop courses out on udp.com. This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on youtube. Thank you for watching