what's going on everybody welcome to
part 5 of the sockets tutorial series in
this video we're gonna do is continue
with our chatroom application and this
time we're gonna code the client side of
things and then we're gonna run it and
fix all of these certain bugs that I've
created or at least some of them so the
ones that we find okay let's get started
so what does our client do our client
really does like two things right or
maybe three things so on immediate
connection the client tells the server
what their username is and then from
that point forward it's just like this
infinite loop of if you the client has a
message to send send that message but
also receive messages from the server
now just to keep it super simple we're
going to do everything inside of the
console so we're just gonna use input
which is gonna block the rest of the
program from running so you'll have to
either send an empty message or send a
message to get updates later I would add
some sort of UI to it
I do have planned to do that but that's
not really a sockets tutorial anymore
that's a GUI tutorial so anyway let's
get started so import socket because
obviously import select for the same
reason as before and import errno so
that should be everything we need what
is air know what are we doing we're
gonna use this to match specific error
codes I'll talk about it when we get
there but basically one of the things
that we're gonna do is try to receive
messages until we can't but are when we
know we can't receive them we actually
will get an error and we want to make
sure that air is a really specific error
and not some other error okay so we want
to make sure that error is because
there's no message to receive and not
because who knows something was wrong
with that message or our code or
whatever so anyways we're gonna use this
to match specific errors you'll see it
when we get there then we're gonna have
the exact same starting constants header
length we'll make that 10 IP will be the
same as it was before one 27001
port will make that one two three four
again and then
my username will be input and we'll just
say username so we'll just grab this
from the client basically immediately
when they they join so now we're gonna
create the socket so we're say client
underscore socket equals socket socket
and then its socket dot address family
Internet
I assume or I net in this case socket
dot all caps sock underscore stream okay
great
client socket good now let's go ahead
and connect to IP import so clients
socket connect to the tuple IP port and
then we're going to say client socket
dot set blocking to be false
this way the receive functionality won't
be blocking all right so set blocking
false the next thing that we want to do
is begin to send in this this
information to the server so like I said
before this does like three major things
the very first thing that we're going to
do is send in that username for the
server so recall somewhere here I was
hoping to user equals receives it's the
first message yeah if notify yeah so if
this is the server socket this is the
brand new user and then this is where we
set that user and stuff like that so
like at this state this is where we are
please don't move that in this inner
like if we're looking at where server is
gonna first interact with client would
be here well sort of but at least at
least this code cuz we actually we would
first interact really here anyways
continuing on so what we want to say
here is user name will be equal to my
user name dot in code
utf-8 then user name underscore header
will be equal to an F string of the Len
of username okay : less Stan and then
header length in this case which is 10
header length as always screws me like
this is hard enough to remember how to
do and then you add f string to it and
it looks goofy but anyways I think
that's good so we will encode utf-8 okay
that's our username in the header
so now what we're gonna do is client
underscore socket dot send username
header header plus whatever that
username is is that really not a pet
violation - and what if I put the space
is that gonna be a pet violation no I
can do whatever I want huh interesting
actually don't have the answer to that I
think you'd probably want spaces around
your pluses that's my guess anyway so we
send in that information once we've done
that that's like the only thing that
isn't gonna be in the loop like we only
need that information one time we're
just not gonna let people change their
username so the next thing that we're
gonna do now is just iterate forever so
while true tour while true what we want
to do here is both send messages and
receive messages so the first thing I'm
going to do here is say any message that
we input is just going to be an F string
that's also what's input F string and
then we'll just say the username so my
underscore username so this is just kind
of before the person types anything it
will just kind of it'll just look like
their own username then they'll type and
it'll look really pretty and stuff
it'll give them the the feel of a GUI
even though it's really really not okay
so now someone might just press Enter
so just to get it an update right just
to get past input so someone might or
they might accidentally hit in our who
knows so what we want to do is be able
to handle for whether or not someone
actually input anything at all so the
first thing that we're gonna ask is if
message so if this is not empty there we
go
if if message then message will be equal
to message dot encode and we will encode
that to utf-8 bytes and then what we're
gonna do is we need to set message
header message header and that's equal
to the F string of again Len oops
Len message message
[Music]
and then here :
less than.this always bothers me to like
throw this so like this one will wind up
the same way so hopefully header length
okay dot in cups dot encode to utf-8 I
don't know if we'll get if we get away
with having spaces there I kind of want
them but I'm not sure if that will work
for the string formatting okay so that's
our message header and now we want to
send that message so client socket dot
sand message oops
message header plus whatever that
messages okay so that's us
sending in the message if we actually
sent one but again we might actually
just press ENTER here just to update the
chat so regardless of whether or not
there is a message the next thing we
want to do is try to receive all of
these messages so this would be a while
true loop that we're going to receive
things and actually I before E except
after C fellas okay so we're gonna try
to receive things here but like I said
we're gonna do that basically until we
hit an error so because that's gonna be
our logic we're actually going to
purposely encase this because we're
expecting an error at some point for
sure
so try accept and then for now a pass
but we'll fill that in in a little bit
but while true we are going to attempt
to receive things so first of all we're
gonna say user name underscore header
equals client socket dot receive header
length and then if not Len username
header whoops
Amen if not Len username header means we
basically didn't get any data for
whatever
reason we're going to say print
connection closed by the server and then
we run sis dot exit and import sis okay
so other than that we want to convert
that that username header to an int so
what we're gonna say is user name
underscore length equals the int value
of username header dot decode utf-8 and
then again like before we'll throw in a
strip because normally you'd have to do
that but I don't think you do here
username length okay so we've got the
username length now what we want to grab
knowing that header you know what the
length of this message of username alone
would be we can actually grab the user
name here so that's going to be client
socket dot receive username length dot
decode utf-8 utf-16 here recall when we
send information we're sending this
right we're sending the the user the
user header followed by the username and
the header just contains how long is the
username and then in that exact same
message here but just right after it
we're gonna receive the message header
and data and now recall this is coming
in a stream so we've accepted this exact
amount we've accepted the exact amount
that is the header data then the header
then the username data based on that
head username header data let's call it
and then again now at the point of data
will be exactly at the header for the
actual
itself so now we're basically going to
do the exact same thing above so message
header equals client socket dot receive
header length so we do basically this
exact thing then we're going to come
down here and and continue that so now
know what did I do here username header
username length equals int username here
username header yeah that caught my eye
so at least we solved one bug already it
was like wait you can't do that using
the length okay then where you seen the
message okay cool
so we're going to basically do the exact
same like these three lines here so the
message won't be sent so in this case
this would be if something went wrong
there but in this case we shouldn't need
that same error so message header now
what we need is m/s length and the
message length is the int version of the
message header dot decode utf-8 and
strip so decoded it that's what that
word was in case anybody wasn't sure
okay message length and now we finally
can grab the actual message itself which
will be exactly this right here copy
pasta change to message length dot
decode utf-8 okay so we have the
username and we have the message hmm so
all we're gonna do now is print F string
and that will be the username followed
by whatever the messages
I just can't wait to run this and see
how much trouble were in
okay so basically gonna have like a
couple of exceptions so except we'll
just do exception as E and then we'll
just print string B and we'll call this
just general error cystic so then we'll
come down here and we're gonna accept an
i/o error as E except IO error capital e
was waiting for the color okay except IO
error as E now if e dot air know is not
equal so we've got two options here that
we might see it would be air node dot e
again and then if it does not equal that
or a dot air no does not equal air node
dot e would block so again these are
these are the errors that we might see
depending on operating system when there
are no more messages to be received so
in this case we are we're like we don't
really want to handle for this
necessarily because we're expecting this
so it but if it's not one of these
things then we'll just say print string
e and we'll just say some sort of
reading error and then whatever string E
is that way we know where we hit an
issue and then also let's just run us
this exit here because we're we're done
so otherwise if we hit it and it is one
of these things we don't care all we
really want to do is continue cool
so with that we are presumably done but
I can almost guarantee you we aren't
done so let's go ahead and run the two
things here so CMD Python what is it
will do server dot PI so get that
running I'll put this over here and then
CMD Python client dot PI will say
username is H so as soon as I connected
we at least got the accepted new
connection hello world figured I could
not be closed connection from H and then
also a reading error on the part of
client could not be completed
immediately
so the first thing I'm going to do is
head over to it's hard to save in this
case I'm it could be actually something
that we type out in server but we'll go
to client so we said hello world server
didn't actually see it but we're
actually checking for the read ok so the
issue is yeah in this case it's not an
either/or situation it it's an and
situation so if you put an or here
obviously you're always gonna get it so
instead it needs to be in hand so if the
error is not this and it's not this then
we go here because this is for deferring
by operating system that's the whole
reason why we have this so not an or and
ant so save that and your server
actually should you should be able to
keep your server running and then just
go ahead and rerun Python client PI will
use H again and ok so we've got a new
message hey okay okay great so bringing
over the server now we see okay we got
the new connection the username is H
received message from H hey
okay so then we could add in one more
user here Python client PI will say
username is C hey many exclamation marks
and if we come over to this one this is
our username H and like we either well
first of all the server got it but also
here we're not gonna get it because
we're being blocked by input so if we
just enter here we'll see okay we'd
actually were able to receive the
message from C immediately and if we did
not take any input we would see things
immediately so for example just for the
fun of it because it's just so fun one
thing you could say or do would be like
for the first guy let's see client or
girl who knows who there
we could just say message equals that
maybe get away with that Python client
dot PI will call this user reader and
then we'll come back over here
and now I actually bring the messages
back yes the Python client PI see hey
there so this here obviously our server
basically does it but this one just
cleaned up a little bit just to show you
that a client other than if you just
commented out the you know the message
input the client actually does receive
in a stream of data here so see hey
there and then we could open up another
one Python client PI username h-hey
what's okay then you get hey what's up
much okay anyway you get the idea so
actually really surprised that I didn't
type out anything along the way and
those funky F strings worked on the
server so that's cool okay so really
that's it I'm the next thing you would
do is make some sort of real UI and not
actually use the command line just
because it actually really is difficult
to put things into the command line and
have that be updated live yes there are
ways but it's all operating systems
specific and I just didn't want to do
that that much whereas you could use
something like a GUI you know like
tkinter PI QT or whatever and you just
wouldn't have that problem anyways so
yeah I think that's probably the next
step would be to do something in a UI
instead so as you might be able to guess
I will plan to be doing that so so
anyway if that's what you want to see
stay tuned for the future otherwise
questions comments concerns whatever you
can leave them below quick shout out to
my recent channel members Michael
Ivanoff for seven months that's a really
long time
thank you very much Chong sampark new
member thank you
Lily Zi also a new member a you mix also
a new member I sort of recognize that
name I think that you're in discord or
something that's probably why I
recognized I named Dave Senta four
months as well thank you very much all
of you guys for your support you guys
are freaking amazing
okay questions comments yeah all that
below if you got suggestions anything I
did wrong here leave those below also if
you can think of a nice clean way to
make this operating system agnostic but
also update live including message input
I'd love to see it I just it's like a
huge rabbit hole that I don't really
want to go down for this but anyway
that's it I will see you guys in another
video