// Example of using sockets in C++
#include
#include
#include
#include
#include
#include
int main() {
// Create socket
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
std::cerr << "Error creating socket" << std::endl;
return 1;
}
// Bind socket to an address
sockaddr_in server_address;
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 a connection
int client_socket = accept(server_socket, nullptr, nullptr);
// Send a message to the client
const char *message = "Hello from server!";
send(client_socket, message, strlen(message), 0);
// Close sockets
close(client_socket);
close(server_socket);
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?