Ep.12: Receiving Packets - Network Chat Programming

The Cherno · Intermediate ·12y ago

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 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 receive packets of data in Java using the DatagramPacket class and datagram sockets, which is crucial for network programming and Java networking. It covers key concepts such as packet reception, concurrency, and socket programming. By following this video, you can learn how to implement network protocols, handle packet reception, and optimize network performance.

Key Takeaways
  1. Construct a datagram socket and bind it to any available port on the Local Host machine
  2. Pass the port to the login method which launches a new client with Port as a parameter
  3. Create a private method to receive data
  4. Return a string of the received data
  5. Create a DatagramPacket object with an array of bytes
  6. Specify the length of the array of bytes
  7. Use the receive method of the DatagramSocket class to receive the packet of data
  8. Store the received packet of data in the DatagramPacket object
  9. Retrieve the data from the DatagramPacket object using the getData method
💡 Socket.receive will hang the program until it receives data, so it's essential to use it in the right context to avoid freezing the application.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →