C Language Socket Programming Tutorial204


Socket programming is a fundamental technique for network communication in C programming. It allows applications to establish and manage network connections, send and receive data, and perform various networking tasks.

Basics of Socket Programming

Socket programming in C revolves around the concept of sockets. A socket is a software endpoint that represents a communication channel between two applications running on different devices connected over a network. Socket programming involves creating a socket, binding it to a specific network address and port, and then using it to send and receive data.

The following steps outline the basic flow of socket programming:
Include the necessary header files (<stdio.h>, <stdlib.h>, <sys/socket.h>, <netinet/in.h>).
Create a socket using the socket() function. The function returns a socket descriptor, which is used to identify the socket.
Bind the socket to a specific network address and port using the bind() function. The network address can be an IP address or a domain name, and the port is a number assigned to the application.
Listen for incoming connections if you are creating a server application using the listen() function. This function puts the socket in a listening state, waiting for clients to connect.
Accept incoming connections on the server using the accept() function. This function returns a new socket descriptor for the newly established connection.
Send and receive data using the send() and recv() functions. These functions are used to send and receive data over the network.
Close the socket using the close() function when communication is complete.

Socket Types

There are two main types of sockets in C programming:
Stream Sockets: Used for reliable, connection-oriented communication where data is sent as a stream of bytes. TCP (Transmission Control Protocol) is a common example of a stream socket.
Datagram Sockets: Used for unreliable, connectionless communication where data is sent in individual packets. UDP (User Datagram Protocol) is a common example of a datagram socket.

Socket Addressing

Sockets are addressed using a combination of IP address and port number. The IP address identifies the device on the network, while the port number identifies the specific application or service running on that device.

Socket addressing is represented by the following structure:```c
struct sockaddr_in {
uint16_t sin_family; // Address family (AF_INET for IPv4)
uint16_t sin_port; // Port number
struct in_addr sin_addr; // IP address
};
```

Server-Client Model

Socket programming typically involves two roles: server and client.
Server: Listens on a specific port for incoming connections and services requests from clients.
Client: Initiates a connection to a server on a specific host and port to request and receive services.

Example Code

The following is an example of a simple server program written in C:```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int server_socket;
struct sockaddr_in server_address;
// Create a socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
// Bind the socket to a specific address and port
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
// Listen for incoming connections
listen(server_socket, 5);
// Accept incoming connections
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
// Receive data from client
char buffer[1024];
recv(client_socket, buffer, 1024, 0);
// Send data to client
send(client_socket, "Hello from server!", 18, 0);
// Close the sockets
close(client_socket);
close(server_socket);
return 0;
}
```

This server program listens on port 8080 and waits for incoming connections. When a client connects, the server receives data from the client, sends a response, and then closes the connection.

2024-12-06


Previous:How to De-Stress With Video Editing

Next:CNC Turning Programming for Beginners