-
Part1 - C code
- Server
gcc server.c
./a.out
[PORT]
- eg. ./a.out 8080
- Client
-
gcc client.c
-
./a.out
[IP] [PORT] [filename present in server directory]
-
eg. ./a.out 127.0.0.1 8080 file1.txt
-
- Server
-
Part2 - Python code - Persistent & Non Persistent Both
-
Server
- server.py num < input files (num = 2 for persistent, num = 1 for non persistent)
-
Client
-
client.py num < input files (num = 2 for persistent, num = 1 for non persistent)
-
-
-
Part2 - Persistent & Non Persistent Multiple Files
-
Server
gcc server_persistent.c or gcc server_nonpersistent.c
./a.out
[PORT]
- eg. ./a.out 8080
-
Client
gcc client_persistent.c or gcc server_nonpersistent.c
./a.out
[IP] [PORT] < Input files
- ./a.out 127.0.0.1 8080
-
-
Socket - A network socket is an internal endpoint for sending or receiving data at a single node in a computer network. Sockets are an API that most operating systems provide to be able to talk with the network.
-
HTTP Connection - HTTP is an application protocol. It basically means that HTTP itself can't be used to transport information to/from a remote end point. Instead it relies on an underlying protocol which in HTTP's case is TCP.
-
Socket Programming - Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.
State diagram for server and client model
-
Server
-
Socket creation:
int sockfd = socket(domain, type, protocol)
-
sockfd: socket descriptor, an integer (like a file-handle)
-
domain: integer, communication domain e.g., AF_INET (IPv4 protocol) , AF_INET6 (IPv6 protocol)
-
type: communication type SOCK_STREAM: TCP(reliable, connection oriented) SOCK_DGRAM: UDP(unreliable, connectionless)
-
protocol: protocol value for Internet Protocol(IP), which is 0. This is the same number which appears on protocol field in the IP header of a packet.(man protocols for more details)
-
-
Setsockopt:
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
This helps in manipulating options for the socket referred by the file descriptor sockfd. This is completely optional, but it helps in reuse of address and port. Prevents error such as: “address already in use”.
-
Bind:
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
-
Listen:
int listen(int sockfd, int backlog);
-
Accept:
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
-
Client
-
Socket connection: Exactly same as that of server’s socket creation.
-
Connect:
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. Server’s address and port is specified in addr.
Persistent Connection - It does not require connection setup again and again. Multiple objects can use connection.
Non Persistent - It requires connection setup again and again for each object to send.
Time taken by persistent or non persistent depends on size of files to be downloaded, system configuration and various other factors.
Time Persistent
real 0m0.156s
user 0m0.020s
sys 0m0.084s
Time Non Persistent
real 0m0.133s
user 0m0.044s
sys 0m0.036s
The time taken in user mode is more for persistent as compared to non persistent as expected but
the server in non persistent send more data in first time as compared to persistent in which data is send at
later more is the main reson for sys timing hence real timing.