 Hey everybody, it's Brian, and welcome to the 66th Qt tutorial with C++ and GUI programming. Sorry it's taken me so long to get a video, life's been kind of crazy. My cat died, I got a new cat, and I just passed the Certified Ethical Hacker exam. So, today we're going to continue right where we left off, and we're going to call this SignalSocket. Sorry, put it in the usual place. And we're going to continue with our socket tutorials. And if you remember from the last tutorial, let's see if I remember, we did TCP socket basics, and we did it procedurally, meaning it's one line after another. And what we're going to do today is object-oriented. We're going to use the Qt SignalSlot methods. So let's just see if I can add new. Sorry, I've been a while since I made one of these. And we'll call this socket test. And our base class will be a Q object. Alright. Now, first thing we want to do, of course, is go into our project file. And we want to add the networking module. And then it'll rescan our project here. And then in the socket test header, we need to add a few includes. So what we're going to do is we're going to include and we want to include, of course, QDebug. So we can see what we're doing here. And this is going to be kind of a larger tutorial. So I'm just going to do some copy and paste magic here. I'm going to include QTCPSocket. And we're also going to include the... Oops, I clicked the wrong button there. Sorry about that. The Q abstract socket, if I can find it here. And I'll show you why we're going to do that here in a minute. The next thing we really want to do here is we want to have an instance of a socket. So we're going to say private. And we'll say QTCPSocket. There we go. And then let's just add a function here. We'll call this test. Now, you notice how I said we're going to add Q abstract socket. And I'd explain that. Well, if you go to help and then you do QTCPSocket, you'll see there really isn't a whole lot to this. But rather it inherits a bunch of stuff. You go back to our conversations on inheritance. So what we need to do is really look at the signals emitted from Q abstract socket. And there's quite a few of them. And you can kind of browse and you can see connected, disconnected, et cetera, et cetera. Those are the ones we're going to focus on. Selects actually just copy these things. And then for our signals, we're just going to paste them in here. That's one thing I absolutely love about the Q documentation is you quite literally just copy and paste most of the code right out of the documentation. The other thing we want to know when there's bytes available and when to read. And so it inherits a QIO device. We also want to know the signals ready read and bytes written. So let's just copy this. This saves our little fingers a little bit of typing here. Not that it really matters. And you notice that is a Q int 64 bytes. Basically that says the number of bytes that have been written. And then ready read. And if you remember from our last tutorial ready read tells us when there's actually information for us to read. Now we want to start implementing these things. So let's go in here and we will go socket. And I'm just going to do this really, really quickly here. I find that in development, it's always good to do things right the first time. But sometimes quicker is really better, especially when you're on the clock here. And we're just going to copy our namespace. I'm just going to set these up very, very quickly. That way we have all of our slots in here. And I actually did put those in the wrong spot. I'm very sorry about that. It's been a long day. It was first day back from a six day vacation that was much overdue. So my brain's just kind of scattered brain here. So before you send me hate mail, yes, they're not signals, they're slots. All right. Now we're just going to go into our main and don't worry. I'm going to review this before we really start playing around with it too much. We'll say socket test. And we want to socket test. We'll say mtest. And then mtest. And we want to call our infamous test function. That's where all of our functionality is going to be at. And go ahead and close that. And now that we've got our header squared away, we can go ahead and close this guy. And we're just going to worry about the implementation here. So now that we've got our implementation, what we need to do here is let's actually focus on test. We need to do a few things. First, we need to actually connect to something. Then we need to understand that we connected and know when data is available, when we've written data and when we're disconnected. So it all starts with, you guessed it, and planning our socket, QTCP socket. We're just going to say this is the parent. And then we need to do what I call effectively plumbing code. We're going to just connect the signals and slots. And the first one we're going to do is we're just going to do connected. And I apologize the limited screen real estate here. It might be a little bothersome for some of you out there, but it really keeps the video sizes small. Actually let me see if I can hide the sidebar there. And then we're just going to do disconnected. And there's one more, actually two more. We want ready read. We want to know when there's information ready to be read. And then we've got another one, which is bytes written. And bytes written, let's just type this one out. So you can see, in case you're really not familiar with signals and slots, but if you've been following along with these tutorials, you should be fairly well versed with them. Signal and then bytes written. And once again, if you're wondering how I'm getting this to pop up the intelligence so readily, it's just a control shift brings it up. All right. So now we've got our, what I effectively call plumbing code out of the way. We've got our sockets created and we've connected the signals and slots. Now we need to actually implement the functionality here. So what we're going to do first is say queue to bug and let's just say connecting. That way we know we're actually attempting a connection here. And then we're going to actually say socket. And if you remember from our last tutorial, let's connect a host. And you can give this a queue address. You can just give it a queue string. We're just going to say queue string. I'm going to say my website void realms.com. We'll help if I could spell it right. And of course we just want port 80 because we're just going to simply send it to the web server. Now, the first thing we need to understand is that connect to host is non-blocking, meaning it's not going to wait for that to trigger. It's not going to sit at this function and wait for the connection. It's just going to immediately return back to your code. So we're going to actually wait for it and see if it's connected. So we're going to say if not socket, wait for connected. And if you just leave that blank, it's just going to wait indefinitely. But we're going to wait one second. And we're going to say, so if this did not connect, then we want to know something bad happened. We're just going to say error. And we can actually socket. And we'll just print out the error string here. That way we can tell exactly what happened. Now, from this point forward, we are using these signals and slots. So what we can do is just do a little copy and paste magic here. We can say connected. We can say disconnected. Now, bytes written, this is called, or should say it's emitted from the QTCP socket, when we actually write bytes out on the line. It doesn't really help us much other than the fact that we know that we wrote something out. So we can just say we wrote. And we just want to put the number of bytes here. Now, ready read. This is where the action is going to happen. This is where we're going to say, OK, we are ready to read data off of the socket. So we're just going to say, reading dot, dot, dot. And then we're just going to read the data right off the socket. We'll say socket. Read all. OK. Now, let's go ahead and give this a quick build. Make sure there's no bugs in this. Looks like I've got something wrong. In function socket test, no action call for blah. What did I do here? No? Guess that's not it. Tell you what, let me pause the video real quick and I'll figure it out. OK, I'm back. I must have fat fingered something. Sorry about that. Like I said, it's been a long day. It's been a while, admittedly, since I've done a video. Basically, I fat fingered this connect statement. So if you had the same error, go back and fix it real quick. All right. So let's just rebuild. Make sure, yes, we get a good build. And so let's just go ahead and run this to see what happens. Connecting. Connected. You notice how we don't disconnect? We don't get any ready read. Well, why is that? Well, this is where we're going to go back and we're going to review here. OK. Basic HTTP protocol 101. We connect to the server, so we're getting connected. But then it just sits there and does nothing. So what we need to do is say, OK, we're connected. Now we need to send a request. So we're going to say socket. And we're just going to write. And we're just going to write the head command. Now what the head command does is it says, hey, tell me about you, Mr. Server. Whoops. So very simply, all we're going to do is basic banner grab, similar to our last tutorial. But we're doing this through signals and slots. So when it's connected, it's going to emit the connected. And we're going to consume that in our slot connected. And we're going to say connected. And then we're going to write out onto the wire. Now, when we write, it's going to emit the bytes written signal. We're going to consume that and say we wrote x number of bytes, or should say bytes number of bytes. And then when the server responds, it'll emit the ready read. And we can actually read all and just dump that out onto the screen. So let's go ahead and fire this back up. And with any luck, nothing will explode. And there we go. Connecting. We connected. We wrote 21 bytes. We're reading. And then this is the, let me actually show you. This is the information right here that we got back from the web server. And then you notice disconnected at the very end. So that is in a nutshell, the basics of how to use the QTCP socket using the signal slots method. I had numerous, numerous requests for this. So I wanted to really cover this and kind of pick it apart. Let me show my window again. If you really didn't understand what was going on, let's take a look at the header here and get all the gobbly cook out of the way. What you need to understand is there's four basic things that happen here. We connect. We get disconnected. We write bytes and ready read tells us there's information for us to read. There's a buffer in the background that you don't directly manipulate. And that buffer fills with data or we put data in it but off to the server in Happyland. So that's really what's going on under the hood. That's all you need to understand is that you can connect. You can disconnect. You can read and you can write. And that's it. So I know this is a very simple tutorial but it really gets you started on what you can do with some of these things. I mean, for example, on the ready read you could actually parse out the information and then based on that make another request and do specific things like that. Now, some things you should know. Let's go up in our test function here. Notice this if not socket wait for connected. If you just admit that it's just going to sit there forever and ever. So if you're not connecting it's just going to wait indefinitely and your program will hang. Likewise, if you have two short of a time like one millisecond you may actually error out. Let's test this just to see if we can get a one millisecond connection. I don't think we will. Yeah, see, here we go. Socket operation timed out. One millisecond just isn't long enough. So when in doubt, go with a second. If you have a slow connection you can go for like five seconds or 15. Once you're connected it'll actually just jump right out here. It won't wait the full five seconds. So you can put a very large number in there if you want to. Another thing you should note is that if you're having problems connecting to a server and you cannot for the life you figure out why, like if you get error unknown check your firewall, check your virus scan or make sure you're connecting to the right port and the right host name and actually use the IP address if you're not getting a good DNS resolution. So this is Brian. Thank you for watching. I hope you found this tutorial educational and entertaining and really keep up the feedback. I've been getting a lot of really good feedback and I've been getting some donations. I really thank you guys. I'm not really asking for donations but I'm very humbled that you're giving them to me. And believe it or not I'm actually taking all the donation money and plopping them right back into these videos. So I make enough money that I'm fat and happy. I'm not really fat but I'm living comfortably. So I do appreciate your donations and I want you to know that they are going back into the videos, not stuffing my face full of pizza and beer which actually sounds pretty good. I think I'm going to go do that right now. All right. Talk to you later.