1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// server.cpp #include <iostream> #include <cstring> // For memset #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> const int PORT = 8080; const int BUFFER_SIZE = 1024; int main() { // Create a socket int server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == 0) { std::cerr << "Socket creation failed\n"; return -1; } // Define the server address struct sockaddr_in address; int addrlen = sizeof(address); address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // Bind the socket to the address if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { std::cerr << "Bind failed\n"; return -1; } // Listen for incoming connections if (listen(server_fd, 3) < 0) { std::cerr << "Listen failed\n"; return -1; } // Accept a connection int new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen); if (new_socket < 0) { std::cerr << "Accept failed\n"; return -1; } char buffer[BUFFER_SIZE] = {0}; read(new_socket, buffer, BUFFER_SIZE); std::cout << "Received message: " << buffer << std::endl; // Close the socket close(new_socket); close(server_fd); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// client.cpp #include <iostream> #include <cstring> // For memset #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> const int PORT = 8080; const int BUFFER_SIZE = 1024; int main() { // Create a socket int sock = 0; struct sockaddr_in serv_addr; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { std::cerr << "Socket creation error\n"; return -1; } // Define the server address serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 address from text to binary form if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { std::cerr << "Invalid address or address not supported\n"; return -1; } // Connect to the server if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { std::cerr << "Connection failed\n"; return -1; } // Send a message const char* message = "Hello from client"; send(sock, message, strlen(message), 0); std::cout << "Message sent\n"; // Close the socket close(sock); return 0; } |
Explanation
Server (server.cpp):
- Socket Creation:
socket(AF_INET, SOCK_STREAM, 0)
: Creates a TCP socket.AF_INET
: Address family for IPv4.SOCK_STREAM
: Specifies a TCP socket.
- Bind Socket:
bind()
: Binds the socket to the specified address and port.
- Listen and Accept:
listen()
: Marks the socket as passive and ready to accept incoming connections.accept()
: Waits for an incoming connection and accepts it.
- Read Data:
read()
: Reads data from the socket into a buffer.
- Close Socket:
close()
: Closes the socket connections.
Client (client.cpp):
- Socket Creation:
socket(AF_INET, SOCK_STREAM, 0)
: Creates a TCP socket.
- Server Address:
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
: Converts IP address to binary form.
- Connect to Server:
connect()
: Connects the client socket to the server’s address.
- Send Data:
send()
: Sends a message to the server.
- Close Socket:
close()
: Closes the socket connection.
Usage
- Compile:
- Compile the server:
g++ -o server server.cpp
- Compile the client:
g++ -o client client.cpp
- Compile the server:
- Run:
- Start the server:
./server
- Run the client:
./client
- Start the server: