Python Online Game Tutorial #3 - Sending & Receiving Information From Server

Tech With Tim · Beginner ·🧠 Large Language Models ·7y ago

Key Takeaways

This video tutorial covers creating a client-server connection using Python sockets, handling network communications, and sending/receiving information between the client and server. The tutorial uses the socket library and creates a Network class to manage connections.

Full Transcript

hey guys and welcome back to video 3 so what we're gonna be doing in this video is we're gonna be coding the kind of client side of this so connecting to the server I know we already coded a client but we're gonna code like the network aspect of the client so that it can connect to the server it's not as much code it's a bit more straightforward and then we're just gonna test out sending very basic information to the server and hopefully getting some back see if that's working ok and then in the next video we're gonna be connecting this so like the little user interface we created with moving that block around in the first video we're gonna be connecting that so that we can have multiple clients running and we can see like different blocks moving on each screen ok so that will involve a bit more work hopefully by video 5 we'll have like a fully working kind of game that's working over the network ok that's the goal so within this video I just want to start by saying on the server class here or sorry server file I did actually forget two lines of code in the last video so after this except break in threaded client just need to add this print loss connection and then connection dot close all this is doing is once we break out of this threaded client we're just letting the we're just gonna print this to the console so we can see what it looks like and then we're gonna close that connection so that we can possibly reopen it in the future okay really straightforward that's all you gotta add so just make sure you add that before moving on oh yeah so next we're gonna do is we're gonna create a new file and I'm gonna call this network okay now you don't have to put what I'm going to in a new file it's just a lot easier so that's why I'm going to do that so let's do network and in here we're gonna import socket now what I'm gonna do is I'm just gonna code a class it's gonna be responsible for connecting to our server this just makes it like so I can reuse this class in the future and you guys could reuse this class in the future and it's just bit cleaner and nicer and that's why I like to do things so I'm gonna say class network again call us whatever you want and in here we're just gonna set an initialization function we'll take actually as Network do we need anything and network now I think we'll just leave like that so we're gonna say self dot clients equals socket socket it's gonna be the exact same arguments as last time so it's a socket dot if' net I think is that what it is i F underscore net af underscore inodes okay and then we'll do socket dot sock stream like that okay now we're going to define the server and the port again so self dot server equals self thought port equals five five five five now for the server this again this number has to be the same as the one you used in the server what do you call it the server file here so no matter what like no matter where you actually are like what client you're using this number is going to stay the same because this is the server you're connecting to it's not the clients address we don't actually have to define the clients address anywhere it'll automatically get that for us okay so what we're gonna do now we have clients server port we need to do this self dot addr equals and then we're gonna say server so self thought server and then self dot port in here okay and then self dot what he called self thought ID is gonna be equal to self dot connect okay now you guys actually let's just do self dot connect right now and we'll talk about why I was gonna have this ID later once we add that functionality the server can essentially I wanted to have an ID that was returned here alike that was stored in this network object just because we are gonna have to like be sending an ID to each of our clients so they know if they're like player one or player to but we'll do that later because we don't really we can't really do that yet okay so we've called this connect method so we need to write that now so let's say define connect okay and in here I believe we should probably be given actually I think it's probably fine just use self right now what we can do is we new self dot clients dot connect okay and then in here we knew self dot addr now we're gonna throw this in a try and except just in case you know this isn't working so we'll say try say accept and is a renew socket air let's say accept and we'll just pass right there okay I just in case this doesn't work so we'll try this we'll try to connect accept pass now once we actually connect what we're going to do is we're return I'm just looking at my other screen right now a self-taught client don't receive and then we'll do 2048 dot decode okay so what this is gonna do I'll talk about this because it might be a little bit confusing is when we connect we want to actually send some kind of information immediately back to the object that connected to us so like for example it's going to I didn't mean to close that what's going to server here and you can see when we initially connect we're not sending any information until we receive something now that's fine but when we connect we should really send some kind of like validation token or like ID back to our network object or back to our client so what I'm gonna do in here I'm just like on dot send okay and then in here what we're gonna type is let's see what should we really type here uhm STR dot in the code then here we'll say connected like that okay just so we know that we did indeed connect if that means if we set this equal to a value so self thought I don't know let's say ID equals self dot connect what'll happen is when we connect will return that string connected so this will get connected and since it's encoded we need to decode it obviously so if we want to print actually in here self dot ID it should say like connected okay let's see if that works okay so now that we actually have that let's see if we can connect to the server and then we'll deal with sending information from the client to the server as opposed to just getting it from the server okay I know it's a bit confusing right now I'm just turn to figure out the best way to do this so let's try this guy so we're gonna say N equals Network I will just type this at the bottom of the script we won't do this after will delete this just for testing purposes and then we're just gonna say actually that's probably all we need to do because it'll just print our ID okay so let's create a configuration for a network so nuke it configuration type network in here and then select that path and then let's test if this is working so remember what I said when we're gonna connect right to our server what we have to do is first run that server script so let's run the server script here waiting for a connection server started okay let's run this network script now invalid argument was supplied s not listened to hmm okay so I had a quick look here I realized the mistake was I was actually running this server two times so obviously that's not gonna work for us so we can put two back in this listen I'm not sure if you guys will see if I left that in the video or not but let's go to network and we also need to change this to AF instead of AF I don't know why typed I fi literally set AF when I was typing it but that's fine and we have server I'm actually running it right now so make sure you guys run that and then they'll run that we can see if this works okay sweet so I know it doesn't seem like much but you can check here if we go to server it says connected to and then gives us that address it's also printing out some other thing that I honestly don't know what that means and then saying disconnected and lost connection sweet that's actually really good and that means that were everything is working and you can also see that we have connected being printed here so that means when we connect to the server we're actually getting the value that is sending so we're getting this connected and we're printing it out decoding it all fine so that's really awesome so now the only next step is to actually send information to the server and keep like a loop going like sending receiving sending receiving sending receiving now let's actually just test um if I run this again you can see again obviously it's working again so this is the server just continually is running I don't actually have to stop this unless I want to make modifications to it so let's just keep that running for now and let's add something to our network class so in our network class we're gonna define a method that's gonna be sent now this method is gonna be very useful later on because it's gonna save us a lot of time so in Santa bridge can take a string which is gonna be data okay and all we're gonna do in here is we're going to try to self dot client dot send and I'm gonna say data STR actually dot in code Donna and then we're gonna get a reply from that server so we're gonna say return so exact same thing that we're doing here self-taught client I receive twenty 48.2 code and will accept and I believe this is a socket error yeah the socket cut error as E and just print e so if we get into some error where you know either we're sending or we're receiving it's not working let's just print that Arab to the screen so we have a look at what that really means so now let's do a test and try to send information to the server and then get something back okay now the information that we're getting back should just be the same information because that's what we have in server we're just gonna send exactly what we got right back right so doesn't make sense right now to do that but we'll talk about like what we can what valid information we can send how to send receive information in the next video when we connect up the client to the thing okay so what we're gonna do is gonna say and not send and we'll print and not send okay and we'll print and not send again and let's just send like a few bits of information and see if everything is indeed working ok so n dot send and then in here let's just type hello and we'll type working okay and see if this works so we have actually you should have yeah server is working well let's run our client and you can see we get connected hello working and if we come here it says received hello sending hello received working sending working so that's awesome that means our network class is working sending information receiving information is working server is working and now the only thing that's left to do essentially is to connect that up to this so use this network class in some meaningful way here and then to actually store information on the server and then send that information to multiple clients which we'll doing in future videos so that's gonna be it for this video we got the network class working hooked up to the server I find this stuff really cool personally like I'm really excited about doing this and showing you guys how to do this I think it's really awesome that within like a few minutes you can just set up a basic server on your local network and send or receive information and it's only like how many lines of code is this like a hundred not very many but anyways let me know what you guys have thought of this video and if you're excited for the next one and I will see you there [Music]

Original Description

This python multiplayer game tutorial covers how to send information to the server using python sockets. It also covers how to receive information from the server. In the next video we will connect the graphical aspect of the game to the server. Source Code: https://techwithtim.net/tutorials/python-online-game-tutorial/sending-receiving-information/ ⭐ Kite is a free AI-powered coding assistant for Python that will help you code smarter and faster. Integrates with Atom, PyCharm, VS Code, Sublime, Vim, and Spyder. I've been using Kite for 6 months and I love it! https://kite.com/download/?utm_medium=referral&utm_source=youtube&utm_campaign=techwithtim&utm_content=description-only ◾◾◾◾◾ 💻 Enroll in The Fundamentals of Programming w/ Python https://tech-with-tim.teachable.com/p... 📸 Instagram: https://www.instagram.com/tech_with_tim 🌎 Website https://techwithtim.net 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-rusci... 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 💵 One-Time Donations: https://www.paypal.com/donate/?token=... 💰 Patreon: https://www.patreon.com/techwithtim ◾◾◾◾◾◾ ⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡ Tags: - Tech With Tim - Python Online Game Tutorial - Python Tutorials
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches how to create a client-server connection using Python sockets and handle network communications. It covers setting up a server, connecting to the server, sending and receiving information, and handling potential connection issues. The tutorial provides a basic understanding of network programming and client-server architecture.

Key Takeaways
  1. Create a new file called network.py
  2. Import the socket library and create a Network class
  3. Set up the server and port, and get the client's address and ID
  4. Call the connect method to establish a connection
  5. Define a method in the Network class to send information to the server and receive a reply
  6. Try to send information to the server and receive a reply
  7. Print the socket error if there is an error sending or receiving information
💡 Using the socket library and creating a Network class can simplify handling network communications and sending/receiving information between the client and server.

Related Reads

Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →