TCP_SOCKET_SERVER
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#define PORT 5000
#define MAXLINE 1024
int main() {
int sockfd, newsockfd, n;
char buffer[MAXLINE];
char response[] = "Hello Client!";
struct sockaddr_in servaddr, cliaddr;
socklen_t len = sizeof(cliaddr);
// Step 1: Create a TCP socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}
// Step 2: Clear the server address structure
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY; // Listen on all available
interfaces
// Step 3: Bind the socket to the address and port
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("Bind failed");
close(sockfd);
exit(1);
}
// Step 4: Start listening for incoming connections (max 5 pending
clients)
if (listen(sockfd, 5) < 0) {
perror("Listen failed");
close(sockfd);
exit(1);
}
printf("Server is listening on port %d...\n", PORT);
// Step 5: Accept incoming client connections
newsockfd = accept(sockfd, (struct sockaddr *)&cliaddr, &len);
if (newsockfd < 0) {
perror("Accept failed");
close(sockfd);
exit(1);
}
// Step 6: Receive message from client
n = recv(newsockfd, buffer, MAXLINE, 0);
if (n < 0) {
perror("Receive failed");
close(newsockfd);
close(sockfd);
exit(1);
}
buffer[n] = '\0'; // Null terminate the received string
printf("Received from client: %s\n", buffer);
// Step 7: Send a response back to the client
send(newsockfd, response, strlen(response), 0);
// Step 8: Close the sockets
close(newsockfd);
close(sockfd);
return 0;
}
CLIENT
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#define PORT 5000
#define MAXLINE 1024
int main() {
int sockfd, n;
char buffer[MAXLINE];
char *message = "Hello Server";
struct sockaddr_in servaddr;
// Step 1: Create a TCP socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}
// Step 2: Set up the server address structure
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
// Step 3: Convert the server IP address to binary form
if (inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) <= 0) {
perror("Invalid address or Address not supported");
exit(1);
}
// Step 4: Connect to the server
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) <
0) {
perror("Connection failed");
close(sockfd);
exit(1);
}
// Step 5: Send message to the server
send(sockfd, message, strlen(message), 0);
printf("Message sent to server: %s\n", message);
// Step 6: Receive response from the server
n = recv(sockfd, buffer, MAXLINE, 0);
if (n < 0) {
perror("Receive failed");
close(sockfd);
exit(1);
}
buffer[n] = '\0'; // Null terminate the received string
printf("Server response: %s\n", buffer);
// Step 7: Close the socket
close(sockfd);
return 0;
}