C++ Qt 66 - QTCPSocket using signals and slots

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everybody's Brian and welcome to the 66 cute tutorial but C++ and GUI programming sorry it's taking me so long to get a video and 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 signal socks signal socket sorry but 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 acute signal slot methods so let's just see if I can add new sorry I'm kind of been a while since I've made one of these and we'll call this socket test and our base class will be a cue object all right 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 every scanner 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 Q debug so we can see what we're doing here and this can be kind of a larger tutorial so I'm just going to do some copy and paste magic here I'm going to include Q TCP socket and we're also going to include the oops click the wrong button there sorry about that EQ q abstracts 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 whereas a private and we'll say Q TCP socket there we go then let's just let's just add a function here we'll call this test now you notice how I said going to add Q abstract socket and I'd explained that well if you go to help and then you do Q TCP socket 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 you can see connected disconnected etc etc those are the ones we're going to focus on so let's 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 cute 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 Q i/o device we also want to know the signals ready read and bytes written so let's just copy this this saves our our little fingers a little bit of typing here not that it really matters I notice that is 8qn 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 all right now we want to start implementing these things so let's go in here and we will go sock it 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 your namespace I'm just going to set these up very very quickly that way we have all of our 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 brains just kind of scatterbrained 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 I'll say socket test and we want to socket test we'll say em test and em test 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 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 implanting our socket TCP socket we're just gonna 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 might be a little a little bothersome for some of your out there but it really keeps the video sizes small actually let me see I think yes I 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 byte written let's just type this one out so you 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 lights written and once again if you're wondering how I'm getting this to pop up the intellisense so readily it's just a patrol shift brings it up alright so now we've got our LR 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 cute a 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 it's connect a host and you can give this a cue address you can just give it a cue string we're just going to say key string I'm going to say my website Boyd realms com help if I could spell it right and of course we just want port 80 because we're just going to simply connect to the web server now the first thing we need to understand is that connect the host is non-blocking meaning it's not going to wait for that to trigger it's not going to sit at this function to 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 sock it 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 gonna say error and we can actually suck it then 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 through a little copy and paste magic here we can say connected and we could say disconnected now bytes written this is called or should say it's emitted from the queue TCP socket when we actually write bytes out on the line doesn't really help us much other the fact that we know that a we wrote something out so we can just say we wrote and we just will 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 okay 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 let's say socket read all okay now let's go ahead and give this a quick bill make sure there's no bugs in this it looks like I've got something wrong in function socket test and I'm action call for blah what did I do here ah no guess that's not it tell you what let me pause the video real quick and I'll figure it out okay I'm back I must have fat-fingered something sorry about that like I said spent a long day it's been a while may the police since I've done a video basically I fat-fingered this connect statement so feel the same Eric 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 see what happens this is connecting connected you notice how we don't disconnect we don't get anybody read well why is that well this is where we're going to go back and we're going to review here okay basic HTTP protocol 101 we connect to the server so we're getting connected but then it just sits there does nothing so what we need to do is say okay we're connected now we see 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 mister server oops so very simply all we're going to do is basic banner grabs 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 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 on to 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 I 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 out of 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 webserver and then you notice disconnected at the very end so that is in a nutshell the basics of how to use the cue TCP 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 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 and ship it off to the server and 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 can actually you know 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 you notice this if not socket wait for connected if you just omit that it's just going to sit there forever and ever so if you're not connecting is just going to wait indefinitely and your program will hang likewise if you have too short of a time like one millisecond you may actually error out let's let's test this just to see if we get a 1 millisecond connection I don't think we will ya see here we go soccer 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 5 seconds or 15 once you're connected it'll actually just jump right out here won't wait the full 5 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 hostname and actually use the IP address if you're not getting a good dns resolution so this is Brian I thank you for watching I hope you found this tutorial educational and entertaining and really keep up the feedback I'm 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 any 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 you know fat and happy I'm not really fat but I'm 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 you know stuffing my face full of pizza and beer which actually sounds pretty good I think I'm going to go do that right now alright talk to you later
Info
Channel: VoidRealms
Views: 50,821
Rating: undefined out of 5
Keywords: Qt, 65, QTCPSocket, using, signals, and, slots
Id: j9uAfTAZrdM
Channel Id: undefined
Length: 16min 33sec (993 seconds)
Published: Wed Apr 13 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.