斯坦福-CS110-计算机系统原理笔记-四-

99 阅读1小时+

斯坦福 CS110 计算机系统原理笔记(四)

P15:Lecture 14 Introduction to Networking - main - BV1ED4y1R7RJ

Welcome, welcome to week seven we're getting there。 How's assignment five going?

It's all right hopefully this one's the first threading assignment obviously and。

it's it's got some nuances to it you definitely have to think about locks and。

you have to think about seven fours you think about new Texas and we've thrown。

in all that stuff about oh you have to make sure you only have so many threads。

and this and that and that's all to test your ability to use semaphores。 RSS。

news feeds are something that we don't really use too much now but about I。

want to say six or seven years ago it was kind of a rage like when these came。

out it was this big deal of like this is how you're gonna read your new Chanel。

and you're gonna get all these feeds and they're gonna it's gonna be all these。

little snippets of news that you can go read the bigger articles and all that。

and everybody thought it was cool and then we all used it for about a week and。

then went out and was boring like the old way and just kind of went back and。

but most like lots of places still have these RSS newsfeed services so you can。

get a newsfeed reader which is what you're building to actually create that。

Okay hopefully it's not too too bad it is due Friday what I think I'm gonna do。

is we're a little behind on the assignments only because the midterm。

happened and I wanted to give you a little extra time in the last one so。

what we're probably gonna do is I'm probably gonna release the next assignment。

maybe Thursday give you a slightly smaller assignment than what we were。

thinking before I got to work all the details out and then we'll get back on。

track because the last two assignments seven or eight are pretty meaty good ones。

but pretty meaty and I wanted to give you enough time to wrap your heads around。

those without making it feel like it was going a little crazy。

Alright but even though we are still in the midst of threading for assignments we。

are moving on to new and hopefully interesting material we're talking about。

there we go we're talking about networking today so this is kind of the。

next big thing and pretty much the last big thing that we're gonna talk about。

for the quarter actually we're gonna talk about networking which is connecting。

two computers together through kind of similar to a read write mentality it's。

kind it's very similar to creating a pipe between two processes but not。

processes but now we're creating this kind of glorified pipe between two。

computers and it's how the internet works and it's how the world wide web。

works and and so forth so we're gonna see a little bit how to do it by the way。

if you really like this stuff CS 144 is it networks yeah 144 is networks which。

goes into obviously much more detail than what we're gonna kind of touch on in。

this class and I think you'd enjoy that by the way if you've enjoyed the other。

stuff then operating systems which is 143 no sorry 140 what is 143 compilers。

yeah compilers compilers is also a great class by the way and then 140 is the。

operating systems class which goes into like how do you build thread a。

threading library how do you build multi-processing into an operating system。

and that sort of stuff if you really didn't like that then maybe stay with。

those classes go take more and that's but anyway if you do enjoy there are。

lots of follow-on classes and we'll spend some time talking about those soon。

anyway so let's talk networking what is networking wealth like I said it's。

basically connecting two computers on a network and that network can be very。

local in fact networking can happen between two computers on the same or。

between two processes on a single computer much like a pipe would but it's。

using network protocols instead of the kind of the underlying pipe protocols so。

you can network between two processes on one computer but most often we're。

talking about multiple computers talking to each other okay the way that works。

is you need one of the computers to act as a server and a server is simply a。

computer that's waiting around for some other computer to ask it something。

I'm waiting around for some other computer to say can I connect to you and。

if you don't have that server set up you can ask all day but the other computer。

is not going to respond okay so that's what you have to have you have to have a。

server and then you have to have one or more clients which are connecting to the。

server okay and the World Wide Web works this way if you connect to CS110。stanford。edu。

you connect to a web server and you are the client and many of you can do the。

exact can do that at the same time so when I say that it's the same time we do。

have to think about efficiency here because there's going to be some kind of。

ability for it has there has to be an ability for the server to get the。

information back to the clients quickly and there might be lots and lots of。

clients so we have to think about that first we're not going to worry too much。

about it but then we will eventually think more about that how does networking。

work it works through this thing called sockets and a socket is a number all it。

is just an int in fact it's an int that goes from zero to six five five three。

five in other words you can only have up to 64 kilo or kilo sockets open at once, 64,000 or 65。

000 sockets open at once which would be many more than you probably。

have most computers do not have that many open at once but it's just a 16-bit。

integer so that's why it's limited to 65, 35 and you basically set your program。

up to say hey listen on this socket or port another way of another thing we call。

it and when a another computer the client wants to connect to your server。

the operating system notices this there's lots of operating system and。

hardware support for this the operating system notices this and then sends a。

little wake-up message to your server that says hey you've got a client deal。

with it and that's that's what we're trying to do okay you can kind of think。

of the port number or the socket number as a virtual process ID okay so you can。

kind of think of it that there's some socket here that connects you to a。

particular process on the server okay that's the that's the way it goes all。

right and and the reason we don't have a do like the why not make just the process。

ID the socket ID is because you want to be you want to say hey listen tell people。

go listen on this particular port and that changes every time you do a new。

process so that's that's how we're going to do it how might we look these up on。

your computer to see what you're connected to well we can do the following。

we can do let's see hang on there we go we can do a studio here next stat and then。

I always forget what that runs our PLNT - PLNT make this a bit bigger so you。

can see the whole thing here okay and let me make it let me make it so that it。

comes out nicely like this there we go that's very nice all right so what you。

can do is you can do that on the mid servers and it looks like the mid servers。

are listening on a whole bunch of ports that are that you were able to see what。

they're listening on okay they're listening on port 25 which is see if I can。

get my cursor there you go port 25 okay they're listening on port 587 those。

happen to be associated with the mail server so all the myths listen on a。

mail server your computer listens on a mail server okay this one is listening。

on 53 every time 53 was 53 is the domain name service so when you go to。

www。google。com well some somebody needs to translate that into a number because。

your computer is dealing with numbers and people like to deal with words so that。

there's a translation layer there which is had handled by the domain name service。

which is running on your computer which connects to another computer which connects。

to another computer which which has all the domains on there like your computer。

kind of keeps a little list of where I go first and then that query is another。

computer which eventually gives you back the name I guess in some sense it's like。

the directory search for your first the file system programs it's kind of what's。

going on there okay port 22 is for telnet and it's all right for SSH we'll。

talk about telnet in a minute it's for SSH which when you SSH it goes to port 22。

by default some people like to set their computers up to listen on some other。

port for SSH because tort 22 tends to be one that lots of bad actors try to like。

connects to because they know that if they can figure out a password they can。

get into the computer so they change that but ours is just the default 22 631。

happens to be the printer server in fact we may not know this but you can。

generally on your computers let's do it in a new window here you can generally。

go to make this a little bigger here 127。0。0。1 which is your local computer it's。

always your IP address colon 631 if all works right there's the little printer。

details so it's actually a web server that you are listening to to actually do。

printing and it happens to be set up on many computers and you might get a little。

message that is not set up and it tells you how to set it up if it's if it's not。

we could also have typed local host colon 631 local host is as you might guess。

your own computer okay and it should also come up there that's the that's what's。

going on there okay so that's that let's see we've got another little 22 and 631。

therefore TCP 6 so it turns out that normal IP addresses are 32-bit numbers。

they've got these colons in them you know like 127。0。0。1 that's a 32-bit。

number just kind of separated up into into little two byte number two byte。

numbers but it's a 32-bit number and with 32 bits how many different IP。

addresses could we have four billion about there's actually more than four。

billion computers on the internet back when the internet was designed which is。

it turns out it was a defense department program back in the day in fact I don't。

know if I showed you this before original internet see there should be some。

images here I'll probably should be able to find out let's see where's the good。

one that I always like to use just the one yeah this might be it yeah there's。

there that's a slightly or oops slightly there's the there's one I was。

thinking oh no that's it that's the tiniest little one you can't see it on there。

well you can kind of see it Stanford is on there we were part of the original。

internet nice right notice who's not there, else relatively local not there MIT is on there BBN which is one of the original。

places it's a company that kind of start on this let's see let's see Carnegie。

Mellon was on there used to just be the Carnegie Institute at that point。

Illinois Utah was on there Utah believe it or not it was a really good computer。

science department back in the 1970s they happened to get on the internet。

really early so lots of lots of early think but anyway back then they said。

32 bits four billion computers on the internet that will never be big that many。

computers on the internet that's ridiculous so that's why they kind of。

started out with 32 bit numbers but of course today we don't do that how do we。

get away with this now what we have is routers that then give like in this。

building there might be one IP address and then the router gives each one of you a。

local IP address and so it's one level removed so that's what's that's just。

going on there we are trying to fix this issue so that every computer can have。

its own email or its own IP address we do that through we do that through IP。

version 6 versus version 4 which has a 128 bit address which is many more。

computers you could practically in fact I think you could probably give every。

atom in the universe its own IP address if you wanted to without many numbers so。

there's lots in there so there's plenty plenty of room to grow with that but it's。

going to take a while to get everybody up to speed some servers are listening for。

that okay now if you happen to be running a program yourself notice that over in。

this side it says PID program it doesn't say anything that's because it kind of。

hides it from non super users which we are not as just regular users we can。

actually let's me run something we're gonna actually create here time server。

descriptors I think that there we go so if I do this just be on the same。

computer what are we on over here myth 58 so let's actually do it on the same。

one let's do oh I'm with 58 okay let's do it again and look at that it says。

2 6 5 5 slash time server and that doesn't give the whole thing but that's。

ours like that's the one I'm running right now okay so that happens to be port。

one two three four five that's a good good why is it。

zero zero zero instead of 127 I don't know why tell you true not actually sure why。

that's why are we 127。0。0。1 is the way the internet says local computer that's。

just the number it gives all the computer all your low computer is the same as。

every other computer I don't know what it's a good question I'm not sure why it's。

as zero dot zero up there but anyway port is correct but the you're right the。

other part is not okay so what else can I tell you about these these addresses as。

I said there's let's see there are 65 or 64 thousand or 65 thousand of them。

you're really only you really only should use some in a particular range。

yeah why didn't it show that my SSH connection was established where I am。

SSH in a myth this is on the myth computer but it doesn't say what doesn't say the。

actual connection like there might be many connect this is just showing you。

which servers are available are listening that's all this is showing you。

now good question all right so you can actually go and see a full list of common。

ports okay the common ports if we go there's lots of them okay this is like。

that's actually just a tiny little list of them here let's see if I go to there。

here here's the common ports I make this a little bit bigger here okay very big。

so basically ports 0 through 1023 are kind of used for systemy things like you。

don't want to do a port that's really low notice all the ones we talked about。

earlier were kind of low they're like very well defined don't use these ones。

because they're all ready for SSH and for oh by the way 80 is for the web the。

web and so forth and so there's you want to be able to you don't want to use。

those okay but they're the list small in there as I say up here what the other。

ones are now so we've got lots of ones look if we look at 80 in there 80 is for。

HTTP which is the web and so forth lots of ports in there that you can look at。

that are well well defined and don't use them if you can help it we go to the。

next list here after that okay from 1024 to 49,151 which is you can see why they。

chose that number based on some powers of two those are assigned ports but it's。

kind of wishy-washy it's basically like somebody says hey I want to use this。

port can you assign it to me and the the IAN a which is like this organization。

that does this kind of determines whether or not you have a legitimate。

reason are you a company are you some you know you're doing something interesting。

they say fine you can have one we'll say that this is your port and some of them。

in there they they do that open VPN for like connecting your computer through a。

virtual private network that's 1194 etc okay there's but lots of them there's。

there's thousands and thousands of these but you don't have to live by those。

right if you do your own nobody's gonna go and you broke my thing if you're。

running a program that needs that port and you run another program that's on。

that port they can't both be listening to the port at the same time so that's the。

the big issue there we use for the one I just showed you we used one two three four。

five one two three four five this happens to be set up for cube world little fighter。

to and net bus they all kind of you know or I guess they assumed that they all。

wanted one two three four five we will often use that one just because it's easy。

to type and whatever but if you have to be running a cube world server on your。

computer well that's probably gonna conflict with the one you want to do so I。

don't know a cube world but anyway you could or a little fighter - whatever okay。

anyway and then above the whatever the forty thousand or whatever it was they're。

kind of upper grabs they will not predefined those you can use those for。

whatever you want and nobody's gonna say hey that was the one we kind of wanted。

after that okay so those are those are the port numbers and let's see there we go。

and those are what you will see you'll see a lot of normal ones and then we'll。

use our own question well that's good question ports let ports are on a server。

to listen to the computers a socket in a port is kind of interchangeable a socket。

is more or less once you set up a socket think of you setting up a pipe that's。

kind of the way it is but the port number is the socket number that's the you use a。

socket on a port number let's put it that way, well a server just is a computer right so it's not like yeah I mean your your you。

are when you send a message to another computer you actually use a port but it's。

not necessarily the one you're connecting to so let's say I want to。

connect to your server on one two three or five my outgoing port might be you know。

1800 or something like that it was whatever it goes out on and then and then。

the other computer goes oh when I respond go back to that port we actually。

hide that a bit of that from you from here you don't need to worry about those。

details quite as much for this one it's just kind of all built into libraries。

we're using it makes sense yeah anybody else good questions lots of details here。

that you can get into all right let's see what else that's that let us create。

our first server okay so we're gonna create a server and all the server is。

going to do is you do it right here it is called time server and we're gonna use。

descriptors to start out okay to do this and there's a bunch of like set up。

obviously in here what we're gonna do is we're going to set up a server that。

basically gets the time on the server as it turns out it's gonna get gm t which。

is the time at Greenwich Mean Time in England it's gonna get that well it gets。

the time and then it converts it to whatever we want we're in this case。

we're gonna convert it to Greenwich Mean Time which is which is pretty standard。

it's the easiest one to type as it turns out but what we're gonna do is gonna get。

the time it's going to win a connection when you get to connection request it's。

going to find the time and then format the time into a string and then push it。

back and connect the computer so basically like a time server it just tells you。

the time okay and that's all there is to it okay how do we do this well you can。

do int server equally remember the server is just a well in this case it's a。

in this case it is a number that associates itself with a server it's kind。

of like a file descriptor in that sense okay and then we're gonna use create。

server sockets okay one two three four five that's it that's all we're gonna do。

for this now to create server socket uses some underlying built-in system calls。

and so forth you can go look at that in I believe it is server socket dot cpp。

actually let's just see let's see if we can find server socket server socket。

dot cpp and it's not it server hang on。

server socket dot c oh that's easy no let's do here server socket dot cc there we。

go okay and create server socket use some underlying function a function called。

socket which does it and it's got some some parameters in there and things if。

we're not gonna worry about right now we're just always gonna use these ones。

you feel free to look at those we'll talk about some of the details of this it's。

pretty soon okay and then by the way did you notice that I did control Z and then。

Fg now you know what that all does I've been using it all year and some of you。

now you know there is okay so we create that we create the server and then we。

basically do a little while Lou that just sits here and waits for connections。

okay so we're just gonna sit here and wait for connections and when we find。

well we're going to do the client we're gonna get the client equals except and。

then we're gonna ask for the tell it to accept on the server number and then。

we're gonna pass in a couple of no's if we wanted to get the client's IP address。

we could use those we could use the no's to be a couple parameters ones like us。

one's like a status the other one's like what do you want to get kind of like。

wait PID we could use that for to get information about the client that we。

want okay and then we're just gonna call this function we're gonna write in a。

second called publish time to the client and that's it for me waits for it does。

the accept when by the way except blocks until you get an actual client that comes。

in okay just sits on wait and then when a client requests the connection it goes。

to the next line it creates the connection then goes to the next line and。

then and then it repeats that's all there is to it okay all right now if。

published time took a long time then you might start to drop connections this is。

something we have to worry about we will get there a little bit later yeah it never。

breaks out the wireless this this server goes on until you do control C and in。

most servers like that they just kind of go and go and then and then you want to。

break them you stop the program that's it because this is really simple now you。

could certainly have some other way to do this and and stop it nicely but doesn't。

really matter in this case yeah good question yeah good question why do we。

want to do this this is so that we can get you can ask for the time then I can。

ask the time and you can ask the time and something this is so that it's gonna sit。

there and just anybody who requests the time is gonna get the time back so does。

it one after the other at the other at the other that's why we want it in a。

while it's serving the time forever more as long as we have it written good。

right a good question the question is like wait this seems a little more hidden。

from us than like all the file descriptors and so forth for now at least we're。

really when you're writing programs all you really need to know is look do I。

have I set up a server using create server socket and then have I accepted。

the server and gotten a client from it that's really all the that's the level。

you're gonna look at feel free to look at the header files in the actual C C plus。

code if you want to dig a little deeper we're also gonna use well we'll see in a。

few minutes how we're gonna make it a little even a little simpler honestly。

that's not the level we care about the kind of level we care about is create the。

server respond adequately and send it something, yeah good question let's try so right now I'm listening on the port the question。

was wait what if I create a - what if I create two programs I try to listen to。

the same port well this one happens to be running right now in one two three four。

five on myth 58 if we do the same thing again and do another one on one two four。

five also a myth to get it says could not start the server doesn't let you it says。

are you've already somebody's already using it sorry and that's all there is to。

it okay yeah so you have to be a little careful when you're setting these up you。

don't you so for instance when you do your assignments with these network。

implants we're going to set up the port you use based on a hash of your username。

which is unlikely to match anybody else's so it's gonna should work out just。

fun but yeah good question you can't we can't duplicate it on the same machine。

doesn't work very good question okay let's write the publish time function here so。

to publish the time we have to get the time all right time and then it's actually。

time to underscore T we're gonna look at that in a second it's a struct that is。

built for you raw time I don't need struct time T because it's C++ or need。

in C++ and then we're going to just basically use the time function to pass。

in a pointer to this raw time which populates it okay and then we are going。

to use now we do needs a different one hour actually that's probably need to。

do this anyway but anyway we're doing it this way P T a pointer to a time which we。

are going to do that we're going to say gm time and we are also gonna pass in the。

raw time this is gonna do that some of the conversion for us okay and there's。

another struct called TM which we're going to do and gm time is going to do。

now let's talk about time for a second it turns out that if you look at time t。

sorry I said this wasn't a this was struck this is not a struck it's actually。

just an integer time t if you look up time t just an integer and when you ask。

for the time when you say time it populates the integer based on the number。

of seconds since the epic okay in fact if we go and look this up let's do it over。

here let's close this man time I believe this is it here and see here we go time。

returns the time is the number of seconds since the epic or epic depending on how。

you pronounce it which happens to be the beginning of time in Unix world is。

1970 January 1st 1970 and you say who came up with that number what was the。

reason for making that number as the beginning of time well Unix was created。

in 1970 and Unix was because they did that they said well let's we got to have。

the time basically from some time why don't we just start it now I don't know。

if they never thought that there might be an issue with using time based on。

numbers before 1970 I don't know they they didn't think they use this anyway。

for that but it was the time since 1970 and once again many computers these days。

still have time underscore T an integer that's actually a four byte or 32 bit。

integer once again number of seconds since 1970 January 19th first 1970 let's。

find out how many seconds that is how many numbers can we fit into a 32 bit。

number four billion right we want we just do this we'll do it this way let's say。

so there's two to the 32 which is how many bits we have right and that's how many。

different numbers we can do it turns out this is a signed number so it's actually。

only two to the 31 okay two 31 in fact it's minus one because you're not get。

you don't get that whole range it turns out because a second since so there's no。

I guess there's no zero in there minus one that's how many different numbers we。

have which is two billion something about this up a little two billion different。

numbers seconds since 1970 well if we wanted to figure out how long this is。

going to be right how are we going to or how are we going to do this so that we。

can figure out how many years it is how are we going to convert seconds to years。

in this case divided by maybe 365 point two five all right got it leap you're in。

there suppose right okay that's going to be the number of days per what is this。

there's seconds seconds per day now we want to end up wanting to know the number。

of years okay and then times 24 times 60 times 60 oops did I forget to print。

there we go okay so 68 years right 68 years from 1970 gives you 2038 what's。

gonna happen to computers that still use this time in 2038 it's gonna wrap。

right this is one of seven stuff I guess right it's gonna wrap around and any time。

after that is going to start from 1970 again so in 2038 we're gonna end up with。

an issue where there are computers in the world that are currently not gonna be。

updated until then because well they might be who knows let's see some of you are。

actually younger than 2000 who's born after 2000 a few of you yeah a few of you。

okay it scares me by the way that vividly remember your 2000 anybody remember。

the Y2K or anybody know about you don't remember it the Y2K issue but what。

happened in your 19 about 1997 people realized that there was a very similar。

issue with many programs where programmers would write the year with only。

two digits because they were writing these programs in like the 1970s and。

like who's gonna be using these computers 25 years from now ha ha ha。

right well banks all right banks still use the computer use the same programs。

and whatever and what they did was they wrote the date with only two days mainly。

to save or to two numbers to save memory because back when they were。

building these computers in the 70s the memory actually meant something and they。

well for every date if we save two digits that's actually a lot of a lot of。

memory back in the day it was a lot of memory so they did this well when 2000。

was gonna come around then it was gonna wrap around and go back to zero which。

instead of 2000 would be 1900 and then this would be a big deal around the。

first time I heard about it was about 1997 some people have thought about this a。

little bit before then but 1997 was when most of the world all of some woke up。

and went oh we're in trouble right we're gonna we're gonna like the entire world。

could collapse if all the banks collapse because all their software thinks that。

it's nineteen hundred instead of two thousand and so the world will burn and。

this was this was a big deal in 1997 or so well people went and looked at all。

these computers said well okay we can fix these we just have to hire people to。

rewrite the code and recompile it and so forth much much of the code believe it。

or not was still written in co-ball a language designed in the 1950s turns out。

that's still kind of true that a lot of banking software for instance happens to。

be written in co-ball and most of the co-ball programmers have retired they're。

all living in Florida are tired right and so they all started picking up their。

phones and somebody said hey we'll pay you a million dollars to come to our。

bank and fix this they all went a lot of plane and they went and like made lots。

of money and fixed it fixed it mostly but it really people December 31st were。

like storing like tuna cans and things and like lots of water and all that because。

they thought this was gonna like the world was gonna blow up because of this。

and of course he didn't have it I remember one website I went to that said。

the date was like January 1st 101 or something weird like that and it was。

probably that issue is what happened but anyway that issue happened in 2000 we。

were coming up with another Y2K like you know more better bigger or faster。

whatever in 2038 and there are still computers that deal with that so all of。

that is to say that we still use this time underscore T some computers like the。

ones we use now use a 64-bit number oh by the way 64 bit number would be instead。

of two to 31 to the 63 and this gives us until well gives us what is that two。

hundred ninety two billion more years so if people are talking about that to not。

a ninety two billion years now probably not gonna be also gonna be computers。

talking about it because you know it's probably not gonna be any human。

stalking anymore they're gonna go ha ha we've got a deal of this it's that could。

happen I think the Sun is going to become a red giant before then and then。

we're all you know the earth is gonna get involved and I think so probably won't。

be computers they might ship some computers off to some other sources。

who knows anyway they're gonna have to deal with that two hundred ninety two。

billion years now but that's that so back to what we were talking about all。

that digression aside we're trying we are getting a time to we're converting it。

to this struct TM which kind of breaks it up a little bit question yeah why would。

the time TV sorry who it's probably because you can get a。

negative like return value like oh let's just make it so we can get negative。

one back that's probably the reason like it yeah who knows but they also thought。

well again 68 years if they've done it if they've done 32 bits on some would have。

gotten another 68 years which is a kind of nice but they didn't think about it。

probably just because though I want a return value would be negative one。

probably the reason yeah oh for the time yes such a good question is there a。

timer in your computer is really what you're asking right I mean is there a。

clock that's we're updating yeah sure there is yep and the timer runs at。

whatever your clock frequency is but then they just divide by the number of。

clock ticks per computer if your computer runs it one gigahertz or whatever。

just divides by one gig or two get you the number of seconds and it and it does。

update that every you know every second in that case for that time that's。

happened that uses signal by the way just the operating system basically it's a。

signal that happens every it's a timer that happens every second it just goes。

and does that it actually happens much more frequently than that because there's。

other more precise timers than one second but yes there's actually a timer。

that updates everything oh yeah good question how do you synchronize these。

timers using time servers basically like somebody in Greenwich England says。

this is the real time boom and then it's and it's much more strata mercer do this。

by the way and they all say this is the real time based on this and there's always。

leaps seconds and things every couple years we get and whatever they say this。

is a real time and then all a bunch of time servers connect to that and then。

have their own local versions and then we connected those and it's all very。

coordinated doesn't always work but it's pretty coordinated yeah。

yeah so so people didn't hear that the so Amy got computer and it was from Germany。

or something and every time she turns it on it resets to the German time zone or。

whatever yeah when they when they set up the bios on that computer it probably。

said if you can't don't know the time look it up by just or just set it to this。

time and some other time zone where it was first originally like built and。

that's that's a hardware sort of issue by the way it may be because your battery。

there's another's a battery a tiny battery inside the computer that might be。

dead that might be the reason that it's a that it does like does it does that but。

and then it connects to the server and it's okay once it does it okay。

good all right so let's actually finish this this program up before you go to。

next programs we are going to get the time using this gm time function and then。

we are going to convert it using this gm time function then we're gonna have to。

convert to a string let's just use a char star time string there 128 is。

definitely long enough to hold our little time string okay and then we're gonna。

do STR F time this is the thing that converts the time to the actual string。

for you and this is in many languages has this stir time function and then you。

takes in the time string and then what else does it take in it takes in how big。

it is size of time string so it's an overwrite past the thing percent C in this。

case actually means not one character but it means do it in a local defined。

time this is the GMT time that it's going to do and then it needs to take in the。

pointer to the actual time struct TM that we have there okay and then what are。

we gonna do well we're gonna do this this will give you flashbacks maybe not。

good ones to the the midterm exam number of bytes bytes written equals zero size。

T num bytes to write equals the string length of time string we're sending that。

we don't need the zero on there as it turns out we don't need to send that and。

then while num bytes written is less than num bytes to write num bytes what is it。

num bytes written I always get this right to think about it num bytes written。

plus equals right and then we'll try to the client and then the time stir plus。

the num num bytes written and then num bytes to write minus num this is a lot。

becoming a long line num bytes written okay all right and then that was in a。

while loop and after that we are done writing and then we need to close the。

client okay so this is actually now all of a sudden really important to do the。

right in a loop because when you're dealing with networks it may it's very。

likely that you would not push all those bytes up the network files it's pretty。

likely that you're gonna get them all pushed but in this case unless you have a。

pipe that's filled another maybe like making you twitch a little from midterm。

but that's what that's what's going on there we need to write this to actually。

write all the bytes out but notice that the client in this case okay the right。

function like no uses treats that just as a file descriptor which in Unix it is a。

file descriptor turns out okay it's a file descriptor that ends up pointing to a。

socket that point that sends it to the client okay all right let's see if I made。

any mistakes here time server descriptors looks okay okay and then we run it。

run in the other one over here time times we've seen this already server。

descriptor we haven't actually seen it work it time server descriptors is running。

okay and you can actually get that information by connecting to the thing。

I've written a little client that we'll talk about probably next time but you can。

do it in an interesting way using a function called telnet which we're going。

to talk about a little bit later to telnet is kind of you get to what used to be or。

it's the original kind of version of SSH except it was very much SSH 10 for。

secure shell telnet is very much not secure but what it does all it does is。

it connects to a network and sets up a connection and then you can talk back。

and forth to the network so if we type telnet myth 58 and then give it port number。

one two three four five okay it all goes well there we go it tells you some。

details says trying myth 58 is it turns it that's the IP address from the 58。

connected to it tells you something about how you exit out of it all you do is you。

do the connection the instant you get the connection the myth the time server。

goes here's the time close the connection and so that's what happened here and it。

and that's it okay there is no back and forth we'll get to the back and forth in。

a little bit but that's it okay notice it is May 13th 914 it's 914 because。

that's the current time in Greenwich England this turns out okay 21 14 914 I。

believe that's correct 7 I was different something like that um so and some who's。

sending messages hi and you don't do that thank you though but don't you um so。

the so anyway once we have the server going now this while what happened it。

needed one connection and you guys could all check that by the way you could go。

right now to town that to myth 58 port 1 3 4 5 and get the actual time from。

myth 58 okay you could actually do that and check it out um we could do this in a。

little while loop here while one do uh let's see actually let me run let me。

just show y'all run the time client right now I need to do myth 58 and then。

1 3 4 5 and there we go just kind of it takes away all the other garbage but if。

we do this in a while loop you can see that it updates。

what while one do time client's myth 58 1 3 4 5 and then let's sleep for one second。

and done and that's there we go and then every second I'll just kind of update。

my skip one is not right on the second or whatever it might make that but you。

can see that every second it's updating because it just updates as we go along。

okay you can do some other kind of fun stuff with this I wrote a little uh。

a little she'll call let's see corner time。sh which uh looks kind of weird and it's。

got it's basically calling this let me change oh it is myth 58 that's good。

here's what corner time does let me run it for you first。

corner time just basically keeps the time up in the top left corner of your。

shell right you may ever wonder how them and emacs actually makes it so you can。

type up here in your shell and not down here and it actually it's using these。

things called antsy escape characters so if we look at this uh let's see。

antsy escape sequences there we go so here's a table of all these antsy escape。

sequences it's basically these agreed upon things you have to send to your。

terminal to make the cursor move around right you can do it you can build up。

pac-man game or whatever make it in the terminal just like you know you can do。

anything else you can make it move around the screen which is kind of fun it takes。

these things called escape commands um and if we look back at here again。

the escape character it's actually a little weird to type that it's actually。

it's actually one character you actually have to type control v and then the escape。

character and it puts that one character in there for you。

and then every time you want to print something you print the escape character。

and then some command basically in this case it saves the cursor position and。

then moves the top left and then um makes it white with red background so I。

wanted to do that and then it calls the time client and updates it and then it, only sleeps for 0。

2 seconds in this case if you do corner time at SH in the。

background well then you can do LS and whatever and it'll keep coming back。

kind of nice if you want to keep that little timer up there of course you've。

got one in the other part of your computer anyway and then we can we can。

do that um it doesn't necessarily work well with like them uh it works a。

ok but then if you want like it'll it'll work ok if you do that but then。

sometimes it goes like if you go back up to the top it'll probably not work。

there we go so yeah it's not not the most robust thing in the world but。

um that's that let's see let's get rid of that there we go。

so uh so you can use this time server if you if you want to okay then anyone try。

connecting to it did it connect okay if you connect at the time server。

can bring it connected to it yeah okay so what do we want to do to make this。

program a little bit better well what we want to do first is。

screen bigger uh we want to do first is we want to this is a。

published time we already talked about here um we want to actually get rid of。

that whole while loop right business that's midterm stuff we don't want to。

write that all right we want to make it easier to print stuff out to the server。

and uh let's do that using a library called socket plus plus okay the socket plus。

library is a just a library that wraps that right command and does all the。

buffering and whatever necessary for you so that it makes it easier to。

uh to do that so let's actually just quickly uh rewrite this。

core uh no we want to do uh time server we'll keep the same one so we don't have to。

type all the differences here uh so what we need to do is you just need to。

pat and you're allowed to use this it is not it's a library that's not built into。

c++ but you can use it uh it's called socket plus plus。

slash sock stream which is nice okay and then all you need to do down here。

is get rid of all this that we don't like writing and we just have to set it up。

to uh know how to connect to our talk to our client we do the following we say。

sock buff sb which is sock buffer and we tell it here's the file descriptor we。

want you to write out to okay and then we do iosock stream ss which is now a sock。

stream uh which is in this library as well and then we can do something like。

ss and then these should look very familiar time string and。

and it takes care of all that works okay and the other nice thing is we also because。

of the way the socket works when the function goes out when the uh sock。

sir the when the sock stream goes out of scope the sock buff descriptor i mean。

sorry destructor closes the client so you don't even need to close it。

anymore once it goes out of scope the client gets closed for you。

it's kind of nice so that makes it so you don't have to write those。

you don't have to write those um why that wild that's really annoying to。

write time server descriptors again, oh no socket sock stream get around here sock。

see i think that's right let me just see um, time server gotta do。

it is socket plus plus slash sock stream do that right。

oh oh dot h oh okay thank you better than me looking at this stuff there we go。

okay and there we go so it's going to be the same。

same exact sock is there okay all right and that's the the next thing we want to do。

um this is going to make it much easier so you don't have to do the writing and。

waiting and all that it's just uh you can use that as long as we have a c plus。

program you can use that which we are going to do uh for all the rest of the。

projects and things it makes it easier all right um。

what else uh are we going to worry about well turns it out and let's actually。

try something let's say that we add maybe let's say we made published time very slow。

okay so let's say time server descriptors cc let's make uh let's say that it might。

publish the time but then it's going to be really slow so we say something like, sleep uh。

or something like that oh that's not gonna work how about two there we go if we do that。

see there we go and then let's say i run this again。

and if we go up in our do our one second business here。

there it is okay what it's going to do is it's trying every second to actually get the number but。

should hang on, let's see we want to do this instead of let's get rid of that sleep and see if that makes a difference。

it should there we go so it's not sleeping anymore who's doing the sleeping now。

that's the actual server that's got the issue and that when you try to request a client it。

blocks requesting a client and so it takes time to actually do this we want to get rid of that delay。

because you don't want to wait for someone else's google search before you do a google search。

right you want to do it much quicker well and actually before we do anything else let's actually。

get rid of that sleep too so we well i guess we could we could do it later but how are we gonna do。

that threads okay now that you know about threads you can launch a thread every time someone wants to。

connect to you and remember how threading works you're doing it now thread says okay i'm going to be。

in some let's say why i move i'm going to launch the thread forget about it let it do its own thing。

and then go back up to the top of my my while loop or whatever and then i can accept another。

connection okay so that's what we're actually going to do we're going to use a thing called a。

thread pool which i've mentioned a couple times thread pool and a thread pool is what you're going。

to build for the next assignment okay so we're going to use it now but you're going to build it for。

the next assignment and you're going to see how that works in the next assignment but it makes it。

relatively straightforward about what we have to do to actually do this let's do this we need to go。

down to main and this while loop is where the problem is right we're blocking basically until。

publish time comes back so what we want to do is we want to say oh okay let's create a what we call。

a pool of threads that allows us to uh to go and set these threads up every time we get a request。

and a request we're going to throw it off to a thread for some maximum number of threads in this。

case we're just going to use four as it turns out you do need another pound include see if i can。

type this one right this time pound include uh i believe it is thread pool dot h there you go。

and that's that and then what we're going to do in main is we are going to uh we are still going to。

do the accept but once the accept happens we want to get back to that accept accept as fast as we can。

so that we don't uh block so we don't like make the uh user wait on it okay and in this case we are。

going to uh do the following okay we're going to say pool dot schedule when you write yours you。

will see what this is all about we're basically going to schedule a pool this takes a function。

great so we'll use a an anonymous lambda function here we need to do the client and then we need to。

publish time publish time for the client and that is going to be it and then we don't need that。

anymore okay so now we're going to this is going to happen instantly and go back up to the accept。

you'll be able to accept many many more clients very quickly they will all go into their own。

thread up to four threads in this case we could make it 40 if we wanted to but oh but we haven't done。

that we haven't set that up yet uh we're need to do that let's see thread pool pool four pretty。

straightforward you set up the thread pool and then you can schedule threads to go off on there。

you can schedule work to go happen on those threads as they go okay that's what's happening。

there questions on that yes so like thread wouldn't be as uh so there would be more efficient slash。

not like the other stuff that are in, so the question is do we do this because it doesn't take up as much resource as they know this makes。

them more resource takes up more resources as it turns out but at the benefit uh with the benefit。

of doing it fast so each three each time somebody requests the time we go and set another thread off。

or or say to one of the threads that's waiting around doing to do work please go publish the time。

that's how we that's how we use that okay pretty straightforward using thread pool is actually very。

nice as it turns out there easy does automatically do it it automatically thread pool does all the。

joining yet you'll build that into your thread pool when you do that okay but the one thing that。

we do need to change you have a question why do we need to create thread?

we need to create the threads when we're downloading things so so the next assignment you're going to。

see how you use a thread pool to make things a little bit faster than what you're doing now。

for it's you won't be able to tell necessarily for for the assignment uh assignment five。

but basically having threads already running means that you don't have to get them running when you。

call it so it's faster in that sense they're all just waiting around and going give me some work to。

be just like farm did for your assignment three okay i just picked four because it was a number。

bigger than one how many do you know how many you need good question depends on what your。

low you expect your load to be so google's gonna have thousands of thousands of threads running。

because they know that millions of people are going to be each server's got it's like lots and lots。

of threads um for ours i'm just making small just to do it we could have made it 40 and it wouldn't。

have mattered you you are generally limited to the number of threads on a particular machine。

if you're not the super user thousands or something like that so we don't want to do that now the other。

thing we don't want to do is make the bandwidth of the system such that it's uh it's we don't we're。

not going to want to like tie it up too too much this is what for assignment five what you're doing。

is you're saying oh i only want 24 total threads and i only want 10 threads going to any particular。

server because you don't want to blast it with all your requests that's kind of what we're trying to。

do with limiting these things as well okay so let's do this we have to do one other thing here。

we need to actually make uh publish time what we call thread safe it's not right now you wouldn't。

know that because you wouldn't necessarily think about it but the way we're doing this we are using。

one we're using one kind of globally available pointer here or one pointer that actually tries。

to go to this that shares the same time struct we can use a different function to to do this the。

first couple lines are going to be exactly the same and what we're going to do now is we're just。

going to instead of making it a pointer to a time struct we are going or a um yeah tm struct a。

struct tm we're just going to make it a an actual local copy of the time the tm struct and then what。

we're going to do is we're going to use another function gm time underscore r r stands for reentrant。

which basically means it's thread safe why is it thread safe now you are going to be copying the。

time into your local copy threads copy of the time so they're not all trying to share this copy。

okay so that's uh that's why we're doing that and it takes a little bit more information here takes。

the raw time and it takes a pointer to tm which is uh we're trying to do and then otherwise i think。

except for this being, antressant m i think that's all we need to do to change that say again did i screw some up。

you don't think it has to because it's a pointer to a time before it was a point ptm but pointer。

so now i think it does we'll see but anyway that's all you have to do to make it reentrant。

you have to think about these things when you do functions with threading you always have to worry。

a is my can i do the function is it thread safe things like stir talk not thread safe why because。

stir talk has its own internal state which is not really what you want why just use another one so。

for instance we look up uh man gm time right it should have there's a whole bunch of different。

times in here uh there is right there gm time gm time underscore r which is let's see say that。

it's the reentrant version yeah uh the gm time r function is like the store the data and user。

supply struct so basically it's uh there i don't know if it even you can look up thread。

there we go tells you if they're thread safe or not you have to think about these things when。

you're doing threads oh yeah the question was why do i have to change from a pointer to a local one。

well first of all the gm time underscore r function takes a pointer to the thing but it。

populates it for you so in this case it populates it for you instead of giving you a pointer and a。

local copy that it's got so that's the difference there the regular gm time says oh here's here's。

my pointer that i'm keeping the time in the one that or the arm i'm creating it mine for you and giving。

it to you and then if you called it again it would just redo that time and so you don't want your。

thread sharing that one value you want their own copy of it good question yeah is there anything。

you can do with threads manually you can't do a thread pool uh not really i mean threads that you。

you're gonna want to do threads if you have no idea what your load is going to be or you just want。

a one-off thread or something like that you probably don't need a whole thread pool for it yeah once you。

write that assignment you'll see kind of the differences of when when you might use it or not。

good question yeah um why wouldn't it be thread safe if it's not changing the value it will change。

the value you want to know when you call the function again if two functions call the call the。

gm time it will get updated and then it won't be the same they're trying to use the same value and。

you just are you only storing it once so let's say another one came by a second later the first one。

might get that later time if it hadn't published it yet or whatever so there's some issues of it。

yeah dude okay yes we took out the thread to make it multi-threaded yes um and as your question。

like should we try it again with the thread sleep to or if i put the sleep to back in it would make。

it would be that so here's what we did before we just put a we put a little thread we put a little。

inside here we put like a sleep to or whatever right so every time a server requests it that server。

is going to get it and then have to wait a little bit before closes its connection two seconds it turns。

out if you have multiple clients coming in at once then they will all get their own they will still。

have to wait two seconds it turns out but there's that each one does not have to wait two seconds each。

one independently waits two seconds doesn't have to wait for the other one to finish that's the。

difference there okay so all of that said we now have our good version which is which uses。

threading a thread pool as it turns out and it allows the server to accept a connection spin。

off a thread really quickly and then accept another connection so if there are 100 connections。

coming out there are four in this case connections at once it will very quickly deal with them it's。

already pretty quick it will very quickly deal with them and not have to wait for the published。

time function to end okay so that's the the big deal there now the uh let's see we talked about。

the published time that there's the updated time okay uh given the time now i think we're gonna talk a。

little bit about building building a client i don't know we might get a chance to to build the client。

but i want to show you a little bit more about uh the well i'm going to use town that to show you a。

little bit more about how the internet works okay here's what i want to show you okay so。

we use town that to go to our server in fact it's still running town that uh myth 58。

one two three four five and maybe not hang on maybe oh maybe we have the oh i bet we still had the。

still have that we still have the delay in there probably and there we go there's still the delay in。

there um so we used town that to do that town that again simply opens a connection and then has a。

plain text communications with the server okay so you could do the following townnet google。com。

port 80 well we know that port the internet is on port 80 and if you do the following you do that。

we are now connected to google okay now we're not connected to google's like pay server or whatever。

you know we're not connected like the internal network connected to the website just like your。

browser this is exactly what your browser does okay your browser says hey connect to google so。

that's that there's a bit of a timeout so hopefully i'll do the rest of it fast enough but how do you。

do this uh if we want to actually request a web page you have to do a very particular protocol。

and the protocol is http which you may or may not have heard about before but it's the protocol。

which is talked by a talked on by the entire internet okay there's a few different versions。

of it as it turns out what you can say though is you can say get slash which means the home page。

and then you say i am using http slash 1。1 there's 1。0 there's 1。1 there's 2。0 now, 2。

0 you can't use tell that for anymore it's a binary format so that's too bad。

sad face but that's that and then if you hit return one more time there's google's home page。

okay so that's the home page for google that you've now that we've now got it's kind of it's。

it's actually believe it or not smaller than you might expect it's mostly javascript in there okay。

and it's it's kind of compressed all right you can see that google doesn't bother with new ones。

right why are they bother with new lines if you're program if google is sending every query new lines。

that don't that the browser doesn't care about the browser could care less about new lines it just。

knows go to the next thing right well if it sends a thousand new lines well that's a thousand。

bytes it needs to send to you and a billion other people that are requesting from google right now。

google wants to minimize the amount of stuff it sends you because it has to pay for bandwidth i。

mean it's not like free right it has to pay for bandwidth and a billion times a hundred is a。

you know a lot of characters extra there's a send every you know day or whatever you know two hours。

or whatever it also wants to do it fast so the more new lines that get sent to your browser that。

don't matter are just wasted time sending you your query and you don't want to have to wait for your。

query so what does google do they get real new lines they run what's called a minifier on their。

javascript which kind of takes it all does this for you like the programmers don't write like this。

right you don't go to google and write like i can not use new lines at google right they don't do that。

right you can use all new lines you want then when they run up to this minifier that actually。

produces this so that that's what you get okay but that's the um that's how you get the details from。

from google okay we can also town that to web。stanford。

edu port 80 right i'll do it up here okay same sort。

of thing this actually has a slightly smaller timeout um if we do get and then you can get the。

actual one class slash cs 110 it actually requires a slash at the end it's very particular you always。

wonder why in 106a they tell you fv exact or 106b it's gotta be the exact output this is why。

computers talk to each other it's exactly the reason let's make timeout um and then if we type it without。

the slash and say 1。1 most web servers require that you actually say which host do you actually want。

google for some reason didn't require that although we could have done it stanford。

edu and then one more, another there we go what does it say it says oh i'm sorry your document that you asked for which was。

the one without the slash on it slash class slash cs 110 without the slash has changed and gone to。

slash class slash cs 110 slash right it keeps us figured out for you which is says you did it wrong。

and so that's why we did so let's try it again and then this is still stanford we're going to get。

slash class slash cs 110 slash okay http slash 1。1 and then host web。stanford。edu and there we go。

there's our web page for the class right and that's uh you can look at exactly the html and like。

the job script and whatever for the class um it's probably actually it's probably not the。

JavaScript uh it many web pages load external resources too and your browser has to figure all。

this out your browser says let me load the main page oh is there anything else i need to load and。

it goes and loads that and then that tells you more stuff for loading it's this big that's why it。

takes some time sometimes for these web pages like cnn。com which has a billion different files that。

wants to load on your computer mostly ads i would say and then it uh and then it takes a while to。

do all that so uh that's that okay all right anybody have any questions about the protocol right now。

you're gonna we're gonna build a little client to do this protocol in fact we might as well do do。

the client um this we're also going to start building a a program called wget which is built。

in your program if you do wget google。com what it does is it actually goes and requests the web page。

just like we just did it's pretty simple as it turns out if we look at index。

html there's the google, page again that we just did so you can do you can pull pages from the terminal all you want question。

when you type a URL in the web page it's doing exactly again。

okay well because when i leave out the slash in like cs 110 figures out how do you get rid of it。

anyway yeah well notice that that's good question the little message that came back and said your。

thing has permanently moved well your browser goes oh that was silly i need to put the slash on it just。

does another get request and gets it with the slash it's it's there are lots of error messages that come。

through uh your browser that are all hidden from you because your browser goes oh i can figure that out。

that's one of them good question okay uh what i'm going to do is client。starter oops to time client。

cc, okay time client。cc this is actually relatively straightforward okay we don't have too much。

for the time this is just saying how to the first part is just saying how to actually get the information。

here or how to get the parameters for that from the uh from the command line okay what we're going to do。

is we're going to do it client socket this time not client if i could type right client socket equals。

create client socket rv1 and we're going to convert the other one to a rv2 convert to an integer。

okay that's going to create the client socket okay we're going to just assert that it actually worked。

client is greater than or equal to zero okay you could do some better error checking if you want to。

on this uh the let's see um the client yeah you're right it should be client socket you're right。

i don't know why that's that's actually wrong so client socket thank you all right should be。

greater than zero uh okay and then we are going to do sock above and sb because this is what we're。

going to set up just like we did before because all we're going to do is we need to be able to get。

the information from the server okay iosockstream and sb like that and then we're going to do string。

timeline we'll say and get line that's nice so we can just do get line takes the sockets over and。

then takes in timeline and it just basically reads it in okay and then we print out timeline nl and。

that's it so there's our first client okay if i didn't make a mistake that's what it should look like。

okay make time client oh lots of hope anyway um, client and thread pool do we have their goal we shouldn't have their goal here hold on。

make time client oh no oh assert was not declared um and there's lots of other stuff in there too。

let's see how about pound include assert how about c assert see if that works。

nope did i do something else yeah let's see。

thank you right hang on see soft that。

i never declared s s you're right thank you very much how about we do that like oh no we do。

oh you're right thank you how about that we need to call the constructor i'm at the call it variable。

timeline, timeline timeline oh perfect okay thanks oh no same thing。

hang on well now it seems to say it's up to date oh maybe the other thing was not was broken。

time client and then do we still run running we do myth 58。

one three four five and there we go now we got the time we made it but anyway the point is that a。

client is actually not that hard to build right you uh all you really need to do is say create a。

client socket and to a particular machine on a particular port and then get the information。

sends you back right and that's that now you could connect up connection then have to send more。

information that's fine too but you can still do it this way so it's relatively straightforward to。

set up a client and now you have two different computers talking to uh up to in fact i think。

they're both on the same computer here but you could have to you could do this from your computer。

as well at home and your parents could do it wherever yeah so when we set the client we're just。

like a server it seems like it's more or less the same thing it's a very um it's not perfectly symmetric。

but it's it's a relatively symmetric thing you're setting up the i'm going to listen server i'm setting。

up i just want to make a connection to you and say please give me please send me some details some。

stuff that's the difference one is setting up a server and then waiting for connections the that's。

the server the client is saying i'm setting up this connection and then immediately connecting to it。

that's the difference to a particular one all right if there are other questions we'll see you all。

Wednesday。 Coq you。

P16:Lecture 15 Networks and Clients - main - BV1ED4y1R7RJ

Okay, so we are going to continue our discussion about networks。

I hope the assignment is going well。 I am going to have some off-sours right after class today for an hour。

hour and a half or, so。 So feel free to stop by if you'd like。

But today we're going to continue working on networks and I think we're going to get。

to a point today where you go, "Oh, I see what's going on here with the wavy internet。

works a little bit。", And hopefully it will be interesting。

But there is a handout because there's tons of code and today is going to be another。

day where I'm not going to just like furiously type code。

I actually did this lecture last quarter and it was a nightmare because it was like my。

fingers were cracked by the end of it and people were saying that's a ridiculous amount。

of code to put on the board or to try to do live。 So you've got a whole bunch of code here。

We will go through it slow enough on the board so that you can either take notes or。

ask whatever questions you have。 So hopefully that will work out。

So let's go back to where we were at the end of Monday's lecture which was to say we built。

our first client and the client was as simple as basically saying, "Okay, we are going to。

create a client socket and we're going to dig into those details。

We're going to do it a little more manually today and you'll see some more details but。

there will be details we see today that I don't want you to concentrate on。

You can look at them and go, "Oh, those are some details but I don't want you to think。

about the wheel talk about those next week。", But that's like how the connection is built and then we basically set up a little stream。

to be able to in this case to be able to read from the server whatever the server tells。

us and that's it and then we're printing it out。 So this is like the most basic server。

Now often you want to actually have some sort of like you want to send the server some information。

which we'll do by via the actual URL today and then of course you want to get more information。

back maybe in another particular form。 So we will see how that works with a couple of examples。

So the first example we're going to look at and I mentioned it, I talked a little bit about。

how it works and I showed you an example on Monday is this WGet function。

This is built in for Linux and you can see that by typing WGetGoogle。com and it will actually。

pull down the results of Google。com to a file。 In this case it saves it as index。html and that's it。

So there's the Google homepage right there in a file called index。html which I just used。

WGet to get。 It's a pretty simple thing。 You need to request it from the website。

the website sends it back to you and restore it。

So that's what we're going to build。 And here's what we're going to do with it。 We are going to。

we have to remember the, we're going to build the server part as it turns, out。 And the。

in this case rather not the server part, sorry we're going to build the client。

part in this case which is going to take a URL and it's going to get the URL and break。

it into its parts。 Okay, so that's like Google is not a great example but web。stanford。

edu/class110 is like, what you might pass into it。 And what it needs to do is this is the part that you connect to。

web。stanford。edu and the, rest is basically the path name。 So we need to break that into parts。

So we can actually use the part, a part URL function to do this。 Okay。

and most of our URLs are going to start with HTTP colon colon。

As it turns out HTTP that's basically tells you hey you're going to the world wide web。

Most of the websites you go to these days are HTTPS which stands for secure which is。

a much better way of doing it so the data actually comes across encrypted。

Because as you may or may not know, I mean when radio signals get sent to the router。

unless it's an encrypted site all of the text that's sent back and forth is completely, unencrypted。

Now if you have a password or if you have that sort of thing they encrypt, you can encrypt。

that in other ways。 But in general HTTP sites are not encrypted。

it doesn't matter that much for some sites。 But it does mean that you could have what's called a man in the middle attack where you。

have, I guess you could for HTTPS to some extent, but you could have an attack where。

you think you're getting data back from a non-secure site and it's coming from some other site。

That might not be what you want。 So you should prefer HTTPS。

For now we're just going to do with HTTP because we don't want to deal with encryption。

Most of it actually happens at a lower level than what we're dealing with anyway。

But for now we're going to go with HTTP。 And the default path is just slash。

That means like if you google。com the default path is just slash。

So what we're doing in here is we are basically pulling the URL apart and we're just saying。

look if it starts with HTTP then we actually need to get the rest of it which is what。

this substring line does here。 And then you're then going to look for a slash and if you have a slash everything after the。

slash is the actual path name。 So that's kind of what it is and the rest is the host part of it which is like google。

com, or stamp for web。stanford。edu。 That's all this is doing。 Not that much to it。

It is returning a pair。 Why is it returning a pair? Because C++ only allows you to return one thing。

And so in this case we're returning we want to return both the host and a path。

So we're returning a pair。 You could also return to vector or an array or something like that whatever。

But the point is that in this case we're just making a pair sending that back and dealing。

with the first and second of the pair。 Okay。 Pretty straightforward。

And this by the way means if you are trying to use the find on the string if the find。

is not found that's string colon colon and pass that means that you are going to use。

the default path which is just slash means that you just said hey I want google。com with。

no extra stuff on it。 Okay。 That's that。 Main in this case is just going to have one it's going to call one function which is poll。

content which is going to actually do the request and get the result back and then save it into。

a file。 That's what we're doing。 We'll break these up into other little parts as we go。

That's really all we're doing right we're going to a website requesting the website and。

then asking for the and then getting the data saving to a file that's all W get does。 It turns out。

Okay。 Questions on that? Yeah。 Awesome。 Yeah。 That's like Google。com slash which is your question。

What it's the slash part is the default path and that just means there's no extra stuff。

after the you are the actual website you're going to we'll see why that makes a difference。

in a little bit but it makes difference for doing like class slash class last CS 110 that's。

our class which is a shared website on all the entire Stanford web service。

Good anybody else? Okay。 All right。 So poll content is going to do the actual like hey we need to set up a client socket。

Okay。 We will see again we'll see in a little bit doing some things a little different with this。

one but for now we're just going to set up the client socket。

And we are going to then do a little error checking if we were to get set up。

Then we're setting up a stream and the stream is going to be to actually be able to send。

to the to the server to say hey give me the data。 Okay。

So we're setting up the stream and then we're issuing a request。

We are going to skip all the headers。 Remember when a website comes back it actually gives you a whole bunch of details and we're。

going to look and we're going to see those some of those details a little later and those。

details we don't care about for this website you might care about them。

You can ask for a you can ask for a website to be compressed coming back to you if you。

want and so that would be a header saying hey this is a compression I chose and then you'd。

have to decompress it yourself but we're not going to worry about that we're just going。

raw text back from the server。 Okay。 And then we're going to actually have to save it to a file as well。

Okay。 So we're going to create a client socket。 Okay。

So by the way what we really need for that we need just the first part just the URL because。

that's how you set it up。 We don't care that it's slash class slash CS 110 we care that it's web。

stanford。edu。 That's what we're connecting to on a particular port for the internet ports are 80 like that's。

the main internet port。 Sometimes you also see 80 80 or 8。

000 or whatever those are some kind of default ones but the。

world wide web lives on port 80 for servers。 It's just generally where it lives。 Okay。

And again you can change that if you have a home network that you don't want any other。

people to know about you can say hey forward my traffic to a different port get my website。

from a different port and people won't necessarily know how to connect to it unless they say。

the port number。 That's the way that works。 Okay。 What questions do you have on this? Yes。

What is component? The components is the pair that we passed in。

It's the host and the actual path name or path。 Yeah。 Good question。

Anybody else? Alright。 It's still pretty straightforward so far。 Okay。

Now we have to start doing the details。 Now remember when I did Talnet the other day。 Okay。

The Talnet command for internet websites or not the Talnet command the actual HTTP request。

is very well defined。 And it starts out with get and then the path and then it says what protocol we're using。

In this case HTTP 1。0 or 1。1 probably doesn't matter which one you use in this case。

And then it has this interesting backslash R backslash N。 Well we know what backslash, N means。

It means new line。 Well it kind of means new line but really it means new line in the sense that it means。

go directly down to the next line。 The backslash R is what we call a carriage return。

And a carriage return comes from the typewriter days where when you hit like the, well it used。

to be you actually had to hit a little lever that pushed this carriage where the paper was。

moving back and forth。 That's what happened in typewriter。 You should check some out sometime。

Come to my office。 You can see a cool typewriter project or a couple of them。

And anyway the carriage return meant that the entire paper would go juke back to the other, side。

I guess from your perspective it's our juke back to the other side。

And that's what's happening with the backslash R。 Backslash R backslash N is what you really should send whenever you are saying I am。

done with a line to the server。 It just happens to be that's the way it was that's the way it ends up working out。

Unix just kind of doesn't really use the backslash R often。

It just means look I want to go to the next line and go all the way to the front。

But you do need the backslash R in there as well。 Yeah, question? Why don't you use N to L?

Good question。 N to L will only send the backslash N as it turns out。 Yeah。 Good question though。

Backslash N just does the Unix way of doing it just does backslash N。

Which is not just what we want。 Okay。 So it does that。

Then the next line it says the host that you are on and the actual that takes the host。

and then another backslash R backslash N。 Tell that we will send that for you by the way, correctly。

And then we send another blank line to say we are done with our request and then we flush。

Now we have to flush the actual data which means if it is buffered in the system somewhere。

make sure it actually gets to the other computer。 Okay。

Sometimes if you don't do backslash N when you do print F or the N down sometimes the characters。

won't show up until you get that N down。 This is what the flush is all about。

It says look make sure the data is sent because otherwise the web server will have no idea。

when to respond or not。

Okay。 Let's do this example again。 Yeah。 Davis。 I'll unplug these。

No the flush doesn't check anything it just actually says oh if I've got some buffer data。

I haven't sent yet make sure it goes out。 That's really what it does。

It just says I could buffer it and keep it to myself。

I'm going to collect it all and send it all at once。 It says look now I want you to send it。

Definitely send it now。 That's really sick。 Yeah。 You could have flushed earlier。 Sure。

It doesn't really matter。 The member of the web server is expecting all of the data。

It won't do anything until it gets all of it。 Good question。 Yeah。 Good question。

Why do you need to flush? It just turns out that there might be a situation where without a just the backslash N might not。

actually do the push like actually might not say I've got enough data to send and who。

knows but the flush definitely says look I'm ready to send the data make sure it goes。

all that goes from this point forward。 That's how it works。 That's what?

It's a safeguard in that sense。 We could try without it and see the words but the odds are it might work。

New lines normally send a flush but you just never know in this case。 Okay。

You were going to get an O。 Is that a new word? Yeah。 Let me show you。 If we do telnet google。

com 80。 And then we say get。 We need the path name。 So that's the actual path name。

I'll show it to you again and you can look at it in the code but I'll show you it again。

And then we say we are going to tell it what one slash one point I guess one point O in。

this case for our example。 And then we send a return then we say host and in this case www。google。

com and then you。

hit a new line and then it gets all out there。 So that is the three things we need to send and that's exactly what we're sending in our。

program。 Let's take a look。 We're saying get right here。 We're saying。

Every time I connect to my this tablet doesn't seem to work when I connect to when I try to。

go back and forth between programs。 Don't exactly know why。 But there it goes。 Okay。

So we do the get and then we do the path which in this case was slash。

There was no path for Google and we didn't want the original index dot HTML the original。

like slash path and then we say oh we're doing HTTP 1。0 new line。

The next line we tell it what host we're on。 This is the Google。com www。google。

com and then we send a couple new lines。 The order does matter。 Yeah。

It expects it in exactly this order。 Why would you want the host to second? I don't know。

That's the way they designed the protocol。 I mean you're already connected to Google。com。

You already know you're connected there but then it just says hey which particular host。

of it might be that the website you're connected to has multiple IP addresses but different。

hosts on that IP address。 But。 Yeah like max。 Yeah。

It's something else dot Google dot com docs dot Google。 It might be a different one too。

It might be that。 Yeah。 So is the double again。 What is tell that part of double again? No。

Good question。 Tell that is just a way to connect via the terminal。 That's all that is。

W get is the program that's doing this via a network。 We're writing it。

You'll see how the actual word but we're writing the program W get which asks request the website。

gets it, downloads it, saves it to a file。 That's what W get does。 Anybody else? Okay。

So that's all the issue request function is going to do。 It basically says hey get the request。

And then we need to do what? We need to skip all these headers。

Skipping the headers is relatively straightforward。

You read a bunch of lines until you get an empty line。

Okay so there's a whole bunch of the first thing that comes back from the web server is。

all this header information。 And the way it's defined is header, header, header, header, blank line。

the rest is data。 That's all it is。 In this case we don't care what those headers are。

We could print them out if we wanted to or we could look at them or whatever but for our。

kids we don't care。 So all we're doing is doing a whole bunch of get lines and when until the line is empty。

we're just going to keep reading them。 Now some servers aren't nice and they just send new lines because some people who write。

these things don't realize that you also need the backslash R。

We will also if we just get a line that has a backslash R on it we will also figure that。

it's just a new line。 It's just kind of a hack in that case。 Just to see what's the most question。

Why are we doing the do with the wild conditions that are just like a straight line of the, wild?

Why are we doing it do wild? I think it probably saves a line of code。

I mean you would have to set the, you'd have to basically set the line up to be not empty。

and then you'd have to set it up to some other way and say it in some default value and then。

do wild and then get it。 I mean you could do it short but in this case that just saves the line or two of code。

So what was the thing with that? The most interesting thing was that a little language but there are other people who are。

familiar with the get that we should know? Yeah so this is a good question。 So why get?

Well get is one of the ways you can request information from a web server。

You can also do a post which means I'm sending data to the web server like I have a file。

I want to send or I name or something else。 You can also do。

there's another one that you're going to do for next assignment and, I'm forgetting what it is。

There's more of them too but it's just part of the language of HTTP。 That's all it is。

It's just the HTTP protocol。 One of the things you do is you use get or you use post or you use 77。

Chase。 So what happens if you just do /r? If you just do /r and not /n the web server might get confused。

I mean it just might go are you done with the line yet? I don't know。 You know that's why you need。

The protocol says you have to have both so we send both。 That's the way it works。

And again this is why when computers talk to each other they need to know exactly the。

right information。 This is why we hammer this into you in 106A, in B, in 107, whatever。

It has to be exactly the same output and it's not just because we're being jerks about it。

It's so that computers can。 Because our autograders are computers。

They're trying to talk too but you will see this again and again and again。

You need to be exacting your output。 So that's the exact thing。 I'm sorry。

What exactly does the header remain in content? The header information contains what kind of data is it sending back?

Whether or not, and we'll see this example, we'll see an example of some header data we。

care about later。 It's got, whether or not it's compressed。 It's got to hold on。

Whether or not the website can ask for the data when it's not on the website。 I mean。

so many times a website doesn't just really nearly give data to some other computer。

through certain methods。 In this case the header could tell you。

yeah it's okay or maybe we don't want to do that。 Whether it's cached or not。

I mean there's lots of different headers。 You can just look up HTTP headers and see the hundreds of them that it could be。

Yeah, header it's one line at a time。 One header another header another header another。

And then the last one is a blank line。 And then we're into data。 That's just a protocol。

Pretty simple。 All things considered。 A whole bunch of headers, new line data。

That's all there is to it。 Okay。 All right。 So that's that。 Let's see as we go to the next one here。

There we go。 Ooh, that's not what I wanted to do。 Hold on。

There we go。 Okay。 So we are going to then look at the get file name function。 Okay。

And by the way this is all because all like in you can look at the code and see what happens。

But it's basically we're just decomposing this into here。

The get file name is just going to take the path and it's going to convert the path to。

a file name if it's the last part of the path, the file name。

If it doesn't have a file name it uses index。html which is like the default file name for web。

pages。 If you've never done any web, if you've ever done any web writing you'll probably have seen。

index。html which is basically the first page you get to on a web page。 Normally。

That's the way that works。 Okay。 So it's pretty easy to do。

It just basically says look if the path is empty or if you've got a slash at the end just。

return index。html。 Okay。 Otherwise you are going to find everything from the last slash。

This is what the R find does and return that part of it。 That's all there is to it。 Okay。

So just finding the actual file and the anchor。 We're about to write the file out。 Okay。

So what do we have to do for saving the payload? Well we have to read in。 Okay。

We're going to be given a file name to save to which we're going to get from get file, name。

We're going to be given some stream that we are going to be in this case reading from。

That's the network basically。 Okay。 We're going to set this up and get the network stream that we're going to be reading from。

so we can read all the data。 Okay。 And how are we going to do that?

We are going to basically say while we are not have not read all the data。

This is the wild do because we can do that in this case as it turns out for this kind。

of stream we can do that。 And then we just read all the data。 Right。

We're going to read as much as we can into our buffer which is a fixed length。 Why 2014?

I don't know。 It's made of 2014。 It could be whatever we want。

And then we count up all the data and then we write it out to the buffer that we actually。

are sorry we write it out to the buffer and then let's see。 Sorry。 We're reading it into the buffer。

Sorry。 We're writing the buffer to the output the amount of data that we read。

That's all there is to that。 Okay。 And then it's counted up how many bytes so we know how many bytes got read。

Yeah。 Right。 Yeah。 That's a good question。 Why do we need to get finally and function?

Remember what would the finally and be if I asked for we have got stamped ready to use。

slash class slash CS 110 would it be class slash CS 110 that doesn't really make sense。

for a file name。 So we just want the last part of it。

So in this case we see us 110 which actually wouldn't work for ours because we would have。

to request CS 110 slash but if we did this would be the final just the last part of it。

So we got the path which we're going to request but then the final name is just the last part。

of the path。 It's a bit you know we're just creating that because that's what W get works。

Yeah so what is an I/O sock stream。 Okay。 This is so you know how you can do see out and you can do that and you can do see out。

stuff and it works。 Well that's a stream and a stream is C plus plus is way of saying hey here's some relatively。

easy ways of doing input and output。 I/O sock stream is a class that's created to make wrap a network file descriptor into。

a stream so that it's easy to use。 That's all I have sock。

Just a class that we're using to make it easy to do so that we can do things like output。right。

We can do things like where we read in from the SS right here SS dot read right that's。

like you do that from a stream and so forth。 And it buffers it all for you and you don't need to worry about having a while loop that。

does you know certain only certain amount of data and so it does all that for you。

David you have a question。 [inaudible], SS dot G count is the amount of data you get you got back I believe。

I believe that's what it is。 So it basically says how much we just read well how much did we just read right and then。

it just keeps that updated from the read。 All right so these are all kind of helper functions right now that we're dealing with。

So let's see was that the only value that was it actually that's the we've actually。

done everything and it's a no we didn't miss anything we actually had all of it in here。

because we needed to first issue the request we saw how to do here the get request。

And then we skip all the headers read all those headers and then the new line and then。

we save the payload which is everything after the headers。

Did we not have this did I skip the skip headers function。

No there it is there it is yeah that's it okay so just read all that。

All right that's how that's all we have to do in this case。

And that's it let's check let's check it out and see that it works。

Okay let's see see us one ten spring live lecture that out of the way。

Okay and then if we do networking and we do wwebget and let's try Google。com right。

well it says that's two hundred nineteen bytes which means something went wrong there。

Let's look at it probably means that it didn't have the right。 Let's see them index。

html yeah it says it has moved to Google。com/ so we need to do slash。

and oh we need to do we need the wwe probably。 There we go now we've got enough bytes we've got 46。

000 bytes so that actually works。 What was going on if we did the if we did without like we did wwebget just Google。

com, without the wwew。 Well if we look at what it says it says hey that document has moved to wwew。

google。com。 So that's just that's yeah it basically there's a difference between Google。com and wwew。

google。com, and so Google has set it up such that whenever you ask for Google。

com it says no no no please, ask for wwew。google。com and your browser does it for you。

If you just type Google。com it'll go to Google。com get this message back and go oh I got to do。

another request and get the real version back。 It's all that yeah。

Question about the save payload function。

Yeah let me pull it off the save payload function yes。

What was the turning backslash zero for the buffer?

What does that do? Ah good question so in this case because we're trying to read the question was why is there。

a terminating backslash I guess I skipped right over that。

When you you're reading in this actually says hey create the entire buffer full of zeros。

what it does in this case so they're all already zero so it doesn't mean you don't。

have to put one at the end when you read all the data in so it's already。 Good question。 Okay。 Yeah。

Would you also have to close the output? No。 In this case OF stream you don't need to close those like this case。

Yeah I think actually I believe it gets closed when it goes out of scope because the way it, works。

Alright。 Now let's look at how much more interesting and kind of a cool example that by the end。

of I think you'll see that you go oh this is cool and this is how the internet works and。

this is how this is not really how you would necessarily create a web page but it's pretty。

close as it turns out。 You don't necessarily need to create your own web server most of the times when you put。

a page on a server it will get run for you by the web server which is already running。

on your computer。 We're going to do a much more lower level version of this right now but it basically。

you can kind of see what the web server has to do to get your data to shoot it out to。

a some other client that requests it。 Okay。 What we're going to do we're going to look at we're going to look at an example that。

which has which is kind of it's an AP it's got an API associated with it right basically。

an application programming interface which basically means we know what we have to send。

to the server and it will give us back data in some form that we can understand。

Okay who here has heard of JSON before JSON。 Okay almost everybody not quite everybody JSON is a format that's machine readable but。

also pretty human readable。 Okay and we'll see the actual format when we get to it but we're going to output our。

results of what I'm going to show you in this format so that if we wanted to and at。

the end I'll show you a little web page I built that uses that we can actually get the。

data formatted such that our computer can understand it and do something interesting。

with it put it on the screen in an interesting way or use it to do some calculations or whatever。

but that's we're going to end up creating。 So what we're going to do is we're going to create this little program that uses a program。

called Scrabble Word Finder and this program you certainly can go look at it but that's。

a 106B program basically takes letters that you might have from a this place grab before。

mostly everybody word finder some other like words with friends whatever you have a number。

of letters and the number of letters you have to use to make words and they're all scrambled。

up and this program will find all the words in a dictionary that are come from those letters。

Okay that's all it's going to do let me show you an example of this let's see Scrabble Word。

Finder Stanford okay and it will print out all the different words that you can make from。

the letters in Stanford okay that's what the program does okay and yeah whatever words are。

in the dictionary it will make okay Scrabble Word Finder Network okay we'll print out all。

the ones that happen to be words you can make from that work and that's what we're going。

to use so let's pretend we have a website that wants to do this well we might create a。

Stanford we might create a Scrabble Word Finder program that does exactly this it takes as。

a parameter into the program the letters and it spits out all the words that create from。

those letters okay so what we want to do is we want to do a server that uses this program。

or does the same thing except instead of printing it out to the screen it shoots it off to our。

client in a form of the client can understand namely JSON format okay you might think okay。

let's go into the Scrabble Word Finder program wherever it does see out let's set up a network。

connection let's do a client or we'll do a little server connection and we'll have an。

accept and all that you know why would we do that if we've already got a program that。

works perfectly well there was a assignment you all did where you used other programs and。

got the results from those programs what was that program you wrote subprocess right this。

let's leverage subprocess to actually get the words from this exact program and our server。

needs to be this tiny little thing which basically just translates request comes in use the Scrabble。

Word Finder get the words send them back to be client and a story okay there's a fair。

amount of details that we're going to go through as we do this but as you see that that's what。

we want to do we don't want to modify the program that already works perfectly well if。

we can use a subprocess to do this okay alright here's what JSON looks like by the way this。

is a the results of what we're going to do we're going to run this program let's run this。

server let's say it's on port 13133 on myth 54 and when we request myth 54 colon 13133。

slash lexical we will take lexical and speed those letters into our Scrabble Word Finder。

so we need to figure this and we need to parse out the various you know parts of this so。

that we can get the letters from that so we're going to do that and then we're going。

to generate something like this we are going to say how long it took to do the request we。

are going to say whether or not it was cached or not so the assignment not this assignment。

but the next assignment in fact the big networking assignment you're going to do is a web cache。

program which is actually kind of cool but caching in networking basically means if you've。

already done something before don't do it again because networking itself is relatively。

slow but in this case it's not really that it's really if the program we have already。

gotten a bunch of letters and we've already converted them into words we might as well。

save those words in case somebody requests those same letters again in a cache and we're。

just going to report back whether or not it was cached why would the client care about。

this who knows maybe the client cares about it but we're going to tell them whether or。

not it was cached and then it's just and then it's just going to be an array of all of the。

words okay so this is actually a little it's basically a map time and then it gives the。

time comma cashed false and then possibilities and it's an array this is what JSON looks like。

notice it's relatively readable once you understand that oh this is what a map looks like in。

JSON you go oh now I can read it that's pretty straightforward and your your JavaScript program。

can read that very easily as it turns out which is what the you're going to probably be requesting。

the server the client data from okay all right so that's what we're going to end up doing。

here by doing this let's see what we how we're going to actually do this we're going to leverage。

as I said we're going to leverage sub process to do this and it's just going to use it get。

the output and do that do we need to send any data into scrap a word finder no which we're。

doing it via the command line so we don't actually need to actually say what the letters。

are we could have set the program to do that but we just do a Bible command line so it's。

going to be fairly straightforward okay so here's the main function what's the main function。

do for our server it should look relatively similar to other servers let's see let me find。

this on here so you guys can copy on it is on page three maybe no some page eight oh that's。

right it's at the end of the program right on page eight sorry on page eight is where the。

actual main is for this okay so what we're doing here is we are setting up the server kind of。

like what we've done in the in the past okay we are going to create the create that's right。

and zoomed in here we're going to create the server socket okay we are first going to extract。

the port that's actually a relatively easy for easy thing we're basically just going to。

get that from the command line all right and we're going to get the port number because。

what you can do is you are going when you run this you can say what port you want to。

run it on and then we are going to if you don't it's going to have a default and then we are。

going to create the server socket okay and then we're going to say what we're listening。

on big deal we're going to set up this thing called a thread pool remember the reason we're。

doing this is so that we can accept connections very quickly up to 16 connections very quickly。

and shoot off threads based on those connections okay so it's going to be a way to do you will。

build thread pool for the next assignment okay then we're going to have a cache and in this。

case the cache is going to be those letters mapped to the vector of the strings of the。

result what do you think we're going to have to do to the strings to make sure that if we。

get a B C D and then we get D C B A aren't those going to create the exact same set of。

words what should we do to this to the word we get in before putting it on a map yeah sort。

it just put it in sort it's all going to do the same but we will do that okay then we。

are going to need a mutex because we're dealing with threads we're going to need a mutex for。

the cache lock here or for the cache basically because if we're reading from the cache and。

anybody else is trying to write to it at the exact time that could be bad news we might。

get the wrong data it might corrupt something the writing might get something might go on。

it probably not going to actually corrupt anything but you might get the wrong data or。

it may actually end up being in a situation where the state is not known so we're going。

to lock anytime we read or write to that cache okay so this is kind of the demonstration。

of that okay then we're going to have our regular old while loop for our server that's。

going to accept connections okay and we're going to do things a little bit differently。

here this is the part where I said don't pay any attention let's just look at it and go。

wow we'll learn about that next week we're going to set up a this thing called a struct。

socket address underscore in for the actual IP address the reason we're going to do this。

is because whenever we get a client we want to actually know what IP address it came from。

why might we care about that we want to log it maybe we're maybe we're a program who cares。

about Scrabble words the most in the world what I like what regions of the world we might。

log that or whatever trust me every website you go to logs your data logs your IP address。

logs what you asked for look this is how people get collect your data always log in details。

about you in this case we're going to care about what the IP address is okay and the。

way we're going to do this is this stuff which I'm not going to go through the details now。

but we will see it next week and it's it's basically used to get the client IP address。

in this case okay and we are going to once we get it by doing all this fancy searching。

through the the response we're going to say that where we got it from and we have to actually。

print it out correctly and so forth but it's basically going to say hey received a connection。

request from IP address that's what we care about okay then we are going to schedule the。

thread so that we can then go back quickly and do another accept and we're going to do。

that by by calling our published Scrabble words function which itself will call self。

process and we're going to do that we need to pass in the client we're going to the file。

script for that pass in the cache by references it turns out and passion the cash lock also。

by reference that's what we're going to do okay that's the big idea of our program and。

then everything happens in published Scrabble words where everything starts to happen there。

questions on this one no fair asking about the details we'll do next week okay all right。

so like I said all of this address size I net and top we'll talk about that next week it's。

kind of cool okay published Scrabble words is going to rely on sub process we're going。

to eventually create JSON output and then we're going to see how that works so let's。

take a look and this next function is pretty detailed we'll do it line by line it's called。

published Scrabble words and it is on what page seven so I'm very good for it okay all。

right so here's what it is you've got it on your thing I'll try to hopefully you can。

kind of see it from the back there so here's what we're going to do in published Scrabble。

words okay we're going to make it so that we can write to the client pretty easily or read。

and write as it turns out from the client pretty easily okay we're going to call a function。

called get letters which is going to request which is going to take the request we've got。

and figure out what the after the slash for the web page that we're looking for and use。

those as the letters okay then as we said we're going to sort them because we don't care。

what the order is but it's going to make a difference for our cash okay we're then going。

to time it so why do we care about timing it again this is like our own little benefit。

of hey how long do I take to do this maybe it's going to take too long or whatever guess。

what the cash one should take much fewer much less time than the actually run on the program。

let's hope that that's the case we'll see if that's the case as we go okay we're going。

to just get the time to start and that's how you do that this is kind of what I showed。

you last week when we did the time server client similar sort of idea here now we're about。

to update the cash or rather we're going to see if our letters are in the cash so first。

we lock it then we do a fine on our map okay and then we immediately unlock don't lock for。

longer than you have to okay we could have just done a lock guard and for the rest of。

the function just been guarding that that variable or the the map but we don't want to。

because we want other threats to also be able to read to it and other threats to be able。

to update it so don't lock for longer than you have to that is a definite style issue。

that also can just affect your program one time so be very careful about doing that okay。

if we were basically coming up with a little Boolean that says whether or not we found。

the cash or not or found the string in the cash and we're going to create a vector that。

we're going to put those words in if we are cached we're done right we basically say great。

the words that in the vector are the words from our cache and the story okay the way。

find works is it gives you back a pair the second part of the pair of the iterator rather。

gives you back an iterator and the second the second thing in the iterator is the actual。

vector in that case the value from the map okay so it's going to do that otherwise we。

didn't find it in the cash well now we need to actually go and use our subprocess so we're。

going to call our subprocess and we're going to set up our subprocess command how are。

we going to do that grab a word finders the name of the program letters dot c string。

that's the letters we just obtained that's the second parameter the first parameter to。

our through our program and then no that should look pretty familiar from your shift Stanford。

shell okay then we're going to call a function called poll formable words and we're going。

to pass in a reference to the vector and we're going to pass in the file descriptor we get。

back to subprocess this is basically doing saying hey we're going to get all the words。

out of subprocess throw them into that vector one line at a time fairly straightforward。

and don't forget we have to wait till we're done right if you're doing multi processing。

you have to do wait PID this is this example ties in everything we've done so far as it。

turns out okay and then once we get the result back we know we weren't cached before we better。

put our vector into the cache so we lock our we lock our variable our map we lock around。

our map I should say via the cache lock then we update the cache with the vector based on。

the key of the letters that have another sorted now and that's it we can use a lock guard because。

when this goes out of scope the lock comes on really it gets unlocked even we were saying。

again we purposely previously said to do what oh right all right all right we shouldn't。

call subprocess with you can call let's see you can call in this oh good question that's。

a question the question is wait we shouldn't we shouldn't mix threading and multi processing。

in this case it's actually going to be okay because subprocesses we were very careful in。

subprocess only to allow the it's still in the same threat in that case okay I mean the。

thread now you do have you're right you do have multiple you have multiple processes in。

that but we are waiting for it properly and we are doing that okay so yeah in this case。

it's all right yeah I think let me look into a little more detail and yeah that's a good。

good point though because we always said don't mix don't mix those in that way it does turn。

on it works but it's good question so I'll look into it yeah good question yes of course。

we're gonna go down this path now oh yeah why should we not usually mix if you are if。

you are in a thread and you're calling multi you're calling it you're you're forking off。

a process right well then the thread is now kind of outside your process and like it's。

in its own like that the thread has to coordinate now wait wait now I've got this other process。

going on but I'm process and I've got thread so it's not necessarily a good thing which。

is wild to look up why this actually works but the in that sense you don't want to you've。

got a thread manager and a process manager and they don't always work well together okay。

in a process you should be able to call threads but we're doing the opposite here so I will。

look this up and see why yeah I'm not but otherwise you don't it's basically because you've got。

two scheduling things going on that don't like to necessarily won't necessarily work well。

together good good question this is a good point for the background right so the question。

is go over again why we use the locking here and not the lock guard but we're able to use。

the lock guard down here and you're absolutely correct your comment was up here we need to。

unlock here before there's any scoping going out anything going out of scope nothing here。

is going out of scope so if we try to use a lock guard it wouldn't actually go out there。

with the lock wouldn't release until somewhere down here where we're out of a scope and that's。

not what we want but here because the last thing we're about to do is update the cache。

and then to definitely go right out of scope we can use the lock guard exactly right yeah。

you don't have to you could do lock here unlock after the catch perfectly fine you're。

not going to mark that nope it's not a style but issue no it's just add to style it's a。

style but it's not a bad thing to do with the lock unlock perfectly fine chase you had a。

question no okay I'm gonna hand over here good questions on here very good questions but。

that's that's the meat of this here after we get the words out well that's how long it。

took so then we get the end time okay then we do a little a couple more commands to or。

functions to actually figure out what the duration of the time was in seconds this is。

using the another time if time valve struck and in this case just it just uses the seconds。

plus the millisecond or microseconds divided by a million which gives you the seconds and。

that's how that works out and then we are going to set up the payload now the payload。

in this case is we're gonna basically construct the payload from the words from whether or。

not we've been cast or not and from the time and then we're passing in this string we want。

to string that we're gonna send back to the client we have to construct that doesn't。

kind of come for free we have to kind of create a JSON string out of it okay once we construct。

that payload we then send it using another function called send response okay all right。

the payload is a string that we're gonna send back to the client and that's JSON you'll。

see it when we build it okay this is a long function other questions very good question。

about the multi-threading multiprocessing other questions on this one good question。

good question a lot okay yeah the question is hey how does this time thing work basically。

up here we are creating a very local variable called start it happens to be a type struct。

time value and then we're populating with the current time get time of day we'll say。

what time is it right now and populates that and then whenever we and then we just keep。

that locally and then later after we do all of our checking or all of our creating the。

words and so forth we then say that took this much time we do it again we get the time of。

day now subtract the two and we get the duration exactly what you would have done in the midterm。

again it's time everything again yeah the sock stream at the top is for basically reading。

in the data which we will let's see we'll write out the data here and then let's see do we。

read it anywhere else or do we already have it ready yes the get letters get letters is。

going to read from that one and o string stream is we are going to build up a string。

and that's how you do this right it actually it allows you to take a string and build it。

up one piece at a time kind of how it works you could do it as a regular string but this。

is like a string builder and Java you know you might have used a job so you'll see how。

it works you'll see how it works in a minute other questions on this one okay let's move。

on okay so the poll form of a words this is where we need to use the file descriptor。

we got from our sub process to actually create all those words and put the words into a vector。

it's pretty straightforward right we are going to do this which is not something you。

may have seen before this is actually creating a it's basically creating a data structure。

that allows you to create an input stream for that for C++ it's all that is and then we。

read one line at a time from the sub process and after we read one line at a time we one。

until we get to the end we then push it into the vector all there is to it there and that。

vectors reference so you don't need to return or anything all right send response well remember。

we've already constructed the payload we'll see that in a minute okay send response does。

the following well it actually is the reverse of the get command okay the get function it's。

the server sending data back before when we did the get time server we didn't care what。

the format was it was our own special format we now want this to be so that a web like a。

client or a web browser could use our server we're actually raising up the stakes here now。

we're saying oh let's do this from our browser so the browser is expecting the response to。

be in the following format it's expecting us to say we are using HTTP 1。1 slash or 1。1 then。

it says what is the status okay in this case it's 200 and if you go look up let's see uh。

on this way HTTP 200 it says it says the HTTP HTTP 200 okay status response code indicates。

that the request has succeeded so you get back a 200 that says hey your request succeeded。

there are lots of things that could go wrong the server could time out some reason that。

would be a different command that would be a different one the server could have moved。

remember the one we got before which was 300 or whatever it says look this means the server。

has moved you can look up the all the uh you can look up all the status codes and all。

the status codes are right here 200 is okay 201 means it's something's created there's。

an accepted you're basically the server sending back to the data to the client saying here's。

what's going on right now and it's and generally you're looking for 200 which says great everything。

worked out okay the ones that did redirection is the ones we saw before okay and that's the。

one that client errors or 400 saying the client says oh I screwed up right or you screwed。

up I get a server saying hey you screwed up you didn't ask for the right thing or you。

didn't give them the right form or whatever bad request or unauthorized or whatever 500s。

are I screwed up the server screwed up which are down here internal server error maybe。

you gave the wrong issue there's timeouts or whatever there's one that let's see there's。

one called I am a teapot which is 418 that one in here there we go one of the original。

web servers ever was a little web server that basically said whether or not coffee was。

ready in an office sounds pretty banal but that's the way things work you know when they。

create these things the first web server was this so somebody coded this up and said if。

you get 418 back it means you're a teapot why well that's the way it goes it's kind。

of an Easter egg it turns out okay all right so so we will send back okay we will send back。

the I am using HTTP 1。1 0 200 and then you also send an okay which says everything worked。

out great and then you send your little back slash our back slash and then you send some。

headers now this is where the server is sending headers to the client okay the header in this。

case is text slash one of them is called content type you will see that all the time。

if you're doing back end server work content type is what kind of data am I sending back。

in this case we're sending text slash JavaScript and they were also saying what character set。

we are using which is good because of the computers around the world know how to translate what。

we're sending the text slash JavaScript is a little weird I looked it up I was I was confused。

about why we're using that why we were using that ourselves so I just looked up like what。

whoops what should be the content type what should the content content type be for JSON。

and if you look it up there's the first link there of course the stack overflow it says。

I've been messing around here are all the ones that I've used seen used which one is。

correct when we were using is application slash no it was text slash JavaScript so you。

can use that one but somebody says no no no you should use application no sorry you should。

use application slash JSON that's what we should be using so we could change this and。

do it basically your browser if you can figure it out and it knows most of these it will。

figure it out that's what we're trying to in this case it's saying hey what's the format。

of the response going to be and that's what we're doing there okay all right then we are。

saying how long the data is so the content length is how many bytes am I about to send。

you why do we need to send the data why can't we just put a zero at the end it could be binary。

data it could be a JPEG or some other data that's not text and you can't have some particular。

character say when the data is ending you have to send the length so the client knows。

to keep reading for that amount of data okay now it could read until it ends which is also。

probably fine but it's good to know good to send it the data so it knows how much data。

it's expecting okay normally it would just keep reading until nothing's left and then。

say oh that's all the data that's that remember after our headers new line so we're sending。

that new line there and then we send the entire payload and then we flush to make sure that。

it gets sent over the data over the network okay so that's going on there questions on。

this one these functions this response is what we're have to send back so the website。

or so the web browser knows what to expect okay all right a couple other ones we're going。

to look at here the get letters function which is basically getting the letters from your。

clients okay how does that work remember if you say if you say myth 58 slash abcde well。

we need the you need the port number in there too but it's the abcde part we want to grab。

off of this so let's see how we do that okay what are we going to do we are given the socket。

stream that we already set up this is where we set that up earlier because we're about。

to read it we are going to create a method string a path string and a protocol string。

because remember there's get and then the path abcde and then the http 1。1 etc okay so。

we need to do that method path protocol we're going to read them in nice thing about a stream。

is you can do it one after the other boom boom boom to read them in one one after the。

other reads it ignores the white space in there or it breaks on the white space all right。

and then we need to get the rest which is one more line because we know that the we're。

going to have one more line after that okay and then we are going to just use the path。

is the only part that we use to do the to get the end of that path to get our letters out。

of it so basically we're using the fact that you're sending to a particular path and using。

that path as the letters and that's how we're doing that okay it's all there is to that return。

pause equals equals string and we didn't find anything right what are we going to return。

back we're going to return back the entire path otherwise we're going to return just。

the stuff after the slash yeah pauses the slash in that case yeah all right and then the construct。

payload part is going to do the JSON part now JSON is a very particular format it starts。

with a curly brace and it ends with a curly brace and inside it's got some fancy things。

in fact the one that's on the slide here I modified a little bit so that I could show。

you something in a few minutes basically generally real JSON actually needs another quote around。

all the different parts here so basically needs a quote around all the parts and it's。

not reflected here so I guess I should update that but basically we're going to say the。

time quit the time whether or not it's cached we're going to put way to her false and then。

the entire vector of strings that we got back from yeah when we said the status is okay we're。

we're saying if we got to this point we're about to send you a good string that's what。

means the status is okay so if you're to the point where you've collected all your data。

it's good to go and you're ready to send a response back and everything looks fine you。

send okay and say yep I'm ready it sounds like you give me a good response here it is。

and that's that now if anything else breaks in the middle like the network goes down or。

something you just won't get a response and then it'll time out in the web browser and, so forth。

Okay good question all right so we are ready to test it and then I want to show you a couple。

of other fun things okay so I've got it all written here it's going to be it is Scrabble。

Scrabble word finder server I am on myth 59 and oh no oh I bet I'm on it if it's okay。

let's do one two three four five and see if that way there we go okay so we're now on。

one two three four five so if in another window we go to Talmet myth 59 one two three four five。

okay over here notice it says received your connection request from 171。64。15。17 that's。

a log it said that's the that's where we got the request from your IP address gets sent。

to whoever it has to of course get sent to whoever your the server is and it gets logged。

okay all right so let's do our little get command we want to get slash let's say Stanford and。

then we're gonna do HTTP colon slash 1。1 and then we want to have like a host myth 59 I。

don't think it just kind of ignores that for ours and then that's that and look what we。

got back right we got all our words back which is kind of cool right so far so good well I。

promised you that we would be able to do this from a web browser let's see if that works。

okay so if I type in myth 59 colon two three four five slash somebody give me a bunch of。

letters a b c d e f j okay this all works well there's our JSON we got back okay it's kind。

of small but that's what it is and that's what we got back from our web so we created。

a web server we made a web server that actually did this okay now this is kind of interesting。

and by the way notice how long it took 0。04 seconds that's how long it took to do all that。

if we do the same request again it took 1。5 to 10 and a negative 5th sets because it was。

cached for us we those words were already created for us they were cached our server。

just went here you go I don't need to re I don't need to call that sub process baloney。

anymore and Eva tells us is probably not a good thing to do anyway so it worked that。

that's how that worked okay now you're saying yourself great this doesn't look like any。

web page I've ever been to right with this stuff so what you can do is you can sorry。

that's true okay what you can do is let me open up let me go to okay and let me go up。

here and do the same thing here but in this case myth let's see 59 colon one three four。

five okay so I didn't put a that time I didn't put natural thing in there in JavaScript you。

can actually request websites from JavaScript okay so let's actually do that what you can。

do in what's kind of cool about your your browser and this is why I was using I was。

using Chrome because I happen to know how to use these ones pretty well let's see that。

one we can close so let's see this is gonna make this bigger yes I can okay we're gonna。

actually use what's called the fetch command to do this so if I type fetch that's actually。

has the ability to fetch from a website okay and if I type HTTP colon slash slash myth 59。

dot Stanford dot edu colon one two three four five because we are slash let's do uh。

leland let's say we had those letters okay and then method you take the method in this。

case what's wrong do I spell it wrong why oh man like that yeah sorry sorry I feel bad。

now okay I'm not gonna work here much longer don't tell anybody please okay um looks get。

we're gonna use the get method here I do need it thank you very much syntax highlighting。

is nice okay so we're gonna do get in this case okay and then we're actually going to。

do a thing called then which is the part of the request that says how to do this you do。

not need to know this obvious I'm just showing you that it works we're gonna return data。

dot JSON because we know it's JSON that's what we're expecting okay and then we do another。

then and in this case this is the one that actually does the response okay and what we。

can do is we can print it out console dot log with principle log response okay and like。

that and then we can actually catch the error if there's an error there shouldn't be in this。

case there might be who knows error and let's see console dot log and whatever the error。

is we can do that okay if all goes well let's see there we go it gave us the amount of time。

it took and it gave us that and that's that so we just wrote a little JavaScript function。

do that again you're saying yourself that doesn't look like any webpage I've ever been。

to okay but what you can do is where do I want to go here I think I put it yeah I put。

it on you are web by the way you all can go from your web browser and do this I don't。

think they tried that in the last five minutes but what's that oh yeah people are doing it。

thank you I guess everybody's doing it you can do that I guess you people are doing that。

class CS 110 WWW okay let's see it is called Scrabble word finder dot HTML I wrote a little。

HTML program to do this and the reason I'm changing it right now is because I hard coded。

you shouldn't be this but I hard coded the wrong I didn't know it was going to be there。

one one two three or five in there okay and basically it's setting up a little webpage。

so if you've done any web were that web step before you'll see I'm just setting up a head。

at the title of the webpage and a couple of places to put the details and whatever and。

if all goes right we should make this a little bigger whoops we should be able to go to web。

dot Stanford dot edu slash class slash CS 110 slash Scrabble word finder word finder。

dot HTML and there we go okay we have our letters and if we type L E L A N D correct。

this time right we should get it back and there's our webpage so we created a and that。

used by the way that fetch and whatever use the JavaScript and said oh I know what these。

things are and it made it into us still a dumb looking web page certainly but it now。

works just like any other web page that you might might care about okay so what other。

weird words in there yeah you can get a Dorns from Leland what's that no you can't you're。

right wait a minute this didn't work right yeah wonder what happened with that say again。

oh I could stand for no I didn't I did I probably did oh my gosh I did you're right。

okay I'll fix I'll fix that wow good call that doesn't make any sense okay yeah for some。

reason I heard that it would be easy enough to get why didn't well anyway I didn't do it。

so oh there it is right there we'll fix it right now since we can it's letters I knew。

I got it out of there I just forgot to do that let's see plus let's do this slash plus。

letters okay live changing the website right there let's see if this works better okay okay。

okay I don't know that's a good thing when you get applause after you make a dumb mistake。

I'm fixing it might be the good thing okay what other question you have about this there。

was a lot of code to look through today but it's all in there it's a lot of stuff we've。

seen over the years I will find out about the multi-processing sets a great question and。

then we'll do it yeah yeah, yeah yeah yeah yeah so good question if you just type myth 59 the web server is a different。

computer altogether the actual Stanford web server is not on the machines we created。

our own little server which is kind of cool part right but we created our own little server。

the web server that Stanford uses is a professionally built open source but professionally built server。

that serves all the regular old Stanford web pages that you go to web。stanford。edu that's。

where I put this one but remember this one goes to myth 59 and I did a hard code go to。

myth 59 in this okay that's wrong this tab is my computer talking directly to myth 59 how。

does no or 59 is it remember the DNS server I mentioned the other day that's where the。

domain name server you say go to myth 59 your computer and you're through your browser basically。

looks up what is that what IP addresses that connect to then it goes directly there this。

goes directly to myth 59 if I'm in the website here now I'm on the Stanford website which I'm。

gonna talk to the browser is talking to the Stanford website the Stanford website and through。

JavaScript and it that comes to me and then it tells me the JavaScript I need to go to。

myth 59 directly so basically I'm loading the web page from web。stanford。edu which is the。

main Stanford website and then I'm myself talking directly to myth 59 on my computer。

here to get the actual words you could probably do a different way where you had this website。

or my computer talk back to the Stanford website which then talks to myth 59 and does a one。

or layer of indirection but in this case we skip that in your direction。 So it's a little。

there's a lot going on there。 Yeah。 Yeah the port closing happens in there it is it happens。

when the client socket goes out of scope so it's basically back wherever we created it。

I think we created it right here so it goes out of scope like down here at the end of。

this function and then it closes it。 That's where the in our case the server closes the。

connection to the client。 Okay good questions。 Alright like I said I'm glad to do it。 I'm。

sorry now feel free to stop by or after class and ask more questions and we'll see you guys。