TCP/IP Programming
This section provides detailed information on using the Windows Sockets API for Transmission Control Protocol/Internet Protocol (TCP/IP) programming. TCP/IP is a suite of communication protocols used to interconnect network devices on the internet.
Introduction to TCP/IP Sockets
TCP/IP sockets offer a reliable, connection-oriented communication mechanism. Before data can be exchanged, a connection must be established between the client and the server. This involves a handshake process to ensure both parties are ready to communicate.
Key Functions and Structures
Here are some of the fundamental functions and data structures used in TCP/IP socket programming:
Socket Creation and Initialization
socket(): Creates a socket.bind(): Associates a local address and port with a socket.listen(): Places a socket in a state where it can accept incoming connection requests.accept(): Extracts the first connection request on the queue of pending connections for a listening socket, creates a new socket with the same properties of the listening socket but with a new connection, and generates a connection.connect(): Establishes a connection to a remote socket.
Data Transmission
send(): Sends data on a connected socket.recv(): Receives data from a connected socket.sendto(): Sends data to a specific address.recvfrom(): Receives data from a specific address.
Socket Closure
closesocket(): Closes an existing socket.
Example: A Simple TCP/IP Server
Below is a conceptual snippet demonstrating the server-side of a TCP/IP connection:
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
// ... initialization and error handling ...
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket == INVALID_SOCKET) {
// Handle error
}
SOCKADDR_IN server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080); // Example port
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
// Handle error
}
if (listen(server_socket, SOMAXCONN) == SOCKET_ERROR) {
// Handle error
}
SOCKET client_socket = accept(server_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET) {
// Handle error
}
char buffer[512];
int bytes_received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_received > 0) {
buffer[bytes_received] = '\\0';
// Process received data
send(client_socket, "Hello from server!", 18, 0);
}
closesocket(client_socket);
closesocket(server_socket);
WSACleanup();
Example: A Simple TCP/IP Client
Here is a conceptual snippet for a TCP/IP client:
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
// ... initialization and error handling ...
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_socket == INVALID_SOCKET) {
// Handle error
}
SOCKADDR_IN server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080); // Port of the server
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP address
if (connect(client_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
// Handle error
}
send(client_socket, "Hello from client!", 18, 0);
char buffer[512];
int bytes_received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_received > 0) {
buffer[bytes_received] = '\\0';
// Process received data
}
closesocket(client_socket);
WSACleanup();