 Hey everybody, this is Brian and welcome to the 71st Qt tutorial with C++ and GUI programming. Today we're going to be covering UDP sockets. So we're going to say my UDP test and just throw this in the usual location wherever you're putting your projects. Next next finish finish. And I have to apologize if you hear some noise in the background. My kitten seems to think that my blinds are new toys so as much as I try to keep my way from him he just really really wants to play with him. Alright let's save that. Now if you've been watching the TCP tutorials you should know that this is going to be much much easier. First off what is UDP? Well UDP is the exact opposite of TCP. Let me get this class built here having some technical difficulties thank you kitten. Alright so let's actually include it, QDP socket, and then let's look this up and help just so we can really quickly see what we're working with here. The Q UDP class reference. If you read the class description it says that the UDP or user datagram protocol is a lightweight unreliable datagram oriented connectionless protocol. What in the world does all that mean? Well it means you send very small very fast packets. It's unreliable because there's no three-way TCP handshake. So you quite literally send your message out into the void that you call your network and you have no idea if the end user ever got it. Well that's kind of silly why would you use something like that? Well UDP is incredibly fast it's much faster than TCP simply because it describes you need small packets and there's no three-way handshake overhead so you can just whip these out. Usually with UDP you'll do things like multicast and broadcast where you'll send a packet out into the network and you'll send it to broadcast address and it'll just go everywhere or you'll use it to do things like ping. We've pinged servers before that's actually a UDP packet. So we are going to do a very kind of slim down client server all in one thingy just to kind of show you how this class works. This is probably going to be the first and last video we're going to cover with UDP simply because it's not really used that much. I mean it is used but most everything out there is TCP and then whoops that's a slot not a signal what am I thinking here. Ready read should look familiar for you if you've been following the TCP tutorials and then for our private we're just going to make a private variable here called QUDP socket and we'll call it socket here. So we have to implement really two things here say hello and ready read so let's grab that save that little copy and paste magic here uh-oh I can hear in the background my other cat sees that I'm on the computer and now suddenly wants my attention hey kitty hold on guys I'm sorry I have to pause the video gotta pet the kitten okay sorry about that for some reason I am just invisible to this cat until I start working on something and then oh my gosh she wants my attention so we'll say socket equal new QUDP socket we'll give the parent of this and this next part is going to take a little bit of explanation if I can spell socket correctly what we need to do with the UDP socket is actually bind it to an address in a port remember you can have multiple addresses in multiple ports so what you want to do is bind it to a specific address in a specific port so we'll say Q host address and we're just going to say local host local host is the equivalent of 127001 and we're going to do port 1234 now we need to connect our signals and slots I'll say socket signal ready read and our slots going to be this ready read very simple very easy just doing that when we create the class and then we want a function here where we can quite literally just spit out some data so we're going to say Q byte array and this would be your datagram we're just going to call it data and then data append and we're just going to say hello from UDP land maybe if I could spell from my spellings atrocious in these videos I'm sorry about that now what we want to do is actually write the datagram out to the socket so we'll say socket write datagram and notice how we've got a couple properties we have to fill in here we have to say the datagram and then you have to say where you're sending it to because remember you don't have a connection so you can send this to just about anywhere so we'll say Q host address local host you know our local loop back port 1234 and that's all you need to really send data now you should know if you want to broadcast something you could really say like we'll say my IP address 192.168.1.10 you could do what's called a broadcast address 192.168.1.255 that will send it to pretty much everything out on your local subnet if you know anything about subnetting so before I get some some emails about how do you broadcast that's really how you do it you just send it to your broadcast address which is in 255 all right now that we're sending something we need to know when that data comes in we've got our signals and slots hooked up so when data comes in ready reads gonna get fired off here so we got to say Q byte array and we'll just call this buffer and we need to resize this depending on what the size of the pending datagram is that way we don't lose information so we'll say buffer resize and you notice how now it wants a size when we can just say socket pending datagram size and that's how you get the size of the datagram sitting in the socket waiting to be read now we just need a couple variables here we'll say Q host address and we're gonna say sender and we want of course a Q unsigned int 16 oops there we go and this is going to be the sender port now why do we need those well when we do the next part the read datagram part of what it does is says who is it from and where did it come from you'll see what I mean here in a second so socket read datagram and now we need the buffer data so it'll fill those bytes and then it wants to know how big can you fill this will say buffer size and now it wants a variable to shove the host address into so we're just gonna say sender and we want to know the port so we'll say sender port that way we can collect all this information right there so we're gonna just you know of course do a Q debug and we're gonna say message from and let's do this and we're gonna say sender we want to know the address and let's just do a little copy and paste magic here just to make life a little easier on us and we'll say message port we want to know what port this thing came on once again I'm the victim of my gaming mouse I love this mouse when I'm playing video games but boy does it make typing a pain sometimes and we want to know the actual message that came in so we're just gonna throw the buffer back out here just so we can see what's going on very very simple class when we call a constructor we're just making a new socket binding it to the address and port and connecting our signals and slots we have a function that sends out information hello from UDP land and we're writing that datagram out to the socket we have ready read this is our slot that is called when and there is a pending datagram in the socket and then we can just you know resize our buffer shove that data into the buffer and get the the address it came from and the port it came from so now in our main dot CPP you say include my UDP and we're gonna make a client server here so we're just gonna say my UDP we're gonna call a server and my UDP and we'll call this client and we're gonna say client say hello that's really all there is to it let's see if we can get a build and compile out of this no matching blah blah what did I do wrong here well let me pause the video super quick and figure this one out yes silly me it wants the address of these sorry about that there we go now we should get a clean compile out of this there we go now as you can see it says message from 127001 that's our local loopback message port whoa it's a big number where did that come from well that is the port that the client is actually sending on remember we bound and listen to a specific port doesn't necessarily mean that's the port you're gonna send on though hello from UDP land is our message so that's our datagram in its entirety so as you can see there's really not a whole lot to UDP it's pretty pretty simple protocol here you really just need to bind to a address in a port listen for a datagram and then process it when it comes in so it's really all there is so this is Brian thank you for watching I hope you found this tutorial educational and entertaining and if you got any questions comments concerns go ahead and send them to me