C++ Qt 67 - QTCPServer - a basic TCP server application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everybody's Brian and welcome to the 67th cute tutorial with C++ and GUI programming today we are going to make a server so let's just go cute console application and we'll call this my first server put in the usual location next next finish finish if you wonder why this looks a little bit different I just upgraded to the version 2 point 1 point O of Q Creator and I'm actually liking it alright 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 let's call this my server base class Q object X 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 get around with anything alright 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 guessed so we'll say include cutie bug and include Q TCP server and of course include Q TCP socket now you might be wondering why 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 cute it's a little bit easier they've actually got this Q TCP server class and if you look this up you'll see it's actually a pretty simple class but we have very powerful and it only has one signal that it's going to admit 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 connections available pretty pretty self-explanatory so we're going to turn that into a slot oops 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 alright now we've got our slot now we're going to add our server and we would say queue TCP a 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 queue TCP server so I hadn't closed your header and let me resize the window a little bit here hmm or maybe not there we go get it in my little recording window here okay now we need to implement the slot we just created so 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 queue TCP server give it a parent of this and then we need to you guess to connect our signals and slots I mean this is 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 constructors 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 a 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 cue 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 now whereas a cue 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 cue 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 the 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 to the buffer or should say flushed out to the stream and then plus a socket wait for bytes written 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 no 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 gonna fire up telnet open 127.0.0.1 port one-two-three-four if you're wondering what one two seven zero zero one is that's your local loopback every computer regardless of operating system has what's called a local loopback 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 localhost I like doing one two seven zero zero one just an old-school now when we hit enter two things are going to happen technically three but two things going to happen we're going to get your response from the server and the server is going to close the connection sure enough there we go hello client connection to house lost press any key to continue so this program is 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 close the connection not not a difficult program 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 Q TCP 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 bright line our 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 them you can 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 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 1 2 3 4 or change the number just you know randomly pick like 4 5 6 7 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 ain't couldn't start so I'm Brian 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 I do hope to get a little more complex into TCP server programming the problem is I'm very limited by time so I have to be kind of careful not to go over but that's all for this tutorial I hope you found this educational entertaining and thank you for watching and as always keep up on the feedback I really like hearing from you guys
Info
Channel: VoidRealms
Views: 90,820
Rating: undefined out of 5
Keywords: Qt, 67, QTCPServer, basic, TCP, server, application
Id: BSdKkZNEKlQ
Channel Id: undefined
Length: 9min 42sec (582 seconds)
Published: Thu Apr 14 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.