Unix socket I/O primitives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr )&address, (socklen_t)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, BUFFER_SIZE);
printf("%s\n", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
This C program demonstrates basic Unix socket I/O primitives for a server. It creates a socket, binds it to a specific port, listens for incoming connections, accepts a connection, receives data from the client, sends a response, and closes the socket.
Explanation of steps:
Include Headers: Include necessary headers for socket programming (
stdio.h
,stdlib.h
,string.h
,unistd.h
,arpa/inet.h
).Define Constants: Define constants for the port number (
PORT
) and buffer size (BUFFER_SIZE
).Declare Variables: Declare variables for the server socket file descriptor (
server_fd
), client socket file descriptor (new_socket
), socket options (opt
), address structure (address
), address length (addrlen
), and a buffer for data exchange (buffer
).Create Socket: Create a socket using the
socket
system call. Check for errors.Set Socket Options: Set socket options to allow reuse of the address and port using
setsockopt
. Check for errors.Configure Address Structure: Configure the server address structure (
address
) with the specified port and any local IP address.Bind Socket: Bind the socket to the specified address using the
bind
system call. Check for errors.Listen for Connections: Listen for incoming connections with a queue size of 3 using the
listen
system call. Check for errors.Accept Connection: Accept a connection from the client using the
accept
system call. Check for errors.Read Data from Client: Read data from the client into the
buffer
using theread
system call.Print Received Data: Print the received data from the client.
Send Response to Client: Send a "Hello from server" message to the client using the
send
system call.Print Sent Message: Print a confirmation message indicating that the response has been sent.
Close Sockets: Close the server and client sockets using the
close
system call.
Note: Error checking is included to handle potential failures in socket creation, binding, listening, and accepting connections.