Ep.12: Receiving Packets - Network Chat Programming
The Cherno
·
Intermediate
·12y ago
Skills:
Network Security60%
Key Takeaways
This video demonstrates how to receive packets of data in Java using the DatagramPacket class and datagram sockets, covering key concepts in network programming and Java networking.
Full Transcript
hey what's up guys My Name Is AO and welcome to episode 12 of network chat programming So today we're going to more or less talk about receiving data okay not sending receiving data now the way that uh the way that this works is um is uh see the thing is I I feel like I should do an in-depth episode that really explains things like ports and everything too because I know that a lot of you guys aren't networking Pros um and it does certainly help to know uh you know what a port is what an address is how um you know what sockets are and how that stuff interacts but um for the sake of time I'm going to kind of skip over a lot of this stuff I will uh explain it roughly but um again I I may make a in fact I I'm pretty sure I will make basically just oneoff episodes that cover all these Concepts such as uh ports for example or sockets and how they all interact but basically what happens is um uh this doam sock that this doam sock that we've created that's responsible for um managing I guess a connection to the network I talked about this last episode but you can see over here in the Constructor we've left blank and that's absolutely fine by the way um what this will do is um if we if we actually Mouse over we'll see the nice Java dock here it will say it it tells us that it constructs a datagram socket and binds it to any available port on the Local Host machine now the reason I've left it as that is because um occasionally what will happen is uh well well essentially what what will happen is um ports might not be available so in other words on on your computer you know the port 25560 might uh might not be available but it will be online because you might be running running an application or you've written your own application that uses that Port so it's advisable uh to not do that but then again um you know we are uh we are if if we go back to this login uh Java and we actually take a look at what it looks like uh in the GUI side um I probably should have just opened the application would have been faster than this uh passing thing um sorry my Mac isn't very fast it's a MacBook Pro from 2010 so give it a Break um okay so um we've got this port right and that's there for a reason right I'm surprised actually no one pointed this out so what what we do is when we actually do hit the login button what we do is we we grab the port here and we pass it on uh to the login uh method here which in turn actually launches a new client with Port as a parameter we're not doing anything with that parameter at the moment we're passing it into to this uh connect um thing over here but we're not actually using it here so let's just put it into the parameter here and what this will do is this will actually construct us a datagram socket based on that Port so instead of being any available Port it's going to be that specific Port okay and that helps us a lot so now let's talk about how receiving works okay because that's uh um to say the least that's um not the easiest thing to um to talk about but uh I think it's I think sending is actually actually a lot easier than receiving to be honest but um regardless we're going to do receiving first so uh we'll make it uh we'll make it private because we don't need to make it public uh private string receive whoops okay and it's not going to take any parameters but it is going to return a string and the string will be the thing that we receive so I'm doing receiving first and not sending um and the reason I'm doing that is because um sending uh sending is a bit more involved as in we we actually handle sending we we more or less with sending we handle sending in the act in its entirety in that actual method whereas receive we kind of do half the stuff in the method and half the stuff when we call the method so that'll make sense to you eventually but um for now we need a way to receive something how does that work well first of all I didn't mention packets didn't I packets are essentially just grp groups of bytes they're groups of data now to receive um to receive this data this packet in uh in uh using the datagram socket here we actually need to make an object called a datagram packet okay so datagram packet uh we we'll just call it packet oops doagram packet packet equals new doagram packet and then I'll it'll take two parameters for us it'll uh it will only take um okay so it has to take an array of bytes okay so in other words when we're receiving a packet of data we're receiving an array of bytes okay um so you know in in Java and in other programming languages there are there there's a vast amount of uh methods that actually do things like strings into bite conversion so in other words if we want to send send the message hello we don't have to know you know the the new numerical value of that we can simply use this string. two byte or whatever it's called two byte array or two bytes I think it is um to actually convert that string into bytes but since we're receiving we'll just make an array of bytes here so byte data equals new bite and we'll set it uh we'll make 1024 of them okay we'll make 1,24 that's 2 to^ of 10 um bytes okay so that that should be enough okay you you could make this bigger but the thing that you would argue as well is that you really don't want to be sending packets that are more than this size um I do realize that Java will probably split them up for you but still you don't want to keep track you don't want to keep track of more than that many bytes um which is essentially a kilobyte of data here um you know as you as you send it so uh okay so now that we've made this we can pass in data as a parameter and we also need to specify a length Okay so in other words this is kind of like the um the range of the of the bites that we actually want to send we want to send all of them right so or receive sorry we want to receive uh the entire um we basically want to fill the entire array with data here so we'll just type in data. length Okay cool so let's import this if we just hover our Mouse over here Eclipse will do that for us so it's located in java.net datagram packet and uh and there we go no errors awesome so now that we've got that we actually need to receive this um data somehow okay this isn't receiving data what this has done is it's created a packet this packet is currently empty we need to fill it with data we can do that by calling socket which is our datagram socket which remember is our actual connection to the network socket. receive and then we actually need to dump the data somewhere so we put it into our packet okay awesome so assuming that this does actually receive something we get a packet of data away from this so we need to try and catch this because um it does throw an IO an IO exception as uh as you would imagine because it is to do with input output um let's import that hang on a minute yeah cool so we'll uh I guess we'll be nice and the stack Trace Okay cool so now that we've done that um we've essentially got this packet of data now but what we kind of want to put it into and is a string format right because we're returning a string so we'll make a brand new string here and we'll call it uh message because you know we will be receiving messages as the entire point of this application and we'll make it equal to new string okay packet. getet data and that's it okay so it's just new string packet. data very very simple so what packet. getet data will do is essentially as you can see return an array of bytes so what we've done is we've created an array of bytes okay think of it as we've just created a bunch of data okay one kilobyte of data here we've um we've made a packet to sore that data okay we've we've actually we've listened and we've received a packet from the network and remember this will actually hang the program this is the other important thing this soccer. receive will sit there until it receives something okay which is why we're going to talk about threads in the next um episode but this will actually this receive call right here this will actually freeze our application okay because what it's going to do is it's essentially a what is what's I'm not sure if I can show you this because I don't think I don't have the source attached here but um essentially what soccer. receive uh is is it's like a wild true Loop almost it's not really while true but it's a while loop that keeps going until it actually gets some data from the network onto this specific Port that we've bound here when we created this doagram socket which is right over um here actually you know what let's put receive Let's uh let's put receive below the um the open connection method here okay so we've created this doagram socket on this port and this receive method will actually just it'll just wait for data on that Port if the data never comes just until we close the application it's never going to stop waiting um and then once it does receive something okay once it does receive data it's going to pop it into this packet okay and then we can use packet. get data to get that original array that is now filled with bytes and what we're doing here is we're creating a new string out of it so we're essentially converting these bytes into a string and then finally we'll return the string so we'll return message okay awesome so that is the receive method okay it's a bit um maybe a bit complicated here but uh I think I think it's probably a bit more complicated um than the send method cuz send is easy right all you do is you make it you make a uh you essentially just make a um you get you get your data that you want to send and then you just send it uh to an to an address and to a port and it's really easy but here there's this whole aspect of how sock. receive works so if we were to do something like put receive into um I don't know into open connection or something we'll just put receive somewhere if if we were to do that it's actually going to essentially freeze our application okay because remember with concurrency I really don't want to talk about this now I should talk about this in the next episode but what's going to happen is this soccer. receive is going to take up the entire process because of course when we execute commands you know when when the computer executes our code it goes sequentially when it gu the socket. receive it says okay there's pretty much a while loop there so I'm going to wait until I get data okay and it's just going to sit there until it gets its data it's not going to execute the rest of the program so we'll be in trouble there but luckily threads will come to rescue in the next episode when we implement the send method so I hope you guys enjoyed this episode of network chat programming if you did please hit the like button 200 likes one video per day 300 likes two videos per day okay so again if you did enjoy the video please hit the like button and I'll see you guys next time goodbye [Music]
Original Description
200 likes = 1 video per day, 300 likes = 2 videos per day!!!
-------------------------------------------------------------------------------------------------
In this video:
- How to receive packets of data in Java
- Java networking
- The DatagramPacket class
- How to use ports
-------------------------------------------------------------------------------------------------
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Network Security
View skill →
🎓
Tutor Explanation
DeepCamp AI