Ep. 27: Server Disconnections - Network Chat Programming

The Cherno · Intermediate ·12y ago

Key Takeaways

The video demonstrates server disconnections in network chat programming, handling disconnections by sending pings and checking for responses, and using a protocol for client-server communication. The Cherno showcases his project, ChernoChat, and provides source code on GitHub.

Full Transcript

hey what's up guys my name is Cho and welcome to episode 27 of network chat programming So today we're going to talk about if the client kind of disconnects you know not cleanly if it's all dirty and stuff in it's kind of disconnection and what happens is um I don't know the user decides to force quit it or you know terminate the process if you're on Windows or if the computer just loses power and crashes or if the computer crashes right or if um or if the the the client loses a network connection for whatever reason the server can no longer contact the client okay so it needs to kick the client out of there because the client is no longer connected okay and we need to handle that so what we'll have to do is we've got this little thing here called managing right we've got this manage clients method and what we're going to do in there is well as you probably guess we're going to manage our clients and the way that we we're going to do that is every so often we're going to send a bit of a ping right so for each of these clients here right for in IAL 0 I is less than clients do size i++ and by the way Pro tip you do not want to go ahead and make it for each Loop so for clients are C uh clients right and this will actually Loop through every client clients and you'll have c as a as a thing and it should have been server clients of course but um you don't want you don't want to do this whoops server client you don't want to do this um because occasionally you can get issues when you run into multiple threads and you can have thread concurrency issues so don't do it this way okay a follow rather than for for each Loop do one of these it'll be much much safer and you'll get much less uh exceptions and problems okay important okay anyway so what we'll do is uh for each of these clients we'll essentially send something to them and then we'll kind of want to get a bit of a response from them okay so I just realized that instead of doing this way we could just simply go ahead and go send uh to all we'll send a message okay and we'll start this with something along the lines of I don't know I don't want to start with C we'll start with I I don't know why we'll just start with i and then we'll just send a message kind of like server I don't know we could say ping whatever so we'll send that to each of the clients let's terminate this looks like we're still running the server um all right so while running we'll send we'll send each of them one of these things and then what we'll have to do is we'll have to see what their response is okay so how are we going to do that how are we going to see if the client has responded or not well what we're going to do is we're going to actually um make another array list here right we'll make a private list of uh of server clients oh no actually no not a private list of C clients my bad we'll make a private list private list of strings uh and we'll call this client response and we'll set this equal to a new array list uh of strings of course and what what we'll do here is basically we'll go back here and we'll say if uh if we start so we'll do another LF in our Pro process here else if our string here starts with an i then we know that um oops make sure you close that off as well and get rid of this all right great syntax is awesome now um what we're going to do is is essentially we got we we'll say that as long as the client has kind of I don't know as long as the client has um has kind of reped to us right if it starts with an i that's kind of like a server kind of checking thing we'll go ahead and we'll add it of course into the client response so client response um do add and then of course we might go ahead and just split it straight away so string uh which is string whoops do split and the rexes that we'll use are just slash I slash or SL a slash okay um and then we'll go one so in other words because we expect to get that kind of message we'll just make sure that if we get a message such as we'll probably send the ID so if we get a message such as that um we'll just extract the important data which is the middle out of it could have just probably turned it into integers but doesn't really matter so um let's move this one inside of course and we should be sweet yep we are sweet um okay right okay now what we want to do is um now that we've kind of got this array list and the reason we do this is because we'll check them one at a time kind of thing and we'll we'll rest in between um but what we'll do is we'll say as we're managing this we'll say um that essentially for each of the clients here so forign i0 I is less than all of our clients I plus plus um what we'll do is we'll say that if I guess if the client response do contains uh let's see so clients. gei do sorry clients. i. get ID right so in other words if our client response um does not contain an ID with this stuff then what we actually need to do is obviously there's a problem okay cuz the client hasn't replied to us yet so what we'll do is we'll handle that um now one thing we have to do I just realized let's just change this string into integer okay that's probably going to make things a bit easier um and if we go down here instead of adding this string do split we'll also have to add an integer do pass in and then we'll just uh kind of come in here and make sure that it's being cast to an integer okay just like that um and then it'll just make it a bit easier to compare the two without having to always do those kind of things so one thing we need to check is because remember in this was a while ago and I'm sorry that I I'm only getting to this now but uh we have our server client yeah and um we've got this variable in it called attempt and we created that when we created this class but I didn't really talk about it I said that I covered it later and here we are that was some nice foreshadowing for you guys some uh some sick Breaking Bad style foreshadowing so um in other words if we do if if we're not containing it right we also need to check if our attempts have kind of kind of expired so if clients do uh in fact let's do this so client uh sorry server client C equals clients. Getti and then we'll just put a c here um if c. attempt right is greater than or or equal to let's just say our Max attempts and we'll make a quick field up here called private final int Max attempts and we'll set it equal to five for now so in other words the the client has five chances to respond until it's basically disconnected so it's kind of like a retrying connection kind of thing so in other words if our attempt is currently greater than Max attempts then uh we need to disconnect that client so we'll go ahead and go disconnect C.G ID with the status of false so in other words it was an unclean exit however if um if we haven't uh reached our maximum attempts yet keep trying so all we'll do is we'll go ahead and set c. attempt Plus+ okay that's it okay so this is what happens this stuff here that we just read that we just wrote all of this stuff happens only if we couldn't find a client response from the client from one of the clients only if one of our clients didn't respond to us so we asked them hey man how's it going and he just sat there like an idiot and didn't reply to us cuz he's mean that's a bad thing that's why we want to disconnect him okay um however if we did receive a a response then everything's great right so what we want to do is we want to remove that client response clearly we cuz we're going to send these now well pretty uh pretty often client response. remove the ID of the client now we can't simply go ahead and say C.G ID and the reason is you'll notice that remove uh you can actually if you just have your mouse here you'll see that it's actually using the int index remove okay so what it's doing is rather than removing this will actually give us a out of bounds exception because what will happen is we're trying to remove the ID but it's thinking we're talking about the index cuz they're both integers so if we go ahead and go new integer and then enclose it in that you'll see that it actually uses a different method it uses the object method which is the one the one we want okay so great we've removed it now what we want to do now what we want to do is obviously also clear the number of attempts so c. attempt equals z okay because um maybe it took a few times for it to work but if it did work work in the end we want to clear those attempts otherwise if we're stuck on four attempts and then it fails for one other time gone the client's disconnected we don't want to do that so hopefully this will work okay so to test this out is going to be a bit difficult but that's all right to test this out what we'll do is um oh this is the other thing that we should mention this is this is going to use an awful lot of processing power okay this is doing it as fast as it can there's no need for it to be doing it as fast as it can the other thing you'll probably notice is that this attempt will happen really quickly okay maybe the client a bit slow this will happen really quickly this probably disconnect a client in about 2 seconds so what we want to do is after we send we just want to wait for a while okay and how do we do that well we just sleep for a while okay so we'll type in thread. sleep for probably around 2 seconds now thread do sleep happens to be pretty inaccurate we don't care about that in this case we're not using it for any kind of timing we're using it to kind of just slow down okay because we don't need to be doing this all the time so what this will do is it'll kind of uh free up the system resources that are to do with this thread in particular um this manage thread um we can also do you probably would have notice manage do sleep but nor one uses it like that you can see that it's actually a static method because um thread. sleep will actually it'll kind of pick up on which thread it's being executed in and thus it'll make this specific managed thread sleep not so it won't make our listen thread sleeps don't worry about that okay thread do sleep is awful for timing but it's great for if you just want to take a break okay um and this will make this will make the wild running Loop run a lot slower so it probably Run 1 every 2 seconds or so um and with a few Nan seconds uh give or take um but that'll just make sure that we're not running this as fast as we possibly can cuz it's useless to us we don't want to be using that many resources for something that simple now let's uh save this and start server main we'll also start our login here so Cher Local Host 8192 great and we'll go ahead and hit log in now if we were to go up and you guys can't see this now but I'm just going to the Apple menu and force quit if we go into force quit or we could do do this inside Clips as well but if we go this is our com do the sho. the CH chat. login if we force quit that and we force quit it then it's gone and I think so is our server did our server go as well there no it didn't here it is so oh here we go and then we get client Cher timed out so you guys didn't see that cuz it switched views unfortunately let me just see if I can kind of organize this a bit better um what we'll do here is go back to console uh the so you can see the server is still running if we start the client out the client up again and I'll keep the view on the server I actually won't be able to in a minute but um Local H A 192 log in what I'll do is we've logged in here what I'm going to do is I'm going to I'm going to exit the server let me just ignore this first time down right I've just logged in again with an identifier five 277 what I'm going to do is I'm going to um go back here and I'm going to switch to the client so I'm then the client now and close it and then switch back to the server so what essentially I've just demonstrated is what what will happen I guess if we uh if we don't send anything so you can see it's timing out and we're disconnecting so what will happen but obviously we haven't made the client reply yet okay so if I go ahead and I start server main again cuz I'm not running anything um that was a very poor demonstration by me sorry about that but if I start if I start this up again Go Local Host a192 hit log in and and I just wait here for like 5 Seconds oh it'll be 10 seconds actually but let's just wait for a while okay you can see that actually in thebug every now and then we're getting another okay and there we go we timed out okay because it it sent out it sent us those uh five kind of attempts but we never replied and the reason we haven't replied yet we are and if you T something like a test it won't return to us okay because the servers disconnected us now um the reason it's disconnected us and we can't send this anymore because we're disconnected but the reason it disconnected us is because it didn't get those messages back and why didn't it get those messages back because we haven't actually coded the client to do that okay so over here in client window where we have all of our code here let me just make some more room here real quick come on there you go um in our uh let's see okay so message starts with m go ahead and go else if message do starts with I okay SL I slash then what happens is it's clearly a um the server is trying to get into contact with contact with us so in this case all we'll do is we'll say um let me just think for a minute I guess we'll just say something along the lines of um well string our string text will be uh n i along with our ID so client. get ID along with an e at the end and we'll just simply go ahead and uh send that text right I think we called it text yeah we did uh and this will be false so we don't want to print it to the console or anything um and that should work okay and now the server shouldn't time us out so if we go back into here that's why we have those attempts as well cuz occasionally uh our Network might drop out for like 5 Seconds not 5 Seconds that's a lot for like 500 milliseconds and we don't want to instantly disconnect the client so we'll launch a um we'll launch a server main here launch that Beast go to login launch that Beast um Cho Local Host 8192 log in see here we've logged in let's just wait a while and if you come into here you can see that I keep showing yeah you see that kind of thread that just blinks for a second that's are sending the Ping okay um and you can see it's not timing us out we're still here great isn't it it is pretty sweet so now if we go into the client into login here and we terminate so we're not ending it cleanly we're terminating it you can see here you can see the server is still sending that and it's not getting a reply anymore so wait for like three more times two and then last one and then now we get the client timed out okay so what happens is the server's tried to send five messages and it's got none of those back in a row so in other words we've timed out and it's disconnected us from the server and you can see it's no longer sending that anymore so that's how that works I hope that kind of cleared it up for you guys that's how we disconnect if it's not a if it's not a clean disconnection okay if the um if the client permanently loses their internet connectivity or for some reason their computer crashes we can disconnect them as well well okay that's pretty much it guys we are done okay in terms of actual I guess um in terms of actual uh chat application we're done this is the end of network chat programming um so if you guys will looking for the kind of the bare minimum without any kind of plugins I guess I'd call them then you're done this is now a fully functional chat application okay now the next few episodes I'm going to show you guys how to um do a few of the things that you requested one of the features was private messaging one other question was uh to see clients how many clients are online and stuff like that um and to to see who's online Okay that was a requested feature um and then there was another um feature that I think well I'm probably going to next episode I'll show you guys how to actually use I guess give commands to the server so if you're running the server you can see who's online how many clients there are in like a command base interface and then we'll Implement a a similar thing for um for uh for the client side GUI but um that's it okay hope you guys enjoyed this episode of network chat programming great series and then the next episodes are pretty much just expanding on what what what we've done but this is the entire framework for network chat application and then soon we'll move on to doing this stuff in game programming series for multiplayer okay so essentially not chatting of course oh we might add that but primarily of course being able to play this as like an MMO essentially but on um our game programming series anyway thanks a lot for watching guys please hit the like button for what is officially kind of unofficially I want to say the last episode of network chat programming and I'll see you guys in the next episode of network chat programming later [Music] guys

Original Description

Source Code ► https://github.com/TheCherno/ChernoChat Twitter ► http://www.twitter.com/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno ------------------------------------------------------------------------------------------------- In this video: - ------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches how to handle server disconnections in network chat programming by sending pings and checking for responses, and how to implement a protocol for client-server communication. The Cherno provides a comprehensive example using his project, ChernoChat.

Key Takeaways
  1. Send a ping to each client
  2. Check for responses in the client response list
  3. Use a regular expression to split the response message and extract the client ID
  4. Remove client ID using object method to avoid out of bounds exception
  5. Clear number of attempts after successful disconnection
  6. Use thread.sleep to slow down the disconnection process
💡 Server disconnections can be handled by sending a certain number of messages without receiving a reply, after which the client is timed out and disconnected.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →