5/27/25, 7:11 AM VTU Network Programming Questions
ockets Programming – Detailed Exploration
🔹 1. What is a Socket?
A socket is an endpoint for communication between two machines. It abstracts the details of
network communication and provides a consistent interface for writing distributed applications.
Socket pair = IP address + port number
🔹 2. Socket Types
There are three primary types of sockets:
Type Protocol Used Use Case
SOCK_STREAM TCP Reliable, connection
SOCK_DGRAM UDP Unreliable, connectio
SOCK_SEQPACKET SCTP Reliable, message-o
🔹 3. Elementary Socket Functions
These are the core functions used to build TCP/UDP clients and servers:
🖥️ Server Side (TCP)
1. socket() – Create a socket.
2. bind() – Bind the socket to an IP and port.
3. listen() – Listen for incoming connections.
4. accept() – Accept a new connection.
5. read() / recv() – Read from the socket.
6. write() / send() – Write to the socket.
7. close() – Close the connection.
🧑💻 Client Side (TCP)
1. socket() – Create a socket.
2. connect() – Connect to the server.
3. read() / recv() – Read from the socket.
https://chatgpt.com/c/68351653-3f10-8010-a055-130ce32b3968 1/3
5/27/25, 7:11 AM VTU Network Programming Questions
4. write() / send() – Write to the socket.
5. close() – Close the connection.
UDP is connectionless:
No connect() , accept() , or listen()
Use sendto() and recvfrom()
🔹 4. Advanced Socket Functions
These provide greater control and are essential for real-world applications:
select() , poll() , epoll() – Multiplex I/O over multiple sockets.
setsockopt() / getsockopt() – Set/get options (e.g., timeouts, reuseaddr).
getpeername() / getsockname() – Get details of remote/local socket.
fcntl() – Control file descriptor flags (e.g., non-blocking mode).
shutdown() – Close one or both directions of the connection.
🔹 5. Socket Address Structures
Commonly used structures:
c Copy Edit
struct sockaddr_in { short sin_family; // AF_INET u_short sin_port; // Port number
struct in_addr sin_addr; // IP address }; struct sockaddr_in6 { sa_family_t
sin6_family; // AF_INET6 in_port_t sin6_port; // Port number struct in6_addr
sin6_addr; // IPv6 address };
🔹 6. Blocking vs Non-Blocking Sockets
Blocking: Functions wait until the operation completes (default mode).
Non-blocking: Functions return immediately; useful for event-driven designs.
🔹 7. IPv6 and Dual-Stack Programming
https://chatgpt.com/c/68351653-3f10-8010-a055-130ce32b3968 2/3
5/27/25, 7:11 AM VTU Network Programming Questions
Use getaddrinfo() instead of gethostbyname() for protocol-independent coding.
Bind to :: to accept both IPv4 and IPv6 if dual-stack is enabled.
🔹 8. Error Handling
Most socket functions return -1 on error and set errno .
Use perror() or strerror(errno) for descriptive messages.
🔹 9. Example: TCP Echo Client/Server (Simplified)
Server (TCP)
c Copy Edit
int sockfd = socket(AF_INET, SOCK_STREAM, 0); bind(sockfd, (struct
sockaddr*)&servaddr, sizeof(servaddr)); listen(sockfd, 5); int connfd = accept(sockfd,
(struct sockaddr*)NULL, NULL); read(connfd, buffer, sizeof(buffer)); write(connfd,
buffer, strlen(buffer));
Client (TCP)
c Copy Edit
int sockfd = socket(AF_INET, SOCK_STREAM, 0); connect(sockfd, (struct
sockaddr*)&servaddr, sizeof(servaddr)); write(sockfd, message, strlen(message));
read(sockfd, buffer, sizeof(buffer));
📚 Further Reading in the Book
Chapter 4–5: Elementary TCP Sockets
Chapter 6: Iterative vs. Concurrent Servers
Chapter 7: Advanced Functions
Chapter 11: UDP Sockets
Chapter 16–17: SCTP
https://chatgpt.com/c/68351653-3f10-8010-a055-130ce32b3968 3/3