C++ Qt 65 - QTcpSocket basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Brian and welcome to the sixty-fifth cute tutorial with C++ and GUI programming today we are going to cover the Q TCP socket there I should say we're actually going to cover part of it this is a pretty big big and complex class and I don't want to make some you know 30 40 minute tutorial and over inundate you with data so we're just going to take this in small easy to digest chunks so today we're going to cover the basics and I've got a command prompt and I'm going to show you how to use telnet if you've never used it and you're on a Windows 7 or Vista machine you're gonna have to actually install it go to control panel programs turn Windows Features on or off and then make sure telnet client is checked if you're on XP or Linux that's probably already installed anyways just type oops telnet notice how the command prompt changes the syntax is open and then just give it a web server name and then the port and notice how nothing really seems to happen we are connected if we hit enter a few times we get this and that's what the security experts and hackers call a banner grab really what it does is it just sends an erroneous request to the web server the web server responds with a 400 bad request and basically you know it says the server type and the content length and some basic HTML here really what I'm just showing you here is that you can send and receive data very simply across the internet so this is essentially what we're going to do today is do a simple banner graph let's open the cute and go to a console we'll call it socket test put in the usual place all right and the first thing we want to do of course is add networking to our project file so that way cute creator knows that we want to work with the networking module then we'll add a new class file here let's socket test of course we want the base class to be queue object and we need to add a few includes here so we'll say include queue TCP socket and include cutie bug and we're going to make a very simple function here and we're just going to call this connect now before we forget let's actually just include our nifty little class here I'm going through this rather quickly because you know we covered this through mom 60 some-odd tutorials so far all right and of course there's my phone let me pause the video and get right back to you sorry about that guys salespeople they always call it the worst times anyway so we're worried ways okay so we've got our test class we're going to call connect now connect doesn't actually do anything yet because we haven't implemented so let's go ahead and implement that go in here and say void socket test connect all right now we've got our connect method now what you saw me do with the telnet prompt is we did four things effectively we connected we sent data got data and then close the connection so we need to do those four simple things and we're going to do them hopefully very simply is an easy to understand manner first thing we need to do though is actually make a socket so we'll say Q TCP socket call a socket now we have an object to work with let's go back in here so say socket equal new queue TCP socket and the first thing we want to do of course is make the connection so we'll say socket and we want connect the host and you just give it a name and I'm going to use my web server your mileage may vary with other web servers I just I know the results for mine for example if you try to use Google Google will not respond the banner grabs 99 percent of the time it will just not respond at all all right so we are connected so we think here's the tricky thing with socket programming you have to test every little statement because you don't really know what's going to happen so we have to say if socket wait for connected and give it a number of milliseconds we'll wait you know 3,000 milliseconds over 3 seconds and say cue debug and connected now that's if something went really good what if something goes really wrong well then we didn't connect and then we need to handle that appropriately not connected so strike one we got that off our list we made a connection now if we're connected let's just say socket disconnect pretty simple to do actually sorry it's not disconnect it's closed I was thinking of totally different language very sorry about that socket closed so now we can strike to get the other one off our list let's just save our work and let's just run this oh let's just see if we can just get a connection to this web server here yep not a signal declaration of slot what did I do wrong here yeah yes sorry about that phone call really jarred me I don't understand how self problem cell phone providers can call your cell phone and offer you more packages it's just annoying just kind of bugs me because I've just paid for that call anyways so connected we've actually connected to the web server we didn't really do anything so now we want to send data and get the response so let's actually just do send and what we'll say is socket right treat it just like a file and we'll just say hello server and we're going to give it a carriage return and a line feed we're going to do that a couple times and that's probably overkill but that'll do the job basically what carriage return a line feed do it's part of the HTTP protocol where it looks for a request and the request ends in two carriage returns and line feeds and we throw the extra one in there just in case the server's having a grumpy day now last but most certainly not least we need to get the data but like I said we have to treat this as if we don't really know what's going to happen so we'll say socket wait four bytes written and let's wait one second because we have data and we have to send it halfway across the world to the web server so that's going to take time so we're going to wait for that to happen now this is a very unprofessional program I'll admit that right now I'm just showing you the basics and the theory behind all this and why you do things now that we've sent the data we need to wait for the response we're going to say socket wait for ready read and let's just wait 30 seconds for that now if you're wondering why I'm using these massive times I mean on networks 3 seconds is an eternity I'm just showing you that there is a little bit of lag time between sending and receiving and then we'll say cue debug reading that way we know we're actually reading the response from the server here and we want to know how many bytes are available so let's just say actually let's just do this socket lights available that way we know how many flights are coming back from that server let me scroll over so you can see that alright and then we're just going to do a cutie bug and we're going to say socket read all so rather than doing anything fancy and doing any parsing we're just going to read the entire the entire buffer and there is last one off our list now let's save and run our work and with any luck we'll get a banner back and sure enough connecting reading 224 bytes and here is the response from the server so we've effectively done the same thing that we did the telnet application just we've done it using cute now if you're like me and you have a firewall program your file all is probably lighting up like a Christmas tree so I'm going to go over some general some general debugging tips if you're trying to connect to the server and it just never never never connects make sure you got the host name right and make sure you've got the port right and when in doubt woopsy me actually open up a command line here in case you skip the primer when in doubt do an NS look up I'm perfected spelt look up on the server you're trying to connect to and this authoritative answer right there let's actually mark that that's the IP address so if you're having trouble getting it through the name my case void rom-com try doing it through the raw IP address and it does the same thing it just skips the DNS resolution let's see here if you send data and never get a response back you have to remember some servers or just very picky like let's actually try Google I tried this with telnet nothing to happen but let's try it with our little program and see what happens here well I stand corrected Google did actually return a response this time when I tried this in telnet absolutely nothing happened I mean it's just a blank screen so I guess if you use this program you can do a banner grab on Google but you notice how the the server type is ominously missing and we got a GFE 2.0 but it doesn't really tell us much details about it not that you need to know any of that it's just kind of interesting to see that trying to think of anything else here before I let you go oh yes if you do a wait for ready read and then you get reading socket bytes 0 that means there's literally nothing there the server responded with nothing it didn't do a darn thing in that case just for this tutorial I try a different server if you'd a homegrown server check your firewall check your virus scan because yes virus programs may treat this is a virus because you're trying to open a network connection and make sure that your server you're trying to talk to you doesn't have a firewall that's blocking your requests so I know that's a lot a lot of things to check but the networking is actually a pretty complex animal that's it for this tutorial hopefully there'll be more I am still studying for my test so many times pretty limited and I'm going to whip these out pretty quick so I am looking forward to any requests you guys have for videos because after I'm done with this test I am done with certifications for a while so send me your ideas I look forward to reading anyways this Brian thank you for watching I hope you found this video entertain
Info
Channel: VoidRealms
Views: 87,796
Rating: undefined out of 5
Keywords: Qt, 65, QTcpSocket, basics
Id: u5OdR46542M
Channel Id: undefined
Length: 12min 53sec (773 seconds)
Published: Mon Mar 28 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.