Hey everybody this is Brian and welcome to C++ tutorial. I don't remember what we are, 133 sorry I had to check the previous one.  We are going to continue our TCP server done right. We're going to make an application and we're going to make a cute widgets application and we're going to call it yeah what should we call this let's call it uh my server why not maybe like I said I'm little brain dead um if this really doesn't go well I'm just going to scrap it and do it tomorrow but I really wanted to get a tutorial out there tonight just so we can get this going because I'm really kind of sick of not doing tutorials although I've been playing a lot of video games probably a lot more than is really healthy so what we're going to need here is we're going to need a spin box um that's going to be for our port we're going to need a label we're going to modify the spin box a little bit here all right so maximum let's just say 64 123 64000 minimum of port one really it should be a minimum of port two um that TCP standard so I think the lowest part you can actually do is two although it might be one all right so and we need some buttons let's say start and stop and we're going to actually name these a little bit here let's scroll up here so it should be pretty obvious what's going on um we're going to set the actually let's set the value here I don't know what I'm thinking about it we're going to set it at port 2000 just because you need a high port because some operating systems like Linux and I think even Mac will say that port one through 1024 restricted you need to run as root or sudo or whatever um like I said I'm tired bear with me here um windows I don't think it really matters but some ports it may actually pop up uac and say ah you can't do that all right so we're just going to go to slot and go to slot now that we've got our beautiful dialogue all set and done we are going to just save that and that and you know call me crazy I actually just like to build and run just to make sure it looks and functions the way I expect it to and right off the bat I noticed that both buttons are enabled so we're going to actually write some code to just you know toggle the enable here and let's call it state now let's call it enabled started there we go and all we're really going to do here is say if if started then and we're gonna we're just setting the you know this the uh user interface for the user here and if it started we obviously want to set this to false because we don't want them to be able to click the start button again however we do want them to be able to stop it and then the inverse is true you know like if it's not started then we want you know them to be able to start it it's been um kind of a hectic hectic few weeks I started a new job um I had to have one of my cats put down I don't remember if I said that last tutorial but uh I took that pretty hard I had that cat for like 20 some odd years um other than that just you know my daughter has gotten married I don't remember if I said that either it's been a busy summer and went to Europe so all right enough of me babbling let's get on here so start stop start stop start stop good and we are going to just double check sometimes with operating systems like I notice this with mac if you don't give enough space it the buttons look kind of funky so I just want to make sure that's big enough for you mac users out there um I know when I run a program on mac sometimes I gotta go in and tweak the UI a little bit and it just gets kind of annoying I don't really know what the deal is but and we're going to save that run it make sure all right good now it's the way we want it now the premise here is you know this is the port you're gonna run on you're gonna click start the server will start or it'll issue an error message and then you know when you're done you'll hit stop all right my phone keeps beeping at me it's kind of kind of driving me a little nut so here all right so let's do file system let's show containing folder here and this is a little different we haven't done this before right so what we're going to do is rename let's just make this lowercase just because I like lowercase we'll call this tcp.pri now if you don't know what a pri is it's a project include file and what that does is actually pretty slick here we're gonna go back to project mode and in here we are going to actually include that pri and we are going to say tcp say if I got my path right here yeah I cannot read what did I do wrong here this is probably going to be utterly embarrassing for a little bit because it'll keep going huh there it is so what it does is it allows you to specify where the pri file is the project include file that's what pri stands for and la la there it is and you can actually treat that little pri like a separate not a project but a project within a project kind of thing if that made any sense whatsoever so the first thing we're going to do in our pri file is we're going to say cute plus equals and we're going to say network and we're going to say include path plus equals and we're going to what is it dollar sign dollar sign print working directory and we're going to say depend path oops plus equals dollar sign dollar sign print working directory now what we're really doing here is let's back into our actual project this is our normal project file right let me clear that error message out this is our normal project file we're used to this now normally we'd put the network up here but what we're really doing is we're building the foundation for a library so we're going to add that into an include file so if we were to actually build a library we would make a library project and then just include the pri which we're doing right here and if you put it in some other directory like if you don't put in a sub folder you can actually just call out the full path and it'll actually include it and when it's included it'll actually just treat this like a project file which is why we have the include path and the depend path set to print working directory so it's going to take the current directory of the pri file and you get it it's going to do the include path and depend path so what is include path and what's depend path well include path excuse me my throat's getting dry include path is well your files your headers your source files etc etc your depend path would be your dependencies we really don't need that in this case but i wanted to demonstrate it could be done uh a dependency would be like a system object or a library or dll so all right so we're going to save all this and just give it a let it uh let it read look for any error messages give it a good build and sometimes i'll actually just rebuild just because i'm that way and we got a good rebuild so we should just be able to control r and voila runs all right so next we're going to create a few classes here we're going to create so let me look at my notes the tcp server class i know so i'm clicking on this little tcp folder here in case that wasn't abundantly obvious we're going to add new and we're going to make a class inherit queue object now notice how in your tcp folder you now have headers and sources so that's one way if we haven't if you haven't watched any of my videos that we've done this or if you haven't browsed other people's source code that's one way you can segregate you know files in your project so we have the actual server now we're going to gotta check out my notes like i said i wrote this thing but i've been working on it working on it working on it i haven't really anticipated starting from scratch here tcp connections and the actual tcp connection class and we're going to flush these out i don't know like i said my throat's getting dry and i'm kind of tired so i'm not sure how much of this we're actually going to get through but we'll get through as many as we can so the overall premise here is you've got your tcp server this is going to inherit inherit geez inherit cute tcp server um from that you will have connections the connections will quite literally just hold a list of connections and then the connection itself which will be the individual connection to with the client then this will pretty much wrap a you guessed it a q socket object so let's actually flesh out let's flesh out the connection class first here because well this is probably one of the more important classes obviously so we're going to include i always include q debug right off the bat that way you can do your debug statements now if you go to include q tcp socket and you can't find it that means you missed a step in the pri and you did not do the cute plus equal network and you have to go back and do that so just you know buyer beware all right so virtual avoid and we're going to set socket um if you fell asleep during your c plus plus class and you don't know what virtual means it means you can override it um overriding means that you can inherit this class which is we're going to do in a future tutorial and you can actually modify its behavior if you don't put the virtual there it won't let you do it and then you're going to be crying later hey why doesn't this work so protected now if you fell asleep but once again in your class um what protected does is it allows this class and classes that inherit this class to use whatever's in this little code block here we're going to make that so that's going to be the socket that we're going to get and then we're going to get uh get socket now you might be wondering why we have both of these well this is going to be the actual reference or i should say the pointer that we're going to set in set socket get suck get socket is um from the signals and slots um just because we connect the signals and slots doesn't necessarily mean that's the object that we're connected to right all right so let's save that out we're going to make our public slots here notice that's how it's got the virtual in front of it um be careful when you're typing that out if you don't download the source code and i should note the source for this will be available on my website i'm going to actually put the source in each stage so if you're at a specific video you can download that source you know and this one's a little special um it takes qabstract socket and it sets the socket state and when i say that's a little special you'll see when we go to connect that signal and slot there's a little bit of magic we're going to have to do there and i think the same with this one too if i remember right i'm going to socket error socket error socket all right so there is our our actual slots here that is actually really all we're going to do for this class this tcp connection class is actually very very generic um we're designing this so that it should be inherited um you should inherit this and then override these as needed so we're just going to flush these out real quick here we're going to flush that out i almost wish those like a super fast way like you could just select all and then right click and well let's see if i'll actually do that i don't think it'll do it this is where i should practice my singing and like you know really drive you guys nuts i don't sing by the way it was just kind of me being stupid all right so let's close that and we've got our beautiful little class here and i'm going to be throwing a lot of qdebug statements into these things just so in the future tutorials you can really see what's going on under the hood because like i said this gets to be a little bit complex and i want us to be able to see exactly what's going on so while it's fairly obvious that this is created we want to be able to track that in the debug destroyed gosh i'm 0 for 2 there there we go now set socket we're going to actually have to set the connections here so we're going to actually go m socket equals socket and then we're going to say and we're going to use the q5 way of connecting the signals and socket signals and sockets signals and sockets i still did it wrong signals and slots sorry this is what happens when you switch jobs just don't switch jobs and you'll be okay and we want tcp connection connected there we go and let's get this little guy and through the magic of copy and paste there's a fly on my monitor it's just driving me insane we're gonna go disconnected now someone once asked me why don't you use the auto connect feature there's actually an auto connect where if you name your signals of slots under a certain naming convention that they'll automatically connect i don't do that simply because it will just drive me insane because you don't know what's going on under the hood and i'm very detail-oriented um and did i not make that well i'll be darn we didn't make that um when you do the auto it just you don't know what's going on under the hood and sometimes you'll get connections you don't want and it could just cause general chaos within your program so i like controlling everything manually there we go and that was a brain dead move and let's see here bytes written all right now state changed let me look at my notes because this is where it gets a little kind of freaky here maybe we're gonna try this all right so there's our state changed and need one more and i'm gonna have to apologize in advance this may take me a few tries to get this right here and we want to go see here's here's the problem let me back up here when we look at the error right if we try to bind directly to error even though it's coming from q abstract socket and i think i actually need to back up and do this with state change too because it's actually not a q abstract socket it's a it's own little entity here and it's not a q socket we have to bind directly to q abstract socket it's bizarre it's actually not a q problem it's a c plus plus problem it's like if we built this and run it it'll actually yeah blows up no matching function call for undersolved overloaded functions to the candidates are blah blah blah blah blah now you'd think we should be able to do that because well we're passing that right but no it won't do it so let me comment this out and see if we can get it to run maybe i screwed something up then error all right let's do this state changed i had this problem before and it really drove me nuts to bear with me here we're gonna go static cast and there's probably you know admittedly there's probably a much much much easier way of doing this we're gonna go dot dot dot dot dot q abstract socket socket error all right so what we're doing is we're just casting this into an actual you know q tcp socket through the q abstract socket socket error and then we're going to pump it into our little application here oops expected token before what did i do wrong static void cast or static cast void anybody anybody bueller bueller let me actually take this i'm gonna take it right out of my notes just because i don't want to waste your guys's time and see what i've gotten wrong here oh yeah that would probably be it right there derp herper derp okay let me actually run this and if it compiles then i'll yeah i'll explain what's actually going on the um this is a c++ problem not a cute problem i mean you may actually run into this where you're trying to set like you know the socket error to an actual slot and the problem is socket error really isn't in the socket it's part of the q abstract socket so you actually have to cast it from q abstract socket to qtcp socket which is what it's expecting which you know sometimes c++ just explodes and does not like that and it took me a while to figure this out and admittedly i had to do a couple google searches and i don't remember where i found the solution but it works so i just stuck with it all right now i hate copy and pasting in tutorials because it takes the magic of watching me type away but there you go so if not sender and the premise of get socket is we're going to get the actual sender when i say the sender i mean when you call a slot from a signal it has a sending object um notice how like connected this is a slot but we have no idea what object called this well that's called the sender so we're going to get socket meaning we're going to get the the object and we're going to try to cast it and i know i'm going to get some hate mail on this so i'm just going to warn you in advance somebody out there's going to go don't use static cast use this other blah blah blah blah and you know i welcome your comments because in many ways i think we're all still learning unless we're playing video games then i'm just right because i play dota and there everybody's an expert in dota all right so what we're doing here and get socket is we're saying if there's no sending object if you just call this directly we're just going to return zero because there's no pointer otherwise we're going to return a static cast of tcp socket from sender meaning we're going to take sender say is it a tcp socket or can it be cast to a tcp socket and if it can return the pointer otherwise it's going to return zero wow that was a mouthful so we're going to say if not sender return and then we're going to say q debug this connected sender and the reason why i'm not using get socket here is because i will call that intentionally on my own we can just you know through some copy and paste magic here let's actually do the get socket let's get those out of there so yeah if it looks like all i'm doing is just spitting out q debug so that's you're right that's all we're going to do for this class simply because i want to make sure that uh we get some good debugging info because this thing will get a little complicated later on and we're going to really need to see what's going on under the hood here all right socket state state changed and here's our little socket error oh all right so this is our class and all its beauty let's give it a good build and run make sure it still goes all right so we are done with the tcp connection class the premise of this class is do i say premise a lot somebody emailed me and said i say premise too much or preface or premise or whatever anyway so the connection is going to come into the server the server is going to send that to the connections class and you'll see why later the connections class going to create a new connection and then pass the socket through set socket where all the signals and slots get wired up and in the inherited classes like later on we're going to make a web server you'll actually inherit the connection and you'll call it like http connection and then you're going to override you know connected ready read bytes written etc etc to do the functionality that you need for your object all right so let's actually close this close this and i'm not sure how far we're going to get through here um just because i'm getting kind of tired let's try to flesh out some of this connections class all right so include qt bug and i also have to be mindful of the clock because i think my videos have a a maximum time yeah the youtube thing just kills it it won't let it go past a certain time so q thread and we're going to include we're going to include qtcp socket whoops include qmap and we're going to include q read write lock and we're going to last but surely not least include our tcp connection all right so let's go through here i don't know why i added this in um i just really wanted to be able to get a count of the number of you know number of clients connected and that's why we have the q read write lock because you have to make you know a read write lock it goes with threading bad things happen if you try to access the same variable from multiple threads and we'll we'll get to that we'll explain that i know it sounds confusing just drink the kool aid bear with it so we're making a qmap um a map is basically a linked list basically it's a map in there we're going to have a pointer to a socket and the pointer to the collection or the connection so we'll have the socket and the connection class now you may be asking why do you need that when you've got the actual you know connection class has the socket in it it's just easier to look it up by socket basically all right so there is our map we need to be able to remove a socket all right we got some signals here and i'm going to apologize in advance if some of these signals and slots just never get used i kind of wrote this by the seat of my pants because i was really excited and i just wanted to get it done it was a challenge so some things like my keyboard are just not really working well all right so void disconnected and yes if you didn't know that you can actually make protected slots so these slots are only visible to this class in classes that inherit this class we want to make some public slots here i got a message is actually quite funny it was from a student in south africa who said teacher he calls me teacher i guess there's uh colleges that actually show my videos which i actually find quite amusing because i think i should be paid i'm just kidding i find it quite amusing because i don't think these are really college-level material but um he was saying well you know i like your videos but you type too slow i guess he types like insanely fast and he's getting kind of frustrated watching them because i sit here and type and talk and type and talk so i told him you know what why don't you just speed up the video all right so let's go over the header of this class here and it gives me a chance to make sure it's actually correct um we've got our constructor our dest destructor we've got a count we've got a member variable called m connections which just is a map of sockets and connections we have removed socket which you know should be pretty self-explanatory we're going to remove a socket from that collection we have some signals of quitting and finishing i should say finished we've got some slots disconnected and error and some more slots start quit and accept now i want to talk about except real quick here when the connection comes in it's going to come to the server notice how this is a slot yes we're going to connect a signal and slot from the server to the connections you have to do this because this is going to be running on a separate thread and you can't take the socket object and move it between threads so you need to take the handle this qint pointer and actually push it out through a signal and accept it in a slot then in that slot on the other thread we'll create we'll create the socket object now another caveat is this qint pointer something has changed in the qt API this used to be qint 64 sometimes it's called q handle sometimes it's called qint pointer so if you start getting compiler errors try changing it to either qt 64 or q handle or qint pointer whoo that's mouthful so i got this guy from south africa saying i type slow and i've got my girlfriend saying i talk too fast so there's our signals and we'll just whoops get our slots going here i can tell it's been a while since i've done a video because i'm like looking around for notes that aren't there and my throat's getting dry and i'm actually going to take a drink of water so if you would like to you know go make some popcorn or something real quick i'm just gonna grab a drink of water i've got my water right here so you get to listen to me drink i know it's it's sexy try to contain your excitement uh that's better all right now let's start fleshing out this little class here we've got our did we include qt bug in that let's double check that yeah we did good and like i said in case you're wondering we want to really make sure that we know what's going on under the hood here all right so right away we come to our first challenge if this is going to be on a different thread we can't just arbitrarily return a value we need to actually you know get this on a thread safe way and i know i'm going to get mail because i always get mail um we're going to use q read write lock somebody else is going to tell me to use q read locker somebody else is going to tell me to do a mutex but we're going to use q read write or lock so whoops i just hit the microphone i'm that frustrated with the fact that i'm going to get emails about this i'm kicking my microphone anyway so lock for read and once we lock it we need to unlock it so unlock to unlock now what this does is it takes this segment of code that we're going to put in between here and it locks it for the thread that we're currently on so we're going to say int value equals and we're just going to grab that maps count right that way have two separate threads like if one thread is actually adding in a socket another thread is removing a socket and then this thread's going in and try to read the number it's not going to like freak out and explode on us because it's going to grab this little chunk of code and it's going to lock it so nobody else can do anything until we're done let's actually look at remove socket here remove socket is when a socket's disconnected and we want to well remove the socket from the collection so we'll say if not suck whoopsie what the heck did i just click there if not socket return we got to do a little defensive programming here we'll do a little uh little speed defensive here speed defensive is basically like you know there's no sense in iterating through the loop through the through the connections object if the socket just doesn't exist so contains actually does that for us whoops now if you notice i keep doing this q to bug this because we're going to have a lot of objects flying around in memory and we need to know which specific object with the memory address because you can't just you know get rid of a socket that's open we got to actually shut that bad boy down so what we're going to do is we're going to disconnect all the signals and slots out of that socket object because we don't want to create what's called a signal collision and then we're just going to close it so yeah i had one cat put down she was just old and the other cat is now like this complete attention mount monger i mean he's like this crazy ex wife that just won't leave me alone he's like waiting for me when i get out of the bathroom he's waiting for me when i get home from work he watches me while i eat my lunch it's just kind of getting creepy all right so delete socket didn't make much sense we should actually say deleting is that even how it's spelled new so we're going to delete the socket now some of you would be inclined to call delete socket i'm doing socket delete later i've found that if you do delete socket sometimes that socket's still in use and when you try to delete it it actually crashes your program it causes a segment fault then after we've done this we just want to make sure that we actually know how many clients are in our object here that way you can verify that they actually have been removed um yeah we're getting really close on time i'm going to try and finish this class but we'll see how we're doing on time this video is getting a little lengthy here all right so where are we start i'm jumping around per my notes my notes are in a specific order and unfortunately this doesn't really match up and what we're going to do here is just get the current thread because remember this is going to start on a brand new thread um and then quit gosh i keep hitting caps lock it's going to drive me crazy if not send it return because remember we're going to call quitting through a signal slot architecture and then we're just going to go through here and we're going to close each one of these sockets because remember this is when we're quitting so we need to close these sockets out and that's why in the uh removed socket we were actually checking to see if it's open because when we go to quit we're just going to go through all of our keys those sockets and we're going to say just get rid of them remove them but if it's open we need to actually gracefully you know close that connection maybe if we remember to do uh remove socket here and in case you're wondering yes the the cat that used to make the noise in every single tutorial and used to meower head off that's the one i had to have put down finishing all right so emit finished and we're emitting finished so that we can tell whatever connected objects aka the tcp server that the connections is actually closed i mean what we're going to do is we're going to go through close all the sockets wait for that and then just emit our finish here all right let's see except whoo except yeah except is an interesting one because this is we we've got to call this through a signal slot because remember you cannot create a socket and then move it across thread so by the time this is called we're already on a separate thread so we're going to accept that now notice how i'm saying this is the parent of the qtcp socket um you could actually create the socket and then do a move to thread but you can't give it a parent and then when you try to connect signals and slots to it it gets kind of funky and it starts really complaining so that's why we're doing this method and if it's not abundantly clear it will be by the time we get through these tutorials we're going to set the socket distributor which is the handle and if we can't do that then we're going to say q warning if you're wondering what q warning is it's a lot like q debug it just flags it as a special message so we know that this is a warning and not a normal debug statement and there's a lot of cool things you can do with q debug like you can filter out specific messages you can set the only show warnings only show errors show a certain level of error etc but that's for another tutorial if i haven't already covered that could not accept connection all right so and we're going to connection delete later now i remember connection is appointed to the tcp connection object so this has already been created and we're going to move that to this thread that this connections classes on and if we can't accept that socket we've got to you know do some memory cleanup here we got to delete that later i always favor you know object delete later over just hard deleting it because well bad things can happen signals and slots nothing real special about that so i'm just going to copy and paste that to save some time here same deal you've got you know disconnected to disconnected you've got your what is it your error yeah socket error to error and i think i just screwed up my class when i did that didn't i yep there we go nope no i didn't i just screwed up my screen a little bit here clients equal connections count and we're going to emit the socket connected now you see how we're emitting socket connected that's right you don't have to just emit your own object you can actually force other objects to emit signals so we're taking that side that socket and we're emitting the connected signal right there all right what else do we got here what are we missing we're missing disconnected and air let's try to flush this out then i think we're going to call this tutorial done um because i'm really hitting my time limit with youtube if i haven't already yeah i don't know what the deal is it's i used to do really long tutorials and then i noticed that when i uploaded them like only half of the tutorial would actually display it made no sense and i figured out it wasn't a problem with my stuff it was youtube chopping it off which really made me mad air removing wait what i'm in the wrong one sorry we're disconnected we're gonna get the socket object from the actual sender here so i'm just curious if any of you guys out there actually play dota i know some of you do because some of you have actually tracked me down on steam and i'll be in the middle of a game and you'll start it say hey i've got this problem with my program and i'm like i'd love to help you but i'm busy like shooting somebody in the face um there's this huge update for the video game coming and i'm kind of wondering what it really entails because i haven't really read about it i've been busy all right so we're just gonna get a socket and if we can't we're just gonna return and if we can we're gonna go remove socket now we've done our defensive programming and remove socket so really that statement's not even needed i just wanted to do it just because i like being in the habit of making sure we have an object before we call something with it all right and then error if not sender return once again we're defensive programming here queue debug this error and socket let's actually do sender sorry oh my gosh what do i keep clicking there somebody out there knows what that is and they can probably tell me what it is and we're just going to try to remove that socket once again all right so that is the connections class in a nutshell so really all the connections classes let me actually take it to the header here when we start the server which is going to have to be the next tutorial um we're going to create an instance of this connections class and we're going to move it to a separate thread so the premise here is the server actually lives on the main gooey thread that thing will actually accept connections remember that's all the synchronous so it'll take that connection and it'll emit a signal which will go to this accept slot with the socket descriptor handle then we create so if we go in here and we go oops did i already have it down here yeah except in the in here in the except we're actually creating the socket and connecting to it and all that so let's actually build and see if this runs i want to make sure yep all right so that's all for this tutorial um the next one we'll actually start fleshing out the server and we'll get all this wired up and actually make a working tcp server and then test it out oh i should know the source code for this and other tutorials is out on my website void realms dot com um so you just go under acute and i'll try to get that out there tonight but no promises um also this site is 100 funded by your donations um if you cannot afford to donate please do not i don't want anybody going hungry but if you've got millions and millions of dollars and you're like an arabian chic or something you know feel free to donate a couple billion so that i can sit here and do tutorials all day anyways that's it thank you for watching


a mathematical snack from the Association of Teachers of Mathematics. We have four numbers, one, two, three and four, and the task is to see what other numbers you can make. There are two rules, one is you must generally use these numbers and the other is you must use them all. So I'll look at that and I might start by writing 14. 14, that's used the one and the four. Two threes are six, so I might want to do 14 plus two times three. 14 and six is 20. So I have made 20, but if you take the calculator on your phone or some other calculator and try and put this in you might get 14 plus two is 16 times three and it will suddenly say 48. So to get this right I need to put a bracket around the two and the three. 14 plus two times three is certainly 20. I could write 14 subtract two subtract three and that is 14 subtract two is 12, subtract three is nine. So each time I've used a one, a two, a three and a four, but I can do other things. I could write four plus three and put it in a bracket. Little two up there, that means squared. Four and three is seven. Seven squared, what haven't I used? I haven't used the one plus one. Seven squared is 49, seven sevens plus one is 50. So I have got 50. The challenge is can you make all the numbers from one to 50 each time using one, two, three and four?