Server Program (server.cpp
)
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 55 56 57 58 |
#include <iostream> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <cstring> const int PORT = 8080; const int BUFFER_SIZE = 1024; int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); char buffer[BUFFER_SIZE] = {0}; const char* response = "Message received"; // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { std::cerr << "Socket creation error" << std::endl; return -1; } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // Bind the socket if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { std::cerr << "Bind failed" << std::endl; return -1; } // Listen for incoming connections if (listen(server_fd, 3) < 0) { std::cerr << "Listen failed" << std::endl; return -1; } // Accept a connection if ((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) { std::cerr << "Accept failed" << std::endl; return -1; } // Read message from client read(new_socket, buffer, BUFFER_SIZE); std::cout << "Received: " << buffer << std::endl; // Send response to client send(new_socket, response, strlen(response), 0); std::cout << "Response sent" << std::endl; // Close sockets close(new_socket); close(server_fd); return 0; } |
client.cpp
)
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 |
#include <iostream> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <cstring> const int PORT = 8080; const int BUFFER_SIZE = 1024; int main() { int sock = 0; struct sockaddr_in serv_addr; char buffer[BUFFER_SIZE] = {0}; const char* message = "Hello from client"; // Creating socket file descriptor if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { std::cerr << "Socket creation error" << std::endl; return -1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary form if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { std::cerr << "Invalid address / Address not supported" << std::endl; return -1; } // Connect to the server if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { std::cerr << "Connection failed" << std::endl; return -1; } // Send message to server send(sock, message, strlen(message), 0); std::cout << "Message sent" << std::endl; // Read response from server read(sock, buffer, BUFFER_SIZE); std::cout << "Response from server: " << buffer << std::endl; // Close socket close(sock); return 0; } |
Explanation:
- Server Program (
server.cpp
):- Socket Creation: Creates a socket using
socket()
. - Binding: Binds the socket to an IP address and port using
bind()
. - Listening: Sets the socket to listen for incoming connections using
listen()
. - Accepting Connections: Accepts a connection from a client using
accept()
. - Reading/Writing: Reads data from the client and sends a response using
read()
andsend()
. - Cleanup: Closes the sockets with
close()
.
- Socket Creation: Creates a socket using
- Client Program (
client.cpp
):- Socket Creation: Creates a socket using
socket()
. - Connecting: Connects to the server using
connect()
. - Sending/Receiving: Sends a message to the server and reads the response using
send()
andread()
. - Cleanup: Closes the socket with
close()
.
- Socket Creation: Creates a socket using
Notes:
- This example uses POSIX sockets and is intended for Unix-like systems. On Windows, the socket API would differ, and you would need to include
winsock2.h
and initialize the Winsock library. - To run these programs, compile them separately and start the server before running the client. For example, use
g++ server.cpp -o server
andg++ client.cpp -o client
. Then run./server
and./client
in separate terminal windows.