Ep. 24: Talking With Each Client! - Network Chat Programming

The Cherno · Intermediate ·12y ago

Key Takeaways

The video demonstrates network chat programming using a client-server architecture, handling multiple clients, and transmitting messages between them. The speaker uses tools like yarn and Local Host 8192 to test the connection and discusses the importance of port forwarding and setting up the server on a Linux server or at home.

Full Transcript

hey what's up guys My Name Is AO and welcome to episode 24 of network chat programming all righty So today we're going to talk about basically handling multiple clients and the awesome news is that essentially after this episode you guys are going to basically have an entire chat program ready to go okay it's pretty much the end of the series I'm probably going to I'm probably going to put in a few more things that you guys wanted me to but um essentially after this episode we're pretty much done right it's a functional a functional application there is one more crucial thing we need to do after which is handling the disconnection of clients but um after today you guys are going to be able to send messages to each other and you can give this to your friend and it will work and you guys can actually talk remotely so um just just for a preview of what we've got now if we go to our server main class and I'm just going to I'll just leave it here I'll put it right over here maybe if I launch that and that launches our server of course um you can see that uh if we if we launch our login oops I launched that twice no I didn't yes I did okay let's get rid of one of them okay and I go Yan Local Host 8192 hit log in and um and you can see that we log in here but uh the important thing is that um we've con we've connected to our server and we can type stuff in but um that's essentially where we're up to right now right but what happens is the server you can see with this it receives all this stuff but it it uh well it doesn't really transmit it to other clients um and where kind I well actually does transmit it to other clients but our clients don't receive it correctly okay so um essentially what I'm saying to you guys is if I launch another instance of this and I'll call this one Cho Local Host a192 log in then we've now got two instances and this is probably going to be hard to see for you guys but um if I say something on this instance you know like hey it's not going to show up on the other instance and likewise if I say uh you know what's up over here here it's not going to show up over here okay and we're going to basically make it show up so that you guys have a chat program to do to to deal with essentially so um the first thing I'm going to do is I'm going to go to uh I guess the client right so the client is what essentially all of the behind the scenes networking for the actual client window that's where it all happens right and inside over here we've got uh this method here called uh receive right what this does is it rece receives any inbound packet and it returns it as a string okay that's what it does and then that of course is used in the uh where is it that of course is used in client window where we have this listen method so sorry the client is the the kind of networking stuff and then client window is the interface so that's in client right there's a method called listen and as you can see here it runs the client. receive method now if the message starts with the c so in other words our little C kind of prefix which indicates that it is in fact a connection packet so in other words the connection should be established here that's why we have this successfully connector thing then um then well we're good okay a sense and it we're good in the sense that you know we can connect if we get that kind of packet however what we should handle is else if the message starts with um you know a for SL and for slash because that's our prefix for an actual message if we go back to our server here and we look at our uh I think we got a thing here called send to all or something send to all right it actually oh okay it actually just transmits it so in other words if we go back to our client here or our client window when we send something and we've got a message here you can see that our message receives a prefix of this slm slash okay for slm for slash so over here where we've got this for/ M4 slash um we know that it's a message that is text that our all of our clients should actually receive so what we can do over here is say something along the lines of uh well first of all what we want to do is I guess um we'll make a string message here um no we won't call a message that's stupid we won't be able to actually uh we'll call it uh text so message do uh split and then we'll use the rejects of M and we'll use the pipe symbol of course to indicate the or kind of in the rejects so that we um so that we actually uh well we divide it by both of them we split it by both of them and then we'll pick one again okay so it's the same thing that we did over here okay to separated basically this will just extract the text from amongst everything else um so now we've got the text here and all we need to do now is kind of send it to the console right so we'll we'll send it to the console like this this is great okay and it's going to work for a lot of it but um it's not going to work in one situation okay well it is going to work but it's just not going to work like we want it to anyone situation and that is since this Senter all method that we have in server right this Senter all method since that sends it to every single client in this um where is it where's that in in this client's array list since it sends it to everything it will send it to the The Source okay so in other words if me if I'm talking to someone and I send a message I'm going to get that message back to myself all right now you guys could do a very very simple little thing you could do something along the lines of if our current ID so if our client doget ID you know is equal to say the message. getet ID so in other words when we get the packet when we get the message we could split it we could en enclose the ID of a client and we could do it that way um if that is the case then you know continue um we could do something along the lines of that but what I'm actually going to do is um just make sure that instead of posting our message straight away to the console when we type it we're going to let the server take care of that rather than the client so you guys know right now if we type a message and we hit enter um which happens in over here I think where's our enter key okay if we hit the enter it's going to send this and over here you can see that this send actually goes console message right so what it does is it prints our message to the console let's not make the client do that whatsoever let's delete that entire line and so now we're not going to get any feedback from the client only from the server okay so when we send something to the server it only gets printed in our history if the server replies with it okay and the reason that's really cool is it verifies that there is there is actually connection with the server okay because obviously if we don't get that packet back something's wrong and that that I think is really awesome and that's also good in the sense that if you lose the connection to the server you won't like accidentally send a message thinking the other person got it but they actually didn't okay so this is like a really cheap easy way of handling um and ensuring that there is an actual active connection that's working so um that's pretty much it right this will pretty much work right so if we launch actually our server is still running so we can go ahead and um and launch our client uh login window here twice I'll launch two instances of this right so we've got our first one over here and our second one over here call this one yarn Local Host 8192 this one I'll call Cho Local Host 8192 hit login for for both of them you can see they both successfully connect here which is wonderful and then what what I want to do here is just type in something like hey okay and we get a response we actually get two responses and over here we get two responses okay that's weird uh what's up okay so we're getting multiple responses which is kind of weird okay now we're getting three responses so we must be adding another client somewhere else yeah okay we are let's go back to our um H I wonder why that's happening let's go back to server and I think if we go to clients so we should be adding it right over here right the client are add and we are so maybe we're adding it somewhere else that's probably the best bet and yeah we are okay that's pretty stupid so in the receive thing before after we process the packet which is what's supposed to add it if it starts with C right over here this line um apparently we also do it up here okay that must have been when I was testing it so let's actually just get rid of this client. ad I'm probably going to get rid of this text as well um and that's this text right here is just this text okay uh this one right here right I'm probably just going to get rid of that cuz we don't need it kind of a debugging thing um and then if I run uh let's just restart the server as well here if I run the entire wh if I run the entire thing again so including the server Main and I will also run the okay so that's started um let's run two login instances here or two inst of two instances of the client um and we'll have you know y Over Here Local Host 8192 I am very narcissistic as you guys can see you H Cho all right doesn't matter um narcissist Cho uh it's just a test um okay um all right so hey and as you can see what's up bro we get some perfect kind of correlation here it appear it appears on both clients dude what is even up we should catch up soon anyway I've spent hours personally talking to myself like this cuz it's very entertaining and um that's pretty much going to wrap up this episode okay and you guys as you can see you've got a pretty good connection here as in well in the sense that you've got a um you've got you've got an awesome connection here in between the two client windows and it all works and this will in fact work on your entire network over the entire internet okay if you do start the server on a port such as 81 92 please remember to port forward that through your router if you plan to use it outside of your internal Network otherwise if you're out of blocks that Port which probably will um you won't be able to connect to the server and you'll probably cry so um make sure you do that I might do a bit of a tutorial on setting this up to run on maybe even a Linux server or something along those lines as well as uh as just at home if you want to launch it on your Windows computer at home you know go to school go to university go to work whatever and um and you want to kind of keep that server going at at your house and you want to you want to get people to connect to it um while they're somewhere else then I'll teach you guys how to do that perhaps if you guys want please leave a comment right if you guys want me to do that kind of stuff so that I can see if it's worth my time essentially um and also the other thing is um the one thing that we haven't covered that's really important is the is the disconnection of clients if I choose to close one of these windows and it's gone um the server actually thinks that it's still up so if I do something like hey it's actually going to try and send it to the other server and the pack is going to get lost and there's just no need to do that um and worse still if I connect again we'll have problems okay so currently we have two clients in this array list even though I got rid of one of them so if we go to client window whoops if we go into server this client's array it's still has a size of two right there's still two server client objects in that array even though we've just gotten rid of one of them so we need to deal with closing it okay and the the way that we can see if there's two of them is if we put something along the lines of uh I think put this up here somewhere where is it uh yeah that's what we're going to do in manage clients actually but um we'll talk about that in a minute well not in a minute we'll talk about this next episode but uh this manage clients um is where we're going to be doing the managing okay now the reason we have to manage it is because of um two things one uh when the user closes the program it's cool like right like as soon as they press that little close button you know this little close button this little close button here we can do something right we can actually listen for that and say that okay if the user closes the button then they are disconnecting or if they hit command Q or they go over here and hit quit um we can we can actually manage that right but if they shut down their computer like without closing this or they force a shut down or I don't know the power plug of their desktop pulls out and they've got no battery and it just crashes or they get a blue screen of whatever you know for some reason or they go into Comm out into task manager and they terminate the process for whatever reason if it doesn't close cleanly obviously the application is not going to be able to send that disconnect signal and thus we have to continually check to see if the client is still actually there physically right they might be gone um and that's what we'll cover next episode different types of disconnections but um as for this if we go into I think there's a nice little system.out that prints the number of oh so okay here we go so in while running I've just put a little um system. out. print line clients. size which prints the amount the amount of clients of course I have got two you can see it's printing it here um if I was to let me just pull this up so that you guys can see um you can see it's printing two if and and you know right now of course you just saw me close the other one I've only got one running um if I start another one here and log in uh and I I don't know the Cho this time um local ho whoops Local Host a19 to log in then you can see we've got three now okay we got this little three here and that's uh I don't know if you guys can see that we got this little three here now right and that's bad because that means that it's it's the server thinks there are three clients but there are not so we need to handle disconnection and we'll cover that next episode anyway I hope you guys enjoyed this episode of network chat programming if you did please hit the like button below leave a comment letting me know whether you want to see that kind of server stuff after I cover disconnection next episode and I will see you guys next time oh one more thing I'm going to quickly show you guys this because if you are having problems with the um with the stuff which you might be if you are having problems with these episodes I have a lovely website here called the cher.com if you go to the Forum section um and you go down into network chat programming you can see that few people have posted here um make a new topic here you're going to have to log in here if you go to register if you haven't if you're not a a member yet or go ahead register if you are go ahead go ahead click log in if you're not and then once you go to forums into Network chaper you'll be able to see this wonderful little new topic button click that type out your problem you know include any code you'll have to log in first obviously to post but um you know go ahead and post into that forum and uh and someone will help you okay me probably but um maybe there's there the wonderful thing about this is everyone seems to be helping each other so I don't even have to step in like game programming is obviously the most popular one with like 216 posts now but network chat programming if you guys need help you can see every topic here gets answered which is awesome but um that's going to wrap up this episode hope you guys enjoyed it and I'll see you guys next time goodbye [Music]

Original Description

Twitter ► http://www.twitter.com/thecherno Website ► http://www.thecherno.com Forums ► 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 network chat programming using a client-server architecture, handling multiple clients, and transmitting messages between them. The speaker provides a step-by-step guide on how to establish a connection between clients, handle disconnection, and set up a server on a Linux server or at home.

Key Takeaways
  1. Establish a connection between clients
  2. Send messages between clients
  3. Receive and display messages in the client window
  4. Split the message by '/' and '|' to extract the text
  5. Send the message to the console after splitting it
  6. Launch two instances of the client on different ports
  7. Type a message in one client and observe the responses
  8. Remove unnecessary code and restart the server and clients
  9. Test the connection again and observe the correlation between the two client windows
  10. Listen for the user closing the program to send a disconnect signal
💡 The importance of handling disconnection and setting up a server on a Linux server or at home is crucial for a successful network chat program.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →