Creating a TCP Client in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back this is a part of mine networking series last time of the last networking video we created a bear bulletin server this time we create a bare-bones client to connect to the server and we always start coding after the seed ok so we're going to go ahead and create a new C++ project so this is visual C++ empty project and I'm just going to call it fair pull mine 2017 for the name of the project and that's going to give me a completely blank slate so I'm going to create a new item in here I'm just going to call it main dot C and now I'm going to add the include files in my private comment you'll see this is very similar to the people and serving a few if you haven't been following along with this tutorial series then have a look there's a carve up the other links to the beveling server and also the sort of introduction to network sockets as well and I can give you a basic idea of how sockets work ok so I'm told a couple of things because libraries to do comes with so one include iostream because I want to use CNN Co I also want to include the windsock TCP lot functions and then I want to see I want to tell C+ but I want to tell the compiler to also link this with the windsock library and the way we use that as user pragma comment to the pragma comment live and then we specify the library Ward's in this case the one that stuck to bit look code because where it's a 32-bit programmer building and as you can add this up here you can decide not to do this and make this those lotta reasons and the Internet's for them as to why this is a bad idea is it is bad idea its bad programming practice book for the benefits of spare balloons client it's good enough for our purposes and we have an entry point when also working for Strings as well from normal other reason other than just to show you that we can use strings and but we can use C++ Perl strings it with networking we just need to do a couple of extra kind of conversions across so therefore we do the the several other connector is the bare-bones server that we created in the first part again link is up there somewhere I want to say to the IP the server's IP address is going to be entering thousands of usually the one piece is connecting to the local machine and the port number is 50 14 which is also the port number for the dessert so this is a listening server so this is the an IP address of the server and this is the listing for number on so our application is listening on port 54,000 so the server application is listening in portrait or face do we want to do here is what the next light window create a socket Helen hence structure a couple evenings ethos you get them click the server cue file group Suzanne and Stata gracefully closed and everything and then let's finish so this is what we need to do to get this program to to be a network client and it's not there's there's not many lines of code 100 lines of code and those too many lines basically ok so to start Windows is the exact same way we did it for the bare-bones server so I need to create a wsad test structure and I need a birthing number to see the work third conclusion 132 or windsock and then I just want to start it so the act when so Rho equals W as a backup I'm a person that the very want to choose where and pointer to the data stream can be address of the data now this data gets filled in if this returns true so we can check as if it's if it's right another undistribute it returns null bed you never eat tons noodle then we have nowhere and if it returns something else then we have an error so I say if W X result it's not equal to you wanna react to the errors and can't start link up arrow number and you pass over here result so let's hos initializing windsock we pass in the version we want it's filled in this data structure if it returns something is non wheedle that means it was an error and if as an arrow then we can start windsock which should be right over the next thing we want to do is create a socket and the socket is the identifier that our program will use to communicate with the network layer to send data out to the remote server the remote machine so this is the same idea as we have in the server side but this is on the client side so stroke it and I can call sock yeah speak code whatever you want we need to personally address family so I'm using this form here which is IP version 4 that's why the air finet of GF is address family and ni net is the internet style address family so when the first created this the internet was broken for which was just the version that we used for the longest time so there's in um a different version if you wanted to use version 6 we would just add a 6 there and then use version 6 the type of stock you want to create is chalk screen and then the protocol which just leave it as blank just week so I don't leave it as blank put in judo nails that work so there's the protocol itself so at this point want to check to see if the socket is valid and if it's not valid we want to be low early and then tell the user that it was an error so I'm here at the socket error now socket returns a valid socket number so if it's a negative number to invalid socket is negative 1 so if it's any other number then it's valid so in order to get the last error like a period with the WUSA start of returning the the error code to get the last error we actually do have to be whe x last arrow so that says we created a socket so we're getting through this quite a bit so initialize one so I'll create a socket the next thing to do is fill in the instruction a hinge structure is going to tell windsock what server and what port we want to connect it so I'm going to say that it is where I am let's hit OK why we got into the end because it's a look because it's an IP version 4 version of the structure so hint address and family here oh yeah pink dots and port equals and remember because networking is big endian and on a little alien machine and even if it wasn't a little endian machine if it comes from Pannell critic otherwise it turns everything down in the screen words work if I even if I was I shoot you the h1 because then I want to I want to have my code be portable enough I can take this and put it onto anything else so when you H : which turns for host to network short and I will use port number and then I want to add the address and the way we use that is we turn it on which is ina and then it's a pointer to a string to number so a specified address family again and then I specify the address string she's my IP address and you notice list on that squiggly there there's no suitable conversion function string to PC STR which is a pointer to a see style string but I can do store C underscore STR and then I get a function make sure you put in the brackets after that and that converts it to a string to a see spell string actually it converts it to a conscious choice star but for our purposes it's which convert it to a see spell string and then you want to passing the address or the buffer of where the internet IP address go so this is going to convert it from the string version of 127 dot 0 dot 0 dot 1 link to the individual bytes and place that in Siberia all right so that suppose we have a hint structure know what those want to connect to the server with you cymbal no rish - when we boned a socket to the IP address port number on the server it's not quite the same but it's similar to Sonic a int on local connect and then we want to specify the sockets were a little bit shocked and then we want to pass in the pointer to the address structure so you stop and rest and then it is pink and then it's going in a cycle and again we want to check and see if this is verax we want to see if connection result now you can see drop it error so like equals socket and later of that go to Dirk if connection is all equal shock error than I want to and open something bad has happened and we terminated the cache or returning the program and service are on alert immediately but this thing we want to because we actually have a have a connection we want to tidy things up so I went to close softest top wate knot and then with them look at that point I actually have a valid socket and I've been doing things with it so I want to clean up there it's like we should probably do that here no think about it because we have actually done something here we've initialized when socks we should actually clean up so we should probably put that in there okay now assuming that at this point the cooling 49 we have everything we need to do we've connected to we've created with initialized when soft we've created the socket we've bounine for boned back to that sock it to a connection to a remote server you know how to do well-lit - right I need a few more variables here to hold some data first thing is I want to create a buffer which is 14 size actually good enough for our purposes I also want to have a thing for you input and then when I do my lip oil is our input top prize here and inside here I want a prompt then I'll wait for fun cool I like it I really do like writing these comments here because then I'm kind of right with the program writing this sort of pseudo code for the program while also getting you know fairly good comments and I don't need to think about those the comments because they actually become part of the program at that point there's an argument form against comments but I I cannot like them but I can see that it's almost like it was a break and the code all this is the initialize windsock are all this is the create socket part this is the fill in the structure part so on so it does help I think okay so I want to prompt the user for some text so I'm just going to give you a very basic prompt which is that and then I'm going to do get wine in your input and I want to use getline because I want to be able to have spaces and if I if I don't use get languages ucn then I'm not going to get the full line of text I'm going to get it's going to if I do a hello space world it's going to send a little first and then it's going to send world after that and I want to send it as one piece of text okay so send the text bill if and then I'm just going to do this all in one light actually now I work at Goodman I say you think end result equals end and I want to send it out in the socket and this is the buffer I want to use which is key to encrypt dot key string comma zero in clicks dot size plus one con view and the reason I want to do as plus one is because if I do if I send hello that's each elo which is five letters but NEC that's actually six wipers because every every string is traveling with a real character so when I send the size of this I can notice that see spell string of actually sending six characters when I send a little because I have h-e-l-l-o and I have a trailing one trillion euro at the end which is white the size plus one but really I should check to make sure that size is like the users typed in something so I should be if either input starts I taking zero do all of this okay okay you want to make sure the EVAs paper in something as I'm saying well this is extra I'm going to put in there extra okay to send okay so if send result is not equal three socket error I'm going to wait for the response and then I'm going to echo response the console so my response is going to be bite because receive and then it's going to be the socket the buffer the length of the buffer and then blacks which means to leave you but I'd want to do that buffer right I mean you you know memory box common flow and then if the client disconnects base receive is going to decrypt on with you if an error happened if bytes receive is going to contain minus one so I only want to respond only want to err call my response for the console if I received anything so if sites are see description field so how's it up it's a positive integer I'm going to sto server and then I'm going to do string and you specify the offer the Europe index and then activate he okay what am I doing here I'm opening this text to the screen and then I'm outputting a brand-new string let's meet up of the contents of buffer from this index and this number of characters and that number of characters is the number of bytes received okay and I need to do anything else not I think we're good here so 80 lines just go okay so graceful ecozone everything so after at well Steven link to close the socket recorder the topic you're talking and then we want to shut down and blink out properly and that's it so that is everything so make sure you got everything taped in correctly and then when you compile it when you build it the bill that you press control shift B it should succeed should get one and one succeeding or like I stuff no we want to do is we want to go over to our server and make sure that we are on 54,000 and that's on any and then we want to run the server so this will run over here and I'll clamp right over there and look at the big ones point here and then when I run this a bat will pop up annoyingly any of you run that verbling server it doesn't do anything there and I run my client which is now being cleaned by avast so I have on this side here I have my client in this side here I have my server so I can type in anything here the old world and you see that hello world appears on my server side and then over here I get my echo back here so one two three obviously we have a bit of a bug over here as far as output concerned but that's not a problem because you can actually just fix that while we're here so we go back to a verbal server then here we have output here so we want to do angling there it was missing this bit here no you have over here and the world and that's it guys thank you very much for watching if you liked the video thumbs up if you didn't like the video dumb bye and if you you know want to leave me a comment below if you want to see more videos like this and let me know and because the algorithm has changed I guess please subscribe please hey it's really important to me it's not really I if I can help that's great if I'm not helping then let me know so anyway thank you for watch for your time and I will catch you next time bye
Info
Channel: Sloan Kelly
Views: 66,572
Rating: undefined out of 5
Keywords: c++, networking, winsock, gamedev, game development, game dev
Id: 0Zr_0Jy8mWE
Channel Id: undefined
Length: 22min 11sec (1331 seconds)
Published: Fri Feb 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.