 Hey everybody, this is Brian and welcome to the 65th Qtutorial with C++ and GUI programming. Today we are going to cover the Qtcpsocket and I should say we're actually going to cover part of it. This is a pretty big and complex class and I don't want to make some 30-40 minute tutorial and over-indate 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. 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 a Vista machine, you're going to have to actually install it. You 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. But 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 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 it says the server type and the content length and some basic HTML here. 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 grab. Let's open a cute and let's go to a console and call it socket test, put it in the usual place. Alright. And then the first thing we want to do of course is add networking to our project file. That way cute creator knows that we want to work with the networking module. And then we'll add a new class file here, let's call it socket test, of course we want the base class to be q object and we need to add a few includes here. So we'll say include qtcp socket and include qdebug 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 and I'm going through this rather quickly because you know we've covered this through 60 some odd tutorials so far. Alright. 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. Anyways, so where were we? Oh yes. 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. 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 closed 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 qtcpsocket, we'll call it socket. Now we have an object to work with. Let's go back in here. So we'll say socket equal new, qtcpsocket. And the first thing we want to do of course is make the connection. So we'll say socket and we want connect to 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 know the results from mine. For example if you try to use Google, Google will not respond to banner, grabs 99% of the time it will just not respond at all. So we're 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 or three seconds and say qdebug 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 disconnected, it's closed. I was thinking of totally different language. Very sorry about that. Socket closed. So now we can strike two, get the other one off our list. Let's just save our work and let's just run this. Let's just see if we can just get a connection to this web server here. Not a signal decoration slot. What did I do wrong here? Sorry about that. Phone call really jarred me. I don't understand how cell phone providers can call your cell phone and offer you more packages. It's just annoying. It 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, write, 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 of times. That's probably overkill, but it'll do the job. Basically what carriage return and 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 servers have 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 for bytes written and let's wait one second because we have data and we have to send it halfway across the world of 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. So we're going to say socket, wait for ready read. And let's just wait three seconds for that. Now if you're wondering why I'm using these massive times, I mean on networks three 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 Qtabug 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, bytes available. That way we know how many bytes are coming back from that server. Let me scroll over so you can see that. All right. And then we're just going to do Qtabug 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 buffer. And there is the 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 in the Telnet application. Just we've done it using Qtabug. Now if you're like me and you have a firewall program, your firewall is probably lighting up like a Christmas tree. So I'm going to go over some general debugging tips. If you're trying to connect to the server and it just never, never, never connects, make sure you've got the hostname right and make sure you've got the port right. And when in doubt, let me actually open up a command line here. In case you skip the primer, when in doubt, do an NS lookup, hope if I could spell lookup, 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, in my case voidrumbs.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 are just very picky. Like let's actually try Google. I tried this with Telnet and nothing happened, 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 was 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 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 rating socket bytes zero, 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 it's a homegrown server, check your firewall, check your virus scan because yes, virus programs may treat this as a virus because you're trying to open a network connection. And make sure that your server you're trying to talk to doesn't have a firewall that's blocking your request. So I know that's a lot of things to check, but networking is actually pretty complex animal. That's it for this tutorial. Hopefully there'll be more. I am still studying for my test, so my time's pretty limited and I got to whip these out pretty quick. I am looking forward to any request 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 you. Anyways, this is Brian. Thank you for watching. I hope you found this video educational and entertaining.