Assignment Lab 5
Assignment Lab 5
TCP Server in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
char buffer[BUF_SIZE];
// Create socket
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
if (listen(server_fd, 5) == -1) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
if (bytes_received == -1) {
perror("Receive failed");
close(client_fd);
close(server_fd);
exit(EXIT_FAILURE);
close(client_fd);
close(server_fd);
return 0;
}
TCP Client in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock;
char buffer[BUF_SIZE];
// Create socket
exit(EXIT_FAILURE);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
// Convert IP address to binary format
perror("Invalid address");
close(sock);
exit(EXIT_FAILURE);
perror("Connection failed");
close(sock);
exit(EXIT_FAILURE);
if (bytes_received == -1) {
perror("Receive failed");
close(sock);
exit(EXIT_FAILURE);
}
close(sock);
return 0;
How to execute
1. Open two terminals (one for server and the other for the client).
T1: gcc server.c -o server
./server
./server
Assignment
1. Implement a client-server program where the client requests the addition of two
integers, the server performs the calculation, and the result is sent back to the client.
Use command-line arguments for input, including the server's IP address during
execution.
2. Implement a TCP client-server program where the client sends a string to the server,
and the server checks whether the string is a palindrome. The server should return the
result to the client.