Placeholder Image

Subtitles section Play video

  • What's going on?

  • Everybody welcome to another sockets tutorial video.

  • In this video, we're gonna be kind of trying to put everything together and maybe learn a couple new concepts.

  • And what we'd like to do is create a chat application.

  • So we're gonna need a server and then a client, which is pretty common to what we've seen so far.

  • But the difference that we're gonna have here is our server is gonna handle many clients.

  • And when the client connects, basically it's a chat room, right?

  • So the client needs to be able to send messages to the server, and then the server needs to distribute all of the messages to everyone.

  • Okay, And then we also have, like, little stuff like user names and stuff like that.

  • But for the most part, the biggest thing that is gonna be different here is this kind of broadcast or distribution that we've never done and then also just just simply, even even that or also just handling multiple connections.

  • So that would probably be the title if I had to pick one that wasn't like chat, app or whatever is just just handling multiple connections on the server side So anyway, with that, let's go ahead and zoom in.

  • Imagine if I did an entire tutorial that signs I'd have to move my face anyway, uh, import socket, because we're going to use that.

  • And then the way that we're gonna manage many of connections of these connections is gonna be with what's called select, which gives us operating system level io capabilities.

  • So it's like with sockets in mind.

  • So on Windows, it would be different than, say, on Lennox.

  • And because of that Python has select that allows us to utilize that without needing to get into the details so that this code will run the same whether you're on Mac Lennox or Windows.

  • So Okay, then what we're gonna do is gonna have a couple of constants here.

  • So header length will make that equal to 10 and then we're going to set I p OK, I pee.

  • And in this case, we'll do 1 27 001 and then we'll say port 1234 Okay, Now we're gonna go ahead and make socket, so server socket will be socket dot socket and the type its socket a f i net someone told me.

  • Actually, probably multiple people told me, but a F stands for address family, and I believe I net is just like Internet.

  • Anyway, um, the next thing is, this is a socket dot sock lips sock stream stream There we go.

  • We can take today Let me zoom out.

  • Just wanna zoom out enoughto where everything's on screen But then hopefully not too too small, So that should do.

  • Okay, so now, uh, I don't want to do is we want to stop.

  • So if you you may be hit it playing with sockets And I think we hit it in some of the tutorials where I had it keep ticking up the port number so we could rerun things and it wouldn't say addresses and use one option you have to overcome.

  • That is just server socket dot Set sock, opt and then socket dot socket option level Underscore socket.

  • So if I recall?

  • Right, um, you've got, like, three options here, and it's like the thing you want to set.

  • And then what?

  • You want to set off that thing, and then you set the thing.

  • Okay, so we're gonna set this, Um What are we gonna set that?

  • Like what?

  • Attribute of that.

  • Are we gonna set?

  • It's going to be socket dot s o socket option.

  • Ah, re use address.

  • And we want to set that toe one, which is true.

  • So that's a little funky.

  • We're not really used to setting things that weighs in Python.

  • Anyways, um, and I could be totally wrong.

  • What this is going to do is allow us to reconnect, and I believe that's what we're doing.

  • So the next thing is server underscores socket dot bind.

  • And then we just find a cz usual that to pull of I p import.

  • Um, actually, I guess you wouldn't have a space there.

  • It wants a space there.

  • That's hard, because it would be It's a two people inside of a parameter.

  • At least it thinks I should have a space.

  • I'll have a space.

  • Ah, server underscore Socket lips server socket dot lists in, and then we're ready to actually start the code.

  • So really, everything up to this point other than I suppose this here, uh, stuff they should be pretty familiar with you are to you.

  • Anyway, It's been a few days since I've done a tutorial video, Um, getting a little rusty.

  • I ended up having a cold, so Yeah, cool.

  • Fun.

  • Now it's a good time.

  • Um, so now we're gonna start doing the actual server stuff.

  • So the first thing we need to do if we're gonna manage a you know, us Ah, a list of clients is to actually have the list of clients.

  • And really, we don't have clients.

  • We have sockets.

  • So we're gonna say sockets underscore list.

  • Um, and that could be empty, although we actually really have one.

  • And that's the server socket.

  • So just paste in server socket there.

  • Now, later, as clients connect will also have the server.

  • Um, we'll have the clients in this list rather, but for now, it's just server socket.

  • That's the only one we're aware of right now.

  • Now, later, we probably want to think of these clients and at least report these clients to the other clients with something better than their you know, their socket information.

  • So price something other than their I p import or that kind of thing.

  • So what we're gonna say instead is we're gonna have a client's ah clients, a dictionary where these clients sock, It will be the key.

  • And then the user data can be the value.

  • So, um, so we got clients list.

  • Okay, so now we need to do is basically the main thing that, um, the server's going to do, and that is simply receive messages.

  • So we're going to create some space is and let's say, define receive on her score message.

  • And the parameter here will be from any client socket.

  • Now I want to do is trying, um And then except I'm gonna just right past here.

  • We'll fill that in in a moment.

  • So what we want to attempt to Dio is first of all, we want to receive the message header.

  • And that's gonna be a client socket don receive.

  • And then we want to receive whatever the header length is.

  • Uh huh.

  • And that will give us the header.

  • And then, uh, if if not Len message header.

  • So basically, we didn't get a thing, So if we didn't get any data, the client closed the connection.

  • Um, so So we just need to handle for this.

  • So we're just going to say it returned Falls Otherwise, what we want to say is a message.

  • Length equals whatever the int value of message header dot decode utf eight.

  • Um and that's it.

  • Um, just for proper clarity, we could throw the strip in there.

  • So I think I talked about this is well, uh, least in python, and you don't have to strip there.

  • And it feels really weird that you can convert like a number with some spaces to, like a string of a number with spaces to a number s.

  • So you might want to just toss in the strip there.

  • Just just say you understand, because I don't think I think that, like, python would be one of the few languages you're gonna get away with that on.

  • So anyway, you could throw the strip in there yet you don't actually need it in Python.

  • At least the last time we didn't need it, we can We can test that later.

  • You can try to test that later.

  • But anyway, so now, uh, we're gonna return a dictionary where the value is so for Header.

  • We're just gonna say the message header, and then we're gonna pass Data and data will be client.

  • Sock it, sock it dot Receive and then whatever the message length is.

  • So in this case, we're just going to receive exactly, however long that is.

  • And I guess you know, you would just hope that soon it doesn't send some gigantic message, but it's a chat room.

  • So later you could probably just handle for length.

  • Or maybe we will.

  • I don't know.

  • Um, but yeah, you just would have to think about that this forest the size.

  • But hopefully no one's going to send a message that's like, you know, 50 megabytes or something.

  • Anyway, continuing on, uh Okay, So we returned that.

  • Now, if we hit this other exception, uh, there's I mean, pretty much the only way you would hit this is if someone like broke their script or something, like so if the client just, like, closed really aggressively.

  • Um, so in this case, let's just return.

  • Well, I already noticed I return Capital F Falls and let's fix this one as well done.

  • Okay, so now we're able to receive messages.

  • Now, what we want to do is receive messages, and, um, really, there's only gonna be like, two types of messages.

  • There's gonna be on an initial connection and then otherwise, this is just a chat message.

  • So, um, let's do come down here and we'll say wow, true.

  • And we're gonna say, Red Sock, it's, um, thing we don't care about.

  • And then exception sockets, which we sort of care about.

  • But anyway, select, we really just care about the 1st 1 But anyway, that's okay.

  • Select dot select sockets underscore list will just make this an empty list and then ah, sockets list again.

  • So what These are, uh is so select dot Select takes in three parameters.

  • You've got a read list.

  • So the things that you want to read in the right list, so this will be sockets.

  • We're gonna read sockets we're gonna write, and then this would be sockets that we might Iran.

  • And so, uh, and then you can, like, you can decide what you want to do with these errors, but mainly our main concern is this Read list.

  • So this is these are the ones that we actually want to read in and get data from so coming down to the reed sockets, this is where the bulk of our logic will be handled.

  • So what we're gonna say is for notified socket in red sockets.

  • Um, if notified Socket is the server socket, This means someone just connected.

  • Um, and we need to accept this connection and also just, like, handle for it.

  • So the way that we're going to do that is ah, client socket client.

  • Um, address is equal to server socket dot Except so we'll bring in that connection.

  • Then we're gonna say the user is equal to receive underscore message client socket.

  • So, uh, recall that would be here.

  • So we're going to receive that message.

  • Client socket.

  • We get the header data and then we get data data.

  • Okay, So the user were going to say is equal to client.

  • Well, I'm probably making lots of typos here that we're gonna only find out in the next video when we do the, uh, the other side of this the client side.

  • Then we'll run it and then likely debug a lot of things.

  • That'll be fun.

  • So Okay, user received message, and then if user is false, someone just disconnected.

  • So we'll just say continue here.

  • Uh, otherwise sockets underscore list.

  • Donna penned well penned that client Sock it to that list and then we're gonna say clients client underscores socket equals user.

  • So recall user is his dictionary of data here.

  • So you've got the header and then data for that user.

  • So that's the current information we have on this client socket.

  • Basically.

  • And then, um, what we want to say is just we can just gives them simple information, so we'll just say, accepted new connection from Porter I p Colon port.

  • Uh, and then we'll say, user name this.

  • So, um, in this case, I'm gonna make this an F string, and I'm going to say client address zero client underscore.

  • Address one.

  • I mean, just zoom out so everybody can see what's going on here.

  • And then, uh, this would be user danta user data dot d code.

  • Oops.

  • Utf eight.

  • Um, this may be one of the first times I've done embedding inside of quotes.

  • I'm not sure we're gonna get away with that.

  • We may have happened to format it, but I'm gonna stick with that for now on, and we'll see.

  • It'll be fun, So Ah, Okay.

  • He's a new man.

  • Anybody can see that.

  • We're gonna have to zoom back in so we can see again, but otherwise cool.

  • So now what we want to say is so that was like, if someone just, like, just connected now, uh, otherwise, what do we want to do?

  • So we want actually handle for this.

  • So it's a message equals receive message again.

  • Notified socket.

  • So, um, message was received.

  • Mission notified socket.

  • And then if message is false, for whatever reason, we're going to say close connection from on and we will say clients notified socket data dot decode.

  • Mmm.

  • This, uh, utf eight.

  • So the other thing we could news like set this as a value and then pass that through there so you can keep using f strings.

  • I'll be curious to see if this works if you can.

  • Like in bed that way.

  • I don't think I've ever actually needed to do that.

  • So Okay, now, if messages false, great and then sockets underscore lists dot remove notified sockets will get, Take that out of the list.

  • And then we also want to Del clients notified socket and then we'll continue.

  • So that's if the message is false.

  • Otherwise, capitalize that f again.

  • I can't wait to see how many errors of made so far.

  • Uh, now we're gonna say, uh, user equals clients notified socket because we already have this client.

  • Now, Now we're just gonna print um, and f string.

  • I received a message from and we'll say user data dot d code and will decode to utf eight.

  • And, um and then we'll put in the message so we'll say Colon.

  • And then the message itself is message data.

  • Don di Cooke, decode.

  • Get back in ut f eight.

  • Okay, so we see the message.

  • Now what we need to do is share this message with everybody, so I'm gonna say four clients socket in clients.

  • What do we want to dio if clients socket is not our notified socket?

  • So we obviously don't want to send this right back to the center, so we don't need, like, repeat that scent.

  • Um, So if that's not the case, uh, client socket dot Send user header plus user data plus message header, plus our message data.

  • So we send in all that information so both the user name with the header information.

  • And then after that, we send the message with its header information as well.

  • This way on the client side weaken display both user name message using a message.

  • So Well, just know that.

  • Okay?

  • We're gonna accept kind of like to two groups of things the user name data and then the actual message itself.

  • Okay, Now, um, were notified.

  • Socket in red sock.

  • It's the other thing we can say.

  • Two is four, uh, notified socket in exceptions sockets.

  • Again, We can kind of just ignore these, um, but we'll go ahead and, uh, just handle them since we can get this information anyway, so we'll just say sockets list dot Remove notified, underscores socket.

  • And then dell clients notified Socket.

  • Okay, save that.

  • And I do declare, I think we're we've completed our server.

  • So what do you angry about?

  • To blank lines after a function definition?

  • Duh.

  • Everybody knows that.

  • Save that.

  • Uh, I'm just looking for any sin taxi issues.

  • I don't think my pep a thing doesn't seem to understand how f strings work, so I'm pretty sure that's all this is, but this could totally be an issue.

  • We'll figure that out when we actually go to run everything.

  • And in fact, we could actually we could just run it real quick.

  • right now, we don't have to run.

  • Um, we're not gonna connect anything to it.

  • But we could at least, um, I forgive a python.

  • Okay, that's good enough.

  • A python server dot pie?

  • No, we actually do have a syntax air here.

  • Received message.

  • This is what you're saying?

  • Is this in Texas air Really received message from Maybe because we actually haven't hit this pie dish.

  • 37 server dot pie?

  • No.

  • So received message.

  • So it's angry about this one.

  • What have I done?

  • This is like these air are a little ah, compacted.

  • So let's just say, uh, user user name equals.

  • Let's just grab cut paste.

  • Oh, it's the double quotes that I used.

  • Here.

  • Let me fix that real quick.

  • Save that.

  • Let me run that again.

  • Okay.

  • It looks like our server actually runs.

  • We won't know.

  • Uh, we don't actually know if this is gonna wind up airing on us or not doing what we hoped until we actually run it and connect a client to it.

  • But at least up to this point, the code runs so that's good.

  • That's good.

  • Okay, So in the next tutorial, we will work on our client code Quick shadow to my recent, uh, channel members slash longtime members Pop Assani moj on.

  • I'm gonna call you Mohand for short seven months.

  • Jean ends or 10 days new New member.

  • Thank you very much for your support.

  • Jot in non Wani.

  • Also a new member fidget also in Remember Jackson War in two months.

  • And Michael Park, 13 days.

  • Also a new member.

  • Thank you all very much for your sport.

  • You guys are all awesome.

  • Okay, I will see everybody in the next tutorial where we write our client code as well as the bug the like at least 100 errors I probably wrote, but so far it's gonna be a great time.

  • I'll see you guys there.

What's going on?

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it