Making Minimalist Web Server in C on Linux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to start by using the socket function so let's start by searching socket in the Man pages you can see I first need to include this file so I'll just add this include here and I can start using the function so I'm going to call socket and the first argument is domain so let's see what domain accepts we can see that domain actually has a couple of different options I'm just going to use afin for regular ipv4 afterwards second argument is going to be the type you need to actually choose the type of the socket I'm just going to use sock stream which will be a simple TCP socket that's a socket that HTTP protocol actually uses finally the last argument is called protocol you can just leave it as zero to make it default this function will return the file descriptor of the socket so I'm just going to save this in a variable called s afterwards we're going to use the bind function to actually bind the socket to a certain address so I'm going to open the man page for bind you can see that this also has the same include file so no no need to add any includes I'm going to start by passing the arguments so bind gets the socket file descriptor that's just going to be S afterwards it gets a structure with the address to bind I'm going to Define this later I'm going to just pass the pointer of the structure so add an ENT over here and I'm going to pass the size of the structure as well which is just going to be size of address let's define the structure for a sec so that's going to be struct and then sock Adder I in that's going to be for internet let's open the man page for sock Adder I in for a sec you can see we have a couple of types here reason I'm using sock Adder I in is that's the one that is suitable for ipv4 and you have three items that you need to fill in here first one is the family so that's going to be just afin net so I'm just going to go back here let's give it a name for this structure I'm going to start filling the details AFI net for the first [Applause] one second one is going to be the port number now this is kind of tricky I'm going to open python now the port I want to use for my web server is the port 880 but this is kind of tricky because you need to change the order of the bytes so I'm just going to see what 880 is in HEX so I'm using the hex function from python you can see that this this is how it looks like in HEX what you need to pass to the actual structure is this number but opposite in X so I'm just going to pass 90 and then one F this is actually opposite order and this is how the port number and the IP address are accepted by the socket API in the case of the address I'll just leave this as zero in which case it'll just bind on all my network interfaces that's fine that's fine with me after bind let's call the listen function listen will actually initialize a socket for incoming connections so after buy I'm going to call listen and I'm going to again pass the socket and I'm going to pass an argument called backlog this is just the number of connections that can be waiting at the same time until the server starts rejecting new connections I'm just going to put this as 10 doesn't really matter because we're just doing this as a PC this is not for production finally I can call the accept function accept will actually accept the connection coming from the clients so I'm going to call accept and pass in the socket and then we have two additional arguments address and address length both can be actually zero if you want to get some additional client information you want to pass something here but in our case we're just going to leave both of them as zero that's fine this will actually return the file descriptor of the client socket so I'm just going to save this in client FD now I can actually receive the string that the client is sending so I'm going to call recv I'm going to pass in client FD let's open the documentation for recv for a sec afterwards it gets a buffer I'm just going to call this buffer and the length let's just keep this as 256 keep it as simple as possible Flags can just be zero going to put the buffer here as well so buffer will be 256 bytes let's just fill it with zeros this will actually get the HTTP request and the HTTP request looks something like this you have we're just going to keep this only supporting get requests so it's going to look something like this get and then you'll have some kind of file here file. HTML then you'll have a bunch of additional stuff we are actually only interested in extracting this so I'm going to skip five characters from the beginning to get to the file here and I'm going to go ahead and for the first White space I'm just going to change this to zero to make it a null Terminator so this I'll have a string only containing the file so I can just open this file so I'll just put this in a comment for a sec and let's do this so I'm going to save aside another CH pointer this will actually be the file just call and call it f you to start by going to the buffer and skipping five bytes this will get me here and afterwards I'm going to use a function called Str Str chart so let's search for this function for a sec Str strr charart this function actually locates a character in a string so we'll just return a pointer to where the character was found you can see that we need to include string.h for this so I'm going to go to the top of the file and includ this now let's go ahead and call this s strr Char I'm going to pass in the string and I want to search for a space now I want to take the result of this and assign it to zero so this will just change the space to a null Terminator which will just change this to a null Terminator leave me only with the file afterwards I'm going to use the open function to open the file so I'm going to read the documentation of open you can see this comes from fcnl Doh so I'm going to add this include and I'm going to call this and I'm going to pass in the file that I want to open the path and flags can just be in this case the argument Flags must include one of the following access modes I'm just going to use this read only mode so going to pass into this o read only this will return the open file so I'm going to call this opened file descriptor afterwards I'm going to use a system call called send file this enables me to actually transfer data between file descriptors so I'm going to use this to transfer data from the open file which which I open over here this is the actual file on the file system that the client is requesting and I'm going to send this to the client socket which we have over here this is a client FD so I'm going to call send file which comes by the way from another include file so I'm going to include this as well send file. H let's see the arguments here so it starts with the output file descriptor which will be the socket so the client FD afterwards the input which will be be the open file from the file system then we have an offset which can just be zero and count which will be 256 we're just going to keep this simple on the 256 bytes finally we can close the file descriptors so I'm going to close the opened file and close the socket as well close the client socket as well also let's the include file of close that comes from un SD finally another thing that I missed is the include file of this structure and I scroll down to the part where you can see this Define here you can see that it actually comes from this include file so I'm going to include this here that should be it let's go ahead and try to compile this we have a little error over here open FD yeah we need to actually add a type over here let's try now looks like it compiles successfully let's go ahead and run the server and I'm going to run this with srace so I can actually see what's going on okay see that the bind actually succeeded so we're return zero and now it's accepting a new connection so now it's waiting for a connection let's go ahead and just open a small HTML file so I'll just call this index.html going to say going to write here hello from server it works going to close this and let's open another terminal I'm going to go to a different directory you can see that it's empty right now I'm going to use W get going to pass in the port of the server I'm going to say that I want to grab index.html you can see that it successfully grabbed and the server exited and if we go ahead and C this file you can see that we see the message from the server writer over here this is the file sent from the server to the client subscribe for more programming videos and thanks for watching
Info
Channel: Nir Lichtman
Views: 178,059
Rating: undefined out of 5
Keywords:
Id: 2HrYIl6GpYg
Channel Id: undefined
Length: 10min 22sec (622 seconds)
Published: Sat Nov 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.