 Hey everybody it's Brian and welcome to the 67th Qtutorial with C++ and GUI programming. Today we are going to make a server. So let's just go Qt console application and we'll call this my first server. Put it in the usual location. Next, next, finish, finish. If you're wondering why this looks a little bit different I just upgraded to the version 2.1.0 of Qt creator and I'm actually liking it. All right now down to business. First thing you need to do is add the network module and this should just be you know old hat. You should know how to do all this stuff by now. If not watch some of my previous tutorials and we're just going to add a class and we're going to call this server. Actually just call this my server base class Q object. Next, next. And what we're going to do here is just very simple. We're going to include my server and we're going to just trigger everything off in the constructor that way we don't have to dittle around with anything. All right so then you can go ahead and just close that. Now open up the header for my server and we have to add a few things as you probably guess. So we'll say include Qtabug and include Qtcp server and of course include Qtcp socket. Now you might be wondering why are we adding both? I mean most programming languages you use a socket you listen and then you manage it from there. Well with Qt it's a little bit easier. They've actually got this Qtcp server class and if you look this up you'll see it's actually a pretty simple class but very powerful and it only has one signal that it's going to emit. That's the new connection signal and what new connection does if you just click on it tells you this signal is emitted every time a new connection is available. Pretty self-explanatory. So we're going to turn that into a slot. Whoops I already have it out there. Sorry about that. It's been an incredibly long day so let's see how many mistakes I can make in this tutorial. All right now we've got our slot now we're going to add our server and we're going to say Qtcp server and we're just going to call this very simply server and that's really all that is to our class. We've added our references. We've added a slot that we're going to implement and we've made our Qtcp server so I can close your header and let me resize the window a little bit here or maybe not get it in my little recording window here. Okay now we need to implement the slot we just created so we'll say my server new connection and now that we have that done the first thing we really need to do for the guts of this class is we of course need to create our server Qtcp server give it a parent of this and then we need to you guessed it connect our signals and slots I mean this you guys are pretty smart you know all this stuff already you're just kind of going along for the ride here and we want to connect the new connection you know when the client connects to the server and we're going to connect it to our slot new connection that way we know when a client connects to the server. Now the last piece of business we really need to do in the constructor is actually start the server so we say if not server dot listen and if you're wondering why do you listen I thought we're going to connect some things well server listens for connections that's what a server does it sits there and listens on a specific port and a client connects to it and then it performs a specific action. Now the tricky part is knowing how to listen you see how it says Q host address and a reference to address well you can give it a specific IP address remember how your computer it can have multiple IP addresses well we're going to take the easy way out with this tutorial and we're going to say Q host address and we're going to say any so we're going to listen on any available IP address and we're going to listen on port one two three four and we're going to say else just in case and we want to Q debug whoops let's get that out of there I'm having all sorts of trouble today there we go server could not start so that's the absolute worst case that could happen at that point as a server just doesn't start and what we're really looking for is this guy down here which is server started now that we have a server that's listening we need to handle connections as they come in and that of course is where the new connection slot comes in and remember we connected that up here to the server new connection signal so whenever that signal is emitted we're just going to jump right down here and what we need to do is we need to grab that socket from the list of pending connections so we're going to say Q TCP socket and we're just going to call this socket equal server next pending connection very simple we're just going to say hey server what's your next pending connection and that's the socket we want to talk to now we are communicating with this client so we'll say socket right and we're just going to say hello client give it a carriage return line feed and then we're going to of course socket flush all the data out to the buffer or should say flush it out to the stream and then we're going to say socket wait for bytes written and we're only going to wait three seconds and then of course we're just going to close the connection because we're done with them we don't need them anymore all right now if everything goes as planned this thing should not explode let's try it find out what happens run this building building building there we go and success we have server started so i have a command prompt here and we're just going to fire up telnet open 127.0.0.1 port 1234 if you're wondering what 127001 is that's your local loop back every computer regardless of operating system has what's called a local loop back so if you want to talk to your own computer you don't need to know your own ip address just do 127.0.0.1 or you could type the word local host i like doing 127001 just some old school now when we hit enter two things are going to happen technically three but two things are going to happen we're going to get a response from the server and the server is going to close the connection sure enough there we go hello client connection to host lost press any key to continue so this program's done exactly what we wanted it to do we have a server that's listening we connected to it the server wrote data and then closed the connection not uh not a difficult program um not a professional program though but it's just to show you the concept of how this works really really quick review here we're creating an instance of my server in the my server constructor we're creating a new qtcp server setting up our signals and slots there's only one signal you got to worry about and that's the new connection so whenever there's a new connection you want to get the next pending connection and you get you get a pointer to the socket and then you simply write line or write information out flush the information to make sure it actually wrote wait for the bytes to be written and then close the socket and as you guys you can do pretty much whatever you want i mean grab a file off your hard drive and write it out to the buffer you can you know read information from the client that they're sending you you're only limited by your imagination and uh some real quick troubleshooting tips if you do server listen and it just says server could not start check your port make sure you have nothing else running on port one two three four or change the number just you know randomly pick like four five six seven i don't know try that i mean for example if you try port 80 and you already have a web server running it's going to come up and say eight couldn't start so i'm ryan it's been a very long day and i'm pretty well spent but i really wanted to get this tutorial out because i've had a ton of requests for this and uh i do hope to get a little more complex into a tcp server programming um the problem is i'm very limited by time so i have to be kind of careful not to go over but uh that's all for this tutorial i hope you found this educational and entertaining and thank you for watching and as always keep up on the feedback i really like hearing from you guys