I started doing my Computer Networks Assignment on socket programming in C. It’s tough believe me. I think it’s one of the toughest things I have taken up. It’s not completed yet and I don’t believe it will in anytime near. I felt good yesterday that I have finally started studying. I sat for an hour 5:30 pm to 6:30 pm continuous and read Socket programming introduction text from Computer Networking: A Top-Down Approach written by Kurose and Ross. It was a good read after so long break from reading textbooks.
Next, I searched the Internet to implement the same [a multithreaded web server that implements a subset of HTTP/1.0 in the GET and HEAD response it sends to the client] in C. So, there are several resources but for a beginner as me, it’s not only tough to comprehend some of the advanced topics but tougher it is to segregate the advanced stuffs apart. I came across geeksforgeeks.org’s article that formed the basis of my assignment. However, I still have to incorporate many changes into it.
In order to understand that small piece of code (nearly 30 lines each for server and client side), I had to surf several Internet webpages and pdf guides. To cut the chase, some of the most important and relevant guides are -
- Beej’s guide -> Please do read it!
- Kameswari Chebrolu, IIT Kanpur -> Really good!
- Your Linux machine’s man pages like -
man 7 socket-> Please don't google stuffs that are already documented in man pages. These documents will save your time and keep you away from turning every google search link to purple. - Tutorialspoint.com - for C functions and libraries description
- Stackoverflow - (do I need to say any description for this?)
So I only could understand server.c before dinner.
At many times during your coding, it will occur to you that everything is going down the hill. But believe me, don’t quit. Codes will run in small parts but when combined together, they will break. Then you should take a break, come back, you will find some way, you will get some new ideas in your head and finally you will make it. 2 times, I wished to quit. Luckily, I am sane and telling you that my code worked!
Multithreading will take another week, if I start doing, perhaps. But for now, single threaded (serving only one client) web server implementing a subset of HTTP/1.0 for GET and HEAD requests is fine.
- We need two codes – one for server, other for client
- We will work in Linux system (I used Ubuntu 18.04.1)
- We will not be scared of data types – POSIX has some weird data types (I don’t know the details or the differences, just know that your gcc compiler will tell which identifier format ‘
%d’ or ‘%lu’ etc, to use) - Linux man pages should be referred to for your system specific parameters to functions (system calls). Example,
man 7 socketlink to all manuals needed - Strictly don’t know everything. You are advised to only know how to implement most things, what parameters to pass, what the function returns and type of those parameters.
- C
struct sockaddr_inis your tool to play with, however, system call functions likestruct sockaddr. So, the former should be type casted into the later. Don’t worry about the fields of sockaddr. - File handle and socket handle are analogous.
- For server – create socket, set socket options, initialize sockadddr_in members, bind socket to a specific port number, listen, accept new connections and assign them a new socket, send/write/rcv/read from new socket.
- For client – create socket, initialize sockadddr_in members including the conversion of IP address to binary form, connect, send/write/rcv/read from socket.
- Rest is file handling and handling of buffers.
- Sometimes or more often,
send()will work better thanwrite() \r\nandEOFcharacters are crucial and should be handled with care.- You can implement better and efficient string reader/ parser/ splitter with regular expression, or other functions but I have used
strtok(). - If you see
segmentation fault (core dumped)that means your char buffer arrays are messed up. Program is doing some invalid/unallowed memory access. - Two links - geeksforgeeks(connection setup) and stackexchange(Josay's answer on file handling).
Okay, more details and code stuffs will be in my code repository.