To build a TCP server in C++, you can follow these steps:
Create a socket using the
socket()function. Specify the address family, socket type, and protocol.TEXT/X-C++SRC1int sockfd = socket(AF_INET, SOCK_STREAM, 0); 2if (sockfd == -1) { 3 std::cout << "Failed to create socket" << std::endl; 4 return 1; 5}Bind the socket to an address and port using the
bind()function. Specify the server address and port in asockaddr_instructure.TEXT/X-C++SRC1struct sockaddr_in serverAddr; 2serverAddr.sin_family = AF_INET; 3serverAddr.sin_port = htons(8080); 4serverAddr.sin_addr.s_addr = INADDR_ANY; 5 6if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) { 7 std::cout << "Failed to bind socket" << std::endl; 8 return 1; 9}Listen for incoming connections using the
listen()function. Specify the maximum number of queued connections.TEXT/X-C++SRC1if (listen(sockfd, 5) == -1) { 2 std::cout << "Failed to listen for connections" << std::endl; 3 return 1; 4}Accept incoming connections using the
accept()function. Specify asockaddr_instructure to store the client address.TEXT/X-C++SRC1struct sockaddr_in clientAddr; 2unsigned int clientAddrSize = sizeof(clientAddr); 3int clientSock = accept(sockfd, (struct sockaddr*)&clientAddr, &clientAddrSize); 4if (clientSock == -1) { 5 std::cout << "Failed to accept connection" << std::endl; 6 return 1; 7}Send and receive data on the client socket using the
read()andwrite()functions.TEXT/X-C++SRC1char buffer[1024] = {0}; 2read(clientSock, buffer, 1024); 3std::cout << "Received message: " << buffer << std::endl; 4 5const char* response = "Hello from the server!"; 6write(clientSock, response, strlen(response));Close the client socket and server socket using the
close()function.TEXT/X-C++SRC1close(clientSock); 2close(sockfd);
Here's an example code that demonstrates how to build a TCP server in C++:
TEXT/X-C++SRC
1#include <iostream>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include <unistd.h>
5
6int main() {
7 // Create a socket
8 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
9 if (sockfd == -1) {
10 std::cout << "Failed to create socket" << std::endl;
11 return 1;
12 }
13
14 // Bind the socket to an address and port
15 struct sockaddr_in serverAddr;
16 serverAddr.sin_family = AF_INET;
17 serverAddr.sin_port = htons(8080);
18 serverAddr.sin_addr.s_addr = INADDR_ANY;
19
20 if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
21 std::cout << "Failed to bind socket" << std::endl;
22 return 1;
23 }
24
25 // Listen for incoming connections
26 if (listen(sockfd, 5) == -1) {
27 std::cout << "Failed to listen for connections" << std::endl;
28 return 1;
29 }
30
31 // Accept incoming connections
32 struct sockaddr_in clientAddr;
33 unsigned int clientAddrSize = sizeof(clientAddr);
34 int clientSock = accept(sockfd, (struct sockaddr*)&clientAddr, &clientAddrSize);
35 if (clientSock == -1) {
36 std::cout << "Failed to accept connection" << std::endl;
37 return 1;
38 }
39
40 // Send and receive data on the client socket
41 char buffer[1024] = {0};
42 read(clientSock, buffer, 1024);
43 std::cout << "Received message: " << buffer << std::endl;
44
45 const char* response = "Hello from the server!";
46 write(clientSock, response, strlen(response));
47
48 // Close the client socket
49 close(clientSock);
50
51 // Close the server socket
52 close(sockfd);
53
54 return 0;
55}xxxxxxxxxx59
}double calculate_interest(double principal, double rate, int years) { return principal * rate * years;}int main() { // Create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { std::cout << "Failed to create socket" << std::endl; return 1; } // Bind the socket to an address and port struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8080); serverAddr.sin_addr.s_addr = INADDR_ANY; if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) { std::cout << "Failed to bind socket" << std::endl; return 1; } // Listen for incoming connectionsOUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment



