This README provides a comprehensive cheatsheet for socket programming in C++, covering key definitions, socket types, basic server-client code structure, system calls, and common debugging techniques. It is designed for students and professionals seeking a quick reference for socket programming concepts and coding patterns.
- Protocol: TCP
- Use Case: Reliable, connection-oriented communication.
- Protocol: UDP
- Use Case: Connectionless, fast, but not reliable.
- Protocol: IP Protocol
- Use Case: Direct access to lower network layers.
- Protocol: TCP/UDP
- Use Case: Listens for incoming client connections.
- Protocol: TCP/UDP
- Use Case: Connects to server to send/receive data.
Include the necessary header files for socket programming:
```cpp #include #include #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> ```
Create a socket using the socket()
function:
```cpp int sockfd = socket(AF_INET, SOCK_STREAM, 0); ```
Bind an address to a socket with bind()
:
```cpp struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(8080);
bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); ```
Set the socket to listen for connections and accept incoming connections:
```cpp listen(sockfd, 5); int client_socket = accept(sockfd, (struct sockaddr*)NULL, NULL); ```
To connect to a server using a client socket:
```cpp struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); ```
- socket(): Creates a socket and returns a file descriptor.
- bind(): Binds a socket to a specific IP address and port.
- listen(): Prepares a socket to accept incoming connections.
- accept(): Blocks the program until a connection is received.
- connect(): Connects a client to a remote server.
- send(): Sends data to a connected socket.
- recv(): Receives data from a connected socket.
- close(): Closes a socket.
Use perror()
to print error messages:
```cpp if (sockfd < 0) { perror("Socket creation failed"); } ```
```cpp #include #include #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h>
int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address);
server_fd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 3);
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
char *message = "Hello from server";
send(new_socket, message, strlen(message), 0);
close(server_fd);
return 0;
} ```
```cpp #include #include #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h>
int main() { int sock = 0; struct sockaddr_in serv_addr; char buffer[1024] = {0};
sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
send(sock, "Hello from client", strlen("Hello from client"), 0);
read(sock, buffer, 1024);
std::cout << buffer << std::endl;
close(sock);
return 0;
} ```
- Address Already in Use: Occurs when binding to an already-used port.
- Connection Refused: The server may not be listening on the specified port.
- Broken Pipe: Happens when the server terminates unexpectedly.
- Use
netstat -tulnp
to check active ports. - Use
strace
orgdb
for in-depth debugging.