### What is maximum segment lifetime (MSL)? Explain each states in TCP state transition along with suitable diagram. The Maximum Segment Lifetime (MSL) is a parameter in TCP (Transmission Control Protocol) that defines the maximum time a segment can exist in the network before it is discarded. It ensures that TCP segments do not circulate indefinitely in the network, which helps prevent stale or outdated segments from causing issues. ![](https://hackmd.io/_uploads/ByHdBA0jh.png) 1. CLOSED: This is the initial state of a TCP connection. It means that no connection exists. 2. LISTEN: In this state, a TCP server is waiting for incoming connection requests from clients. 3. SYN-SENT: After a client initiates a connection request, it enters this state. The client sends a SYN packet to the server to establish the connection. 4. SYN-RECEIVED: When the server receives the SYN packet, it enters this state. The server acknowledges the client's SYN packet and sends its own SYN packet. 5. ESTABLISHED: Once the client receives the SYN-ACK packet from the server, it enters this state. Both sides have exchanged the necessary SYN packets, and the connection is established. Data transfer can occur in this state. 6. FIN-WAIT-1: When a party (either the client or server) wants to terminate the connection, it enters this state. The party sends a FIN packet to the other party, indicating its intention to close the connection. 7. CLOSE-WAIT: When the other party receives the FIN packet, it enters this state. It acknowledges the FIN packet and notifies its own application that the remote party intends to close the connection. 8. FIN-WAIT-2: The party that initiated the connection termination enters this state after receiving an acknowledgment for its FIN packet. 9. LAST-ACK: When the other party is ready to close the connection, it enters this state. It sends a FIN packet to acknowledge the received FIN packet and informs the local application about the connection closure. 10. TIME-WAIT: After sending the FIN packet, each party enters this state. It waits for a period of MSL (maximum segment lifetime) before transitioning to the CLOSED state. The MSL ensures that any delayed or duplicate segments are cleared from the network before the connection is fully terminated. 11. CLOSED: Finally, after the TIME-WAIT state, the connection transitions to the CLOSED state, and no further communication is possible. ### Describe TCP, UDP and SCTP. **TCP** Connection: TCP provides connections between clients and servers. A TCP client establishes a connection with a server, exchanges data across the connection, and then terminates the connection. Reliability: TCP requires acknowledgment when sending data. If an acknowledgment is not received, TCP automatically retransmits the data and waits a longer amount of time. Round-trip time (RTT): TCP estimates RTT between a client and server dynamically so that it knows how long to wait for an acknowledgment. Sequencing: TCP associates a sequence number with every byte (segment, unit of data that TCP passes to IP.) it sends. TCP reorders out-of-order segments and discards duplicate segments. Flow control Full-duplex: an application can send and receive data in both directions on a given connection at any time **SCTP** Association instead of "connection": An association refers to a communication between two systems, which may involve more than two addresses due to multihoming. Message-oriented: provides sequenced delivery of individual records. Like UDP, the length of a record written by the sender is passed to the receiving application. Multihoming: allows a single SCTP endpoint to support multiple IP addresses. This feature can provide increased robustness against network failure. **UDP** UDP is a simple transport-layer protocol. The application writes a “datagram” to a UDP socket, which is encapsulated as either an IPv4 datagram or an IPv6 datagram. It is then sent to its destination. But there is no guarantee that a UDP datagram ever reaches its final destination. Problem that is encountered with network programming using UDP is its lack of reliability. If it is required to be certain that a datagram reaches its destination, numerous features will have to be built into our application such as acknowledgements from the other end, timeouts, retransmissions, etc. Each UDP datagram has a length. If the datagram reaches its final destination correctly, then the length of the datagram is passed to the receiving application. As UDP provides a connectionless service, there need not be any long-term relationship between a UDP client and server. For instance a UDP client can create a socket and send a datagram to a given server and then immediately send another datagram on the same socket to a different server. Similarly a UDP server can receive five datagrams in a row on a single UDP socket, each from five different clients. ### Which function is responsible for sending SYN segment during TCP connection establishment phase? Illustrate the TCP 3-way and TCP 4-way handshake mechanism with suitable state transition diagram. It is handled by the underlying network stack when you call functions to establish a TCP connection. This is usually done via the `connect()` function, which initiates the TCP three-way handshake under the hood. ![](https://hackmd.io/_uploads/HJeNuLkhn.png) - Server: passive open, by calling socket, bind, and listen - Client: active open, by calling connect. The client TCP to send a "synchronize" (SYN) segment with no data but it contains client's initial sequence number for the data to be sent on the connection. - Server: acknowledges (ACK) client's SYN. The server sends its SYN and the ACK of the client's SYN in a single segment which also contains its own SYN containing the initial sequence number for the data to be sent on the connection. - Client: acknowledges the server's SYN. ![](https://hackmd.io/_uploads/B18YuIy2n.png) It takes four segments to terminate a connection: - One end calls close first by sending a FIN segment to mean it is finished sending data. This is called active close. - The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP (sending an ACK segment). The receipt of the FIN is also passed to the application as an end-of-file. - Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN. - The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN ### Describe the application of Network Programming? 1. **Web Applications**: Web applications like Google, Facebook, Amazon, and many more use network programming to communicate between the client's browser and the web servers. Every time you make a request from your browser, such as clicking a link or submitting a form, network programming is used to send that request to the server and receive the server's response. 2. **Email Systems**: Protocols such as SMTP, POP3, and IMAP are used to send and receive emails, and network programming is used to implement these protocols. 3. **File Transfer**: Applications that transfer files over the internet, such as FTP clients, torrent applications, or cloud storage services like Dropbox or Google Drive, all require network programming. 4. **Chat and Messaging Applications**: Network programming is heavily used in instant messaging, chat applications, and VoIP systems like Skype or Zoom. These applications require real-time communication between clients, often involving direct connections or using servers to relay messages. 5. **Distributed Systems**: In distributed systems, tasks are divided among multiple computers. These computers need to communicate with each other to coordinate their work, which is done using network programming. 6. **Games**: Multiplayer online games use network programming to sync the game state between multiple players. Real-time games often use UDP for this, while turn-based games may use TCP. 7. **Internet of Things (IoT)**: IoT devices, like smart home devices, wearable tech, connected vehicles, etc., all use network programming to communicate with each other and with the cloud. 8. **Remote Databases**: Applications often need to communicate with a database that isn't located on the same machine. Network programming is used to make these connections and transmit data between the application and the database. ### Explain the client/server mode of communication. Client-server communication is a setup where clients (devices or apps) request services or data from servers. Servers respond to these requests. Steps: - Client Setup: Clients prepare to send requests. - Server Setup: Servers listen for incoming requests. - Client Connect: Clients connect to servers. - Server Accept: Servers accept client connections. - Data Exchange: Clients send requests, servers respond. - Close: Either side closes the connection. Benefits: - Scalability: Multiple clients can interact with servers. - Reliability: Centralized management improves reliability. - Security: Servers can enforce security measures. Examples: Web browsing, email, gaming, databases. ![](https://hackmd.io/_uploads/H1O2qI122.png) ### Compare TCP and UDP on the basis of frame format. TCP Frame Format: - Header Length: 20 bytes minimum, up to 60 bytes (without options). - Reliability: Reliable, ensures data delivery and ordering. - Connection: Connection-oriented (establishes a connection before data exchange). - Flow Control: Automatic flow control using sliding window mechanism. - Error Checking: Includes error checking (checksum). - Acknowledgments: Uses acknowledgments and sequence numbers for data delivery. - Congestion Control: Implements congestion control to avoid network congestion. - Ordering: Ensures data arrives in order. - Overhead: Higher overhead due to reliability mechanisms. UDP Frame Format: - Header Length: 8 bytes fixed. - Reliability: Unreliable, no guarantees on data delivery or ordering. - Connection: Connectionless, no prior setup needed. - Flow Control: No flow control mechanism. - Error Checking: Includes error checking (checksum). - Acknowledgments: No acknowledgments or sequence numbers. - Congestion Control: No built-in congestion control. - Ordering: Does not guarantee data order. - Overhead: Lower overhead compared to TCP. ### What is network programming? Network programming is the art of creating software applications that communicate and exchange data over computer networks, like the internet. It involves writing code that allows different devices, often located far apart, to share information and work together. These applications can perform tasks like sending messages, downloading files, streaming videos, or playing online games. ### Explain different communication protocols used in networking. Transmission Control Protocol (TCP): - TCP is a reliable, connection-oriented protocol. - It ensures data integrity, sequencing, and error correction. - Widely used for applications requiring guaranteed delivery, such as web browsing and file transfer. - Follows a handshaking process to establish and terminate connections. User Datagram Protocol (UDP): - UDP is a lightweight, connectionless protocol. - It offers low overhead and fast transmission but does not guarantee data delivery or order. - Suitable for real-time applications like online gaming, streaming, and VoIP, where speed is crucial. Internet Protocol (IP): - IP is responsible for addressing and routing packets of data across networks. - IP addresses uniquely identify devices in a network. - IPv4 and IPv6 are the two main versions of IP. Hypertext Transfer Protocol (HTTP): - HTTP is used for transferring web pages and resources on the World Wide Web. - Defines how a client (browser) requests web content and how the server responds. File Transfer Protocol (FTP): - FTP is designed for transferring files between a client and a server. - Supports both interactive (user-initiated) and automated transfers. Simple Mail Transfer Protocol (SMTP): - SMTP is used for sending and relaying email messages between email servers. - Responsible for routing emails to their destination. Domain Name System (DNS): - DNS translates human-readable domain names (e.g., www.example.com) into IP addresses. - Enables users to access resources using meaningful names rather than numeric IP addresses. Secure Shell (SSH): - SSH provides secure, encrypted communication for remote access to systems. - Used for secure remote administration and file transfers. WebSocket: - WebSocket enables full-duplex communication between a client and a server over a single, long-lived connection. - Allows real-time data exchange, making it suitable for applications requiring constant updates, like chat applications. ### Define Computer network and network programming? Computer Network: - Computer network is a large number of separate computers that are interconnected to exchange data and information. - In computer network, users directly interact with the actual machine to invoke the data exchange and the system do not attempt to make the computers or machines to act coherently. Uses of computer network 1. Business Applications Resource sharing (Availability of programs, equipments and data to anyone on the network). E-commerce 2. Home Applications Access to remote information Communication Entertainment Network Programming: Network programming involves writing software that enables communication and data exchange between devices over a network. It includes creating applications that utilize protocols and APIs to establish connections, send/receive data, and manage network-related tasks. Network programming allows developers to build applications ranging from web browsing to real-time online gaming, enabling devices to interact seamlessly across distances. ### What do you mean by Active Network Model? - Active network model is a communication model in which packets flowing through a network can dynamically change or modify the operation of the network. - The packets used are known as active packets. - Active networking places computation within packets traveling through the network. - It allows sending code along with packets of information allowing the data to change its form (code) to match the channel characteristics. - One of the challenges of active networking has been the inability of information theory to mathematically model the active network paradigm and enable active network engineering. ![](https://hackmd.io/_uploads/rySt2Ik3h.png) ### Compare peer to peer and Client/Server based Model on the basis of communication, cost and Security. | S.NO | Client-Server Network | Peer-to-Peer Network | |------|-----------------------------------------|-----------------------------------------| | 1. | Clients and server are differentiated, | Clients and server are not differentiated. | | | Specific server and clients are present.| | | 2. | Focuses on information sharing. | Focuses on connectivity. | | 3. | Centralized server is used to store the | Each peer has its own data. | | | data. | | | 4. | Server responds to services requested | Each node can request and respond to | | | by clients. | services. | | 5. | Costlier than Peer-to-Peer Network. | Less costly than Client-Server Network. | | 6. | More stable than Peer-to-Peer Network. | Less stable with increased peer count. | | 7. | Used for both small and large networks.| Generally suited for small networks. | ### Explain relationship between Socket, Port and IP with help of outline code. ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { // Create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); // Define IP address and port struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8080); // Port number serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address (localhost) // Bind the socket to an IP address and port (optional) bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); // Connect to a server using IP and port struct sockaddr_in clientAddr; clientAddr.sin_family = AF_INET; clientAddr.sin_port = htons(8080); // Port number clientAddr.sin_addr.s_addr = inet_addr("192.168.1.2"); // IP address // Send and receive data over the socket char buffer[1024] = "Hello, server!"; sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)); // Close the socket close(sockfd); return 0; } ``` - We create a socket using the socket() function. - We define an IP address and port using a struct sockaddr_in for both the server and the client. - We can optionally bind the socket to a specific IP address and port using the bind() function (typically done by servers). - We demonstrate connecting to a server using the client's IP address and port. - We send and receive data using sendto() (UDP) or other relevant functions (e.g., send(), recv() for TCP). - Finally, we close the socket using close(). ### Explain network programming model with help of suitable diagram. Client-Server Model - Data are stored in powerful computers known as server. - Server is managed by a system administrator. - Users have simple computers called clients. - Client and server computers are connected by a network. ![](https://hackmd.io/_uploads/SJq-kDJ2n.png) Peer-to-peer Model - Peer-to-peer model is a decentralized communication model. - Every member of a network have equal capacity. It means there is no any distinction of client and server computers. - For communication, any member is able to initiate the session. - It is difficult to filter network traffic that which files are being shared. - It is impossible to administrate the network. - A failure in the single peer does not fails the whole system. - It is very much scalable and robust. ![](https://hackmd.io/_uploads/Ski7JDy32.png) Active Network Model - Active network model is a communication model in which packets flowing through a network can - dynamically change or modify the operation of the network. - The packets used are known as active packets. - Active networking places computation within packets traveling through the network. - It allows sending code along with packets of information allowing the data to change its form (code) to match the channel characteristics. - One of the challenges of active networking has been the inability of information theory to mathematically model the active network paradigm and enable active network engineering. ![](https://hackmd.io/_uploads/H1NSkv12h.png) ### What do you understand by system calls used with sockets? Briefly describe any two of them. fork() System Call: - The fork() system call is used to create a new process, known as a child process, which is a copy of the current (parent) process. - After a successful fork(), two processes are running concurrently: the parent process and the newly created child process. - The child process is an exact copy of the parent, including code, data, and open file descriptors. However, it has a unique process ID (PID). exec() System Call: - The exec() system call is used to replace the current process's memory space with a new program. It loads a new executable into the current process's memory. - After a successful exec(), the original program code is replaced by the new program's code, and the new program starts executing. - Common variants of exec() include execl(), execv(), execle(), execve(), etc., each accepting different argument formats. These system calls are often used together in combination. For example, a program might use fork() to create a child process and then use exec() to replace the child's memory space with a new program. This is a common approach in Unix-like operating systems to create and run new processes with different programs. ### Describe different internet layer protocols in detail. - Internet Protocol, IP − It is a connectionless and unreliable protocol that provides a best effort delivery service. It transports data packets called datagrams that travel over different routes across multiple nodes. - Address Resolution Protocol, ARP −This protocol maps the logical address or the Internet address of a host to its physical address, as printed in the network interface card. - Reverse Address Resolution Protocol, RARP − This is to find the Internet address of a host when its physical address is known. - Internet Control Message Protocol, ICMP − It monitors sending the queries as well as the error messages. - Internet Group Message Protocol, IGMP −It allows the transmission of a message to a group of recipients simultaneously.