Socket Programming Flashcards
What is socket programming about?
It refers to the programming of processes that communicate with eachother using an API known as sockets
Describe the data route of a segment through the OSI model from client to server..
- Web client (app layer) 2. TCP (transport layer) 3. IP (network layer) 4. Ethernet driver (datalink layer) <–> 5. ethernet driver 6. IP 7. TCP 8. webserver.
What transport layer protocols can be used for communication between client and server?
UDP (Datagrams, SOCK_DGRAM)
TCP (Connection, SOCK_STREAM)
What is the difference between IP and UDP
IP:
Operates at the network layer (Layer 3).
Connectionless and handles packet routing and addressing.
Small header with source and destination IP addresses.
Does not provide reliability, error checking, or flow control.
Fundamental to the internet.
UDP:
Operates at the transport layer (Layer 4).
Connectionless and provides a minimal transport layer.
Small header with source and destination port numbers.
Does not guarantee reliability, order, or error handling.
Used for real-time applications with low latency requirements (e.g., streaming, gaming, VoIP).
Explain what is taking place in this code:
int main(int argc, char **argv) {
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2) err_quit("usage: a.out <IPaddress>"); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]); if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); while ((n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0); }
This C code initializes a simple network client for retrieving the current time from a daytime server.
0. Parses an IP address from the command line.
1. Creates a TCP socket.
2. Sets up the server’s address (IP and port).
3. Establishes a connection with the server.
4. Reads and prints the time from the server.
Explain this code:
int main(int argc, char **argv) {
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server */ Bind(listenfd, (SA *)&servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); for (;;) { connfd = Accept(listenfd, (SA *)NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } }
Simple Daytime Server
- Creates a TCP server socket.
- Initializes the server address.
- Binds the socket to a specific port.
- Listens for incoming connections.
- Accepts client connections.
- Retrieves the current time.
- Sends the time to connected clients.
- Closes the client connections in a loop.
Explain how a iterative server works(initiates connection)
The server process waits for connections in the accept call.
TCP connections are established using a three-way handshake.
accept returns a new descriptor (connfd) after the handshake.
The server handles one client at a time.
Multiple client connections are queued and processed one at a time.
What are connect, accept and close, bind, functions in relation to socket programming?
connect: Used to establish a connection to a remote server in client code. It initiates the three-way handshake for TCP communication.
accept: Used in server code to accept incoming client connections. It blocks until a client connects and returns a new socket descriptor for communication with the client. close: Closes a socket when communication is complete. It releases resources associated with the socket and terminates the connection. socket: creates a socket and defines its properties. bind: associates the socket with a local IP address and port. listen: prepares the socket to accept incoming client connections, specifying the maximum backlog of pending connections.
When during establishing a client-server connection is the TCP handshake?
After server has created “socket”, “bind” and “listen” and when the client sends a “connect”. Continues hanshake by server sending syn ack and also “accept”
Defien three way hanskahe in terms of syn, ack but also in terms of programing functions(accept,bind, listen, socket, connect)
- SERVER: socket bind listen 1. CLIENT: Syn (Connect), 2. SERVER: Syn-Ack (accept) 3. CLIENT: connect return, ACK . 4. accept returns, read
testing
Describe TCP connection termination with ACK, FIN, SYN and socket programming functions
- CLIENT: close, FIN 2. SERVER: read returns 0, close, ACK, SEND FIN 3. CLIENT: Receive close, ACK.
Look at the TCP state Transition diagram an EXPLAIN THAT SHIT MF
look at the diagram yo
What is the state that an active-close-end goes through in TCP’s full-duplex connection termination, and how long does it typically remain in this state?
An active-close-end goes through the “TIME_WAIT” state and typically remains in this state for a duration of 2 times the maximum segment lifetime (MSL). The MSL is the maximum time that any IP datagram can live in a network. The recommended value for MSL, as per RFC 1122, is 2 minutes, but BSD systems commonly use 30 seconds, which means the TIME_WAIT duration typically falls between 1 and 4 minutes.