what's going on everybody welcome to
another sockets tutorial video in this
video we're going to 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 usernames and stuff
like that but for the most part the
biggest thing that is going to 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 size I'd
have to move my face anyway
import socket because we're gonna use
that and then the way that we're gonna
manage many of connections of these
connections is going to be with what's
called select which gives us operating
system-level IO capabilities so spent
like with sockets in mind so on Windows
it would be different than say on Linux
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 Linux or Windows so okay
then what we're going to do is we're
gonna have a couple of constants here so
header length we'll make that equal to
ten and then we're going to set IP okay
IP and in this case we'll do one 2700
one and then we'll say port
one two three four okay now we're gonna
go ahead and make the socket so
serversocket will be socket dot socket
and the type is socket da 8f eye net
someone told me actually probably
multiple people told me but a F stands
for address family and I believe inet is
just like Internet anyway the next thing
is this is a socket dot sock whoops sock
stream stream there we go we can type
today let me zoom out I just want to
zoom out enough to where everything is
on screen but then hopefully not too too
small so that should do okay so now what
we want to do is we want to stop so if
you've maybe hit it playing with sockets
and I think we hit it in some of the
tutorials where I had to keep ticking up
the port number so we could rerun things
and it wouldn't say addresses in use one
option you have to overcome that it's
just server socket dot set sock opt and
then socket dot socket option level
underscore socket so if I recall right
you've got like three options here and
it's like the thing you want to set and
then what you want to set of that thing
and then you set the thing okay so we're
gonna set this what are we gonna set
that like what attribute of that are we
gonna set it's gonna be socket dot s Oh
socket option re use address and we want
to set that to one which is true so
that's a little funky we're not really
used to setting things that ways in
Python anyways 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 underscore socket dot bind and
then we just bind as usual the tuple of
IP import
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
tuple inside of a parameter at least it
thinks I should have a space I'll have a
space server underscore socket whoops
a server socket dot listen and then
we're ready to actually start the code
so really everything up to this point
other than I suppose this here it's
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 getting a little rusty I ended up
having a cold so yeah cool fun now it's
a good time so now we're going to start
doing the actual server stuff so the
first thing we need to do if we're gonna
manage a you know 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
and that could be empty although we
actually already have one and that's the
server socket so just paste in server
socket there now later as clients
connect we'll also have the server 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 probably
something other than their IP and port
or you know so that kind of thing so
what we're gonna say instead is we're
gonna have a clients a clients a
dictionary where these clients socket
will be the key and then the user data
can be the value so so we got clients
list ok so now we need to do is
basically the main thing that the server
is going to do and that is simply
receive messages so we're going to
create some space and let's say
fine receive honor score message and the
parameter here will be from any client
socket now we want to do is try and then
accept I'm gonna just write pass here
we'll fill that in in a moment so what
we want to attempt to do is first of all
we want to receive the message header
and that's going to be a client socket
dot
receive and then we want to receive
whatever the header length is and that
will give us the header and then 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 so so we just need to handle
for this so we're just gonna say return
false otherwise what we want to say is a
message length equals whatever the int
value of message header dot decode utf-8
and that's it just for proper clarity we
could throw the strip in there so I
think I talked about this as well at
least in Python 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 so you might want to
just toss in the strip there just just
so you understand because I don't think
you'd I think that like python would be
one of the few languages you're going to
get away with that on so anyway you can
throw the strip in there yet you don't
actually need it in Python though the at
least the last time we didn't need it we
can we can test that later or you can
try to test that later but anyway so now
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 socket socket dot
Seve 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 it doesn't send some
gigantic message but it's a chat room so
later you could probably just handle
four lengths or maybe we will I don't
know but yeah you just would have to
think about that as far as the size but
hopefully no one's gonna send a message
that's like you know fifty megabytes or
something anyway continuing on okay so
we return that now if we hit this other
exception 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 so in
this case let's just return though and I
already noticed I returned capital F
false 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 really there's only
gonna be like two types of messages
there's gonna be on an individual
connection and then otherwise this is
just a chat message so let's do come
down here we'll say wow true and we're
gonna say read sockets 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
first one but anyway that's okay select
dot select sockets underscore list we'll
just make this an empty list and then
sockets list again so what these are 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
we're gonna write and then this would be
sockets that we might air on and so and
then you can like you can decide what
you want to do with these airs 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 read sockets
this is where the bulk of our like logic
will be handled so what we're gonna say
is for notified socket in read sockets
if notified socket is the server socket
this means someone just connected and we
need to accept this connection and also
just like handle for it so the way that
we're gonna do that is a client socket
client address is equal to server socket
dot accept so we'll bring in that
connection then we're gonna say the user
is equal to receive underscore message
client socket so recall that would be
here so we're gonna receive that message
client socket we get the header data and
then we get data data okay so the user
we're gonna say is equal to client
underscore so I'm probably making lots
of typos here but though we're going to
only find out in the next video when we
do the 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 receive message and then if
user is false
someone just disconnected so we'll just
say continue here otherwise sockets
underscore list dot append will append
that client socket to that list and then
we're gonna say clients client
underscore socket equals user so recall
user is this 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 what we want
to say is just we can just give some
simple information so we'll just say
accepted new connection wrong poor IP
colon port and then we'll say username
this so in this case I'm gonna make this
an F string and I'm going to say client
address 0 client underscore address 1
let me just zoom out so everybody can
see what's going on here and then uh-huh
this would be user data user data dot d
code oops utf-8
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 end up having to format it but I'm
gonna stick with that for now and we'll
see it'll be fun
so okay zoom at so 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 Ries like just connected
now otherwise what do we want to do so
we want to actually handle for this so
it's a message equals receive message
again notified socket so message equals
receive message notified socket and then
if message is false for whatever reason
we're going to say closed connection
from and we will say clients notified
socket data dot decode mmm do this utf-8
so the other thing we could do is like
set this as a value and then pass that
through there so we can keep using f
strings I'll be curious to see if this
works if you can like embed that way I
don't think I've ever actually needed to
do that so okay now if message is false
great and then sockets underscore list
dot remove notified socket so I'll get
take that out of the list and then we
also want to Dell clients notified
socket and then we'll continue so that's
if the message is false otherwise
capitalized that F again
I can't wait to see how many hairs I've
made so far now we're going to say user
equals clients notified socket because
we already have this client now now
we're just going to print an F string
[Applause]
[Music]
received message from and we'll say user
data dot D code and we'll Dakota to you
utf-8 and and then we'll put in the
message so we'll say : and then the
message itself is message data dot d
cook decode oops
get back utf-8 ok so we see the message
now what we need to do is share this
message with everybody so I'm gonna say
for client socket in clients what do we
want to do if client socket is not our
notified socket so we obviously don't
want to send this right back to the
sender so we don't need to like repeat
that send so if that's not the case
client socket dots end user header +
user data + message header plus our
message data so we send in all that
information so both the username with
the header information and then after
that we send the message with its header
information as well this way on the
client side we can
display both username message using a
message so we'll just know that okay
we're gonna accept kind of like two two
groups of things the username data and
then the actual message itself okay now
for notified sockets in red sockets the
other thing we can say too is for
notified socket in exception sockets
again we can kind of just ignore these
but we'll go ahead and just handle them
since we can get this information anyway
so we'll just say sockets list dot
remove notified underscore 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 are
you angry about two blank lines after a
function definition duh everybody knows
that save that I'm just looking for any
syntax e issues I don't think my PIP 8
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 we're not going to connect anything
to it but we could at least I forget
what Python okay that's good enough
Python server dot PI no we actually do
have a syntax error here received
message this is what you're saying is a
syntax error really received message
from
maybe because we actually haven't hit
this pie - 3 7 server pie know so
received message so it's angry about
this one what have I done this is like
these are a little compacted so let's
just say user username 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
ok it looks like our server actually
runs we won't know 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 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 shout out to my recent
channel members slash longtime members
papasan e Mohan I'm gonna call you Mohan
first short seven months gene ends or
ten days new new member thank you very
much for your support
Jatin nandwani also a new member fidget
also a new member Jackson Warren two
months and Michael Park thirteen days
also a new member thank you all very
much for your support you guys are all
awesome ok I will see everybody in the
next tutorial where we write our client
code as well as debug the like at least
a hundred errors I probably wrote but so
far it's gonna be a great time I'll see
you guys there