what's going on everybody welcome back
to another sockets with Python tutorial
video in this video what we're going to
be talking about is basically just
building on the last video where we
reached the point where we realized
things are still a little clunky when we
have messages that are bigger than the
buffer size that we selected but also
how do we maintain this stream of data
so because we're finding that the only
way that we can finally get our full
message is to issue a closed which
closes the socket and ends the client so
we'd have to rerun the client obviously
doesn't work so we did cover everything
you need to send and receive data via
sockets so I'm sure lots of people are
gonna leave us behind be like I've cut
everything I need but you probably don't
because it's gonna get a lot more
complicated than this and or you're
gonna be missing out on a ton of stuff
that sockets have to offer you with
basically anytime you want to send
things bigger than your buffer size so
most of the socket tutorials that I've
ever seen don't even talk about how you
build the message they it's usually like
a chat example they use a buffer of like
2048 bytes and they don't talk about
what happens when you exceed that but so
anyways not gonna do that to you guys
so how do we actually do it how do you
handle sockets that exceed your buffer
but you don't want to have to close this
connection you want to keep a stream
open and all that generally what you're
gonna do is use a header now there's a
million ways that we could do something
with a header tool too and basically the
header is going to notify your program
how long is your message and maybe give
some other information about your
message but in this case we really just
want to inform the program hey I'm about
to send you a message that it's fifty
characters long okay and then once the
program receives that first initial
header they're like okay I'm gonna wait
for till I have 50 characters of data
once I've got that boom now I know that
message is done now I'm gonna be waiting
for a header again and then the process
repeats so there's lots of ways that we
could build a header you could make a
header have some sort of fixed pattern
like begin header you know and then
maybe end you know and a message or you
know who knows you could do something
like that until and it would work fine
until people learned that that's what
you used and then they
start adding that to their messages just
to have a good ol time so you don't want
to do that so what should you or what
could you do and one option is a fixed
length header so you just always have a
header that you know precedes every new
message and it says how long is that
message going to be and then that way
your program knows exactly how much to
wait for and then when it's all done it
really looks for that fixed length
header you know nobody can really mess
with that okay so that's what we're
gonna do so now how do we do a fixed
length letter well enter string
formatting and Python welcome to the e
Python boozers okay so first let's just
do let's say you got a message message
and that will be equal to our welcome to
the server right now what we want to do
is get the length of that and issue that
in a fixed length header so you can't
just say Len message because that might
be you know if it's under 100 it could
be two characters one character if it's
greater than 100 be three characters we
need it to be a fixed length so how do
we do that so the way we're going to do
that is first you have to kind of decide
well what is like the longest message we
could ever receive okay I'm gonna go
with 10 so 10 would be that's 1 comma so
it's 4 7 10 so if you're thinking you're
gonna get a message longer than 1
billion characters do something even
bigger okay but for now I'm gonna go
with 10 so what we're gonna say is print
and then this will be F so we're gonna
use the F string here and then we're
gonna say Len message and then and this
will be : less than and then whatever
that header length is in this case we're
gonna say it's 10 so now we'll print
that out and you see it says 22 but what
you don't see is the extra characters
but if I highlight it you can see it
it's a fixed length it's 10 characters
the next thing we could do is we could
actually append to it the original
message so we'll do that and now you can
see here it it precedes the message now
if we said 20 it would you know proceed
with 20 characters so again it can be as
many as you need so you can understand
that eventually you you can come up with
a number and it's
kind of like you know require a huge
like your buffer will always be more
than enough to handle your header size
well I guess I can't say always but it
it should be okay great
so now what we want to do is implement
this luckily we have basically done it
so at least server-side so now I'm going
to come down here cut it come down here
paste that will tab that bad boy right
on over message will equal the header
plus the message don't forget the plus
message bit otherwise you're gonna have
a bad day cool okay we're done
server-side easy now we have to come to
our client and also let me change this
back to ten the other thing I'd like to
do is implement a constant that is just
going to be header size and we're gonna
set that to 10 and then here we're gonna
pass that with another what the hell
happened another set of brackets cool I
actually did not know this before doing
this tutorials I've never needed to
embed a variable inside of another
variable but yeah you can just embed
more brackets inside more brackets
fascinating okay save that come over to
our client and we are ready to implement
this on our client so the first thing I
would do is share the header size
constant so paste that in there and now
we are gonna continue with a new while
loop so again while true full message
we're gonna tab this over not only will
we have full message but we also want to
track new message as a flag so the very
first message we get is definitely the
start of a new message so we're gonna
set that equal to true will continue
receiving in small amounts but eight is
way too small so we'll go with 10 so we
can at least receive the header right or
actually let's do 16 so we receive a
little more than the header so message
we receive the message and now we're
going to say is if new message if that's
the case when you
No how big is this message is gonna be
so if it is then we're gonna say print
and then we'll say new message length
and that will be message up to header
size okay so that will give us basically
the length of the message and then what
we're gonna say is and it's sort of kind
of dawning on me that I don't think I
properly explained like what the heck is
actually happening here but so the so
obviously the 10 is how long this is
your alignment so that means it's gonna
be left aligned that's really it's more
like an arrow so if you wanted like
right aligned you would go right and
then I honestly forget what's there is a
center I think it's like enter enter or
something I could look it up I am kind
of curious I forgot already forgot what
it was oh no it's a carrot that so it's
a carrot sign so if you want it to be
centered you would use that but we don't
it doesn't really matter okay but we're
gonna use left aligned but I did
probably should point that out if you go
to the text-based version of the
tutorial I linked to the string
formatting example for Python just
because I see that it's like such such a
rarely used thing in Python so anyway
that's how you do it okay so so now we
get the message length the next thing I
want to do is actually grab the message
length so we're gonna say message length
equals int and then we can actually just
parse the in straight from this data so
yay for Python in this case because that
actually contains like some spaces but
Python figures it out in other languages
like if you're trying to if you can like
sockets work the same it's like you know
doing stuff with MySQL or something or
any SQL you can take your knowledge and
go to other languages so but python is
magical in the sense that this is gonna
work out for us in other languages you
would you need to do like a strip like a
dot strip to get rid of those extra
spaces like that probably this probably
wouldn't work in another link
but you could do a dot strip in another
language and do it anyway moving on so
we get the message length and then we'll
set new message is false because it's
not any longer a new message
so now new message length we've got that
information then every loop we do want
to append to the full message whatever
the new message is and then if the
length of full message - the header size
is equal to the message lend that we're
expecting then we have full message
recived and then let's print a full
message header sighs :
so we don't really care about the header
we just want to see it then finally
we're gonna say new message equals true
and print phone message great now the
other thing I'd like to do is remove the
client socket close we don't need that
no more new message true and then also
we'd like to set full message equal to
empty again so we empty it out so if we
got new messages we would continue so
let's save that save server I think
we're ready to go hopefully won't have
any errors we're gonna find out soon
enough
server dot pi actually I forget I
actually shouldn't matter what version
of Python it is okay we already do I
have it running sore okay let me yeah
let's up the sockets or the port rather
so we'll do 1 2 3 5 2 3 5 come back over
here and in fact I'm just gonna exit
that let me just do it again nice and
clean server dot PI and then we'll do
one more Python client dot pi ok so
tried to convert that message to the int
message lend so it definitely oh I see
what we've done right yeah so we built
the message but then we never actually
converted it okay so convert it darn I
can't so honestly the only machine I've
done this tutorial or I tried to film
this tutorial on a boon to and I had a
bunch of issues I always have issues
filming on a boon to so then I tried to
film it or I've got my Mac I don't want
to film out on a Mac though it's just
like a laptop and it's annoying to like
work on that in film client so I'm like
legit stuck with doing it where I can't
break it's very frustrating if anyway
okay so that worked we're done so now
the connection has not been severed
we are still sending and receiving the
data and all that and we could continue
to send a receive data so just to show
that as a quick example if we wanted to
we could come down here and wow true so
this is just like a really quick example
of sending like for example we could
import time while true what we're gonna
do is just issue a time dot sleep 3 and
then let's do copy/paste to have that
over to this over the message will be
whoops F it just doesn't matter time to
time the time is okay whatever
so we send the time then we get the
header size message and then all we want
to do is send that message so copy this
paste that save that and I believe we
should be all set if not oh we're going
to find out really quick but this is a
great way for me to test that what I've
said is true Python
a server dot pi really wish I didn't
have to do this every time I really wish
I could just break it
welcome to the server and then hopefully
in three seconds we'll see the time
congratulations we got it
continue continue to do okay so every
three seconds we'll get this new message
of how long the time is hopefully one of
these would be shorter sometimes a time
mole-like round itself but it doesn't
appear to be occurring but anyway you
get the idea obviously you wouldn't have
to send the time this could be a stream
of any information sensor data or chat
data or you know server log information
who knows it could be anything yeah so
this one was actually shorter so anyways
you can see is working variable length
the buffer is working
everything's great we're having a great
day I think I just have one more thing I
want to show you guys and I think I'll
do that in the next tutorial and that is
gonna be how do we send like messages
like this are cool like sending strings
is awesome and you can use at least like
JSON or something like that but in the
next tutorial I'd like to show you guys
how to send Python objects so how like
let's say you've got an array or a
dictionary and you didn't wanna use JSON
or anything everything in Python is an
object so basically any data structure
so including like machine learning
models or anything doing this in Python
is really easy so I want to show you
guys the next video exactly how to do
that but before that quick shout to my
most recent channel members Rajesh Kumar
cheeky buns 13 Frank Lloyd jr. Peyman
cyi Adi and Mohammed almora thank you
guys very much for your support you guys
are awesome okay I will see you guys in
another video