Socket Programming Lecture Notes
Document Details

Uploaded by SelfRespectBurgundy
2025
Tags
Summary
These lecture notes cover socket programming concepts including the client-server model, TCP and UDP socket APIs, concurrent server design, and byte order. It explains key concepts such as sockets, protocols, and commonly used functions like socket(), bind(), listen(), accept(), connect(), send(), recv(), and close(). The notes also provide example flow summaries for both server and client implementations using TCP and UDP.
Full Transcript
Socket Programming Lecture Date 10/04/2025 Goals We plan to learn the following from this lecture: What is a socket? The client-server model TCP socket API UDP socket API Concurrent server design Byte order Key Concepts Sockets: Endpoints for network communication, identified by an...
Socket Programming Lecture Date 10/04/2025 Goals We plan to learn the following from this lecture: What is a socket? The client-server model TCP socket API UDP socket API Concurrent server design Byte order Key Concepts Sockets: Endpoints for network communication, identified by an IP address and port number. Client-Server Model: A common architecture where a server listens for connections from clients. Protocols: Rules that govern how data is transmitted (e.g., TCP for reliable, ordered data, UDP for faster, unordered data). Functions: Socket programming typically involves functions like socket(), bind(), listen(), accept(), connect(), send(), recv(), and close(). Socket Programming Socket programming allows applications to communicate over a network by establishing a connection between two endpoints, represented by sockets, using protocols like TCP or UDP. It's a fundamental concept for building network applications. Types of Sockets: Stream Socket (TCP): Reliable, connection-oriented Datagram Socket (UDP): Fast, connectionless "Think of a socket as a phone — it connects you to another person (device) to talk (send/receive data)." The Client-Server Model Client Server A network communication model where: Requests Provides service service Client initiates the request Passive Active, always Server provides services or data listening Characteristics: Temporary Always ready connection 1. socket() Purpose: Creates a new socket (communication endpoint). int sockfd = socket(int domain, int type, int protocol); domain: Address family (e.g., AF_INET for IPv4) type: Socket type (SOCK_STREAM for TCP, SOCK_DGRAM for UDP) protocol: Usually 0 for default Used by: Both Server & Client Returns: A file descriptor (socket ID), or -1 on error. 2. bind() Purpose: Assigns an IP address and port to the socket. bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); Required for server to define where to listen (IP + Port). Not usually needed for clients unless binding to a specific port/IP. Used by: Server 3. listen() Purpose: Marks the socket as passive, ready to accept incoming connections. listen(int sockfd, int backlog); backlog: Number of pending connections the queue will hold. Used by: Server 4. accept() Purpose: Accepts a new incoming connection on a listening socket. int clientfd = accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); Blocks until a connection is made. Returns a new socket descriptor for the client. Used by: Server 5. connect() Purpose: Initiates a connection to a server socket. connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); Used by client to connect to server’s IP & port. Returns 0 on success, -1 on error. Used by: Client 6. send() Purpose: Sends data over a connected socket. send(int sockfd, const void *buffer, size_t length, int flags); Sends bytes of data. flags is often set to 0. Used by: Both Server & Client 7. recv() Purpose: Receives data from a connected socket. recv(int sockfd, void *buffer, size_t length, int flags); Waits for data from peer. Returns number of bytes received. Used by: Both Server & Client 8. close() Purpose: Closes the socket and releases resources. close(int sockfd); Used by: Both Server & Client Server Example Flow Summary int sockfd = socket(AF_INET, SOCK_STREAM, 0); bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); listen(sockfd, 5); int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen); recv(clientfd, buffer, size, 0); send(clientfd, response, size, 0); close(clientfd); close(sockfd); Client Example Flow Summary int sockfd = socket(AF_INET, SOCK_STREAM, 0); connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); send(sockfd, message, size, 0); recv(sockfd, buffer, size, 0); close(sockfd); TCP Socket API As shown in the figure, the steps for establishing a TCP socket on the client side are the following: Create a socket using the socket() function; Connect the socket to the address of the server using the connect() function; Send and receive data by means of the read() and write() functions. Close the connection by means of the close() function. The steps involved in establishing a TCP socket on the server side are as follows: Create a socket with the socket() function; Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server. Send and receive data by means of send() and receive(). Close the connection by means of the close() function. UDP client-server. UDP client-server. As shown in the Figure, the steps of establishing a UDP socket communication on the client side are as follows: Create a socket using the socket() function; Send and receive data by means of the recvfrom() and sendto() functions. The steps of establishing a UDP socket communication on the server side are as follows: Create a socket with the socket() function; Bind the socket to an address using the bind() function; Send and receive data by means of recvfrom() and sendto(). Coming Byte order Concurrent Servers