Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Assignment Lab 5

The document provides C code for a TCP client-server application, detailing the implementation of both server and client functionalities. The server listens for connections, receives messages from the client, and sends a response, while the client connects to the server, sends a message, and receives a response. Additionally, it includes assignment tasks for implementing further client-server interactions involving integer addition and palindrome checking.

Uploaded by

Anmol
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment Lab 5

The document provides C code for a TCP client-server application, detailing the implementation of both server and client functionalities. The server listens for connections, receives messages from the client, and sends a response, while the client connects to the server, sends a message, and receives a response. Additionally, it includes assignment tasks for implementing further client-server interactions involving integer addition and palindrome checking.

Uploaded by

Anmol
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

National Institute of Technology, Rourkela

DCCN Lab (CS3072)

6th Semester – 2025 Spring Sem

Client-Server message sending

TCP Server in C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define PORT 8080

#define BUF_SIZE 1024

int main() {

int server_fd, client_fd;

struct sockaddr_in server_addr, client_addr;

socklen_t addr_len = sizeof(client_addr);

char buffer[BUF_SIZE];

// Create socket

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("Socket creation failed");

exit(EXIT_FAILURE);
}

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = INADDR_ANY;

server_addr.sin_port = htons(PORT);

// Bind the socket to the IP address and port

if (bind(server_fd, (struct sockaddr*)&server_addr,


sizeof(server_addr)) == -1) {

perror("Bind failed");

close(server_fd);

exit(EXIT_FAILURE);

// Listen for incoming connections

if (listen(server_fd, 5) == -1) {

perror("Listen failed");

close(server_fd);

exit(EXIT_FAILURE);

printf("Server is listening on port %d...\n", PORT);

// Accept incoming connections

if ((client_fd = accept(server_fd, (struct sockaddr*)&client_addr,


&addr_len)) == -1) {

perror("Accept failed");
close(server_fd);

exit(EXIT_FAILURE);

printf("Connection established with client\n");

// Receive message from client

int bytes_received = recv(client_fd, buffer, BUF_SIZE, 0);

if (bytes_received == -1) {

perror("Receive failed");

close(client_fd);

close(server_fd);

exit(EXIT_FAILURE);

buffer[bytes_received] = '\0'; // Null-terminate the string

printf("Received from client: %s\n", buffer);

// Send a response to the client

const char* response = "Message received";

send(client_fd, response, strlen(response), 0);

// Close the connection

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>

#define PORT 8080

#define BUF_SIZE 1024

int main() {

int sock;

struct sockaddr_in server_addr;

char buffer[BUF_SIZE];

// Create socket

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("Socket creation failed");

exit(EXIT_FAILURE);

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(PORT);
// Convert IP address to binary format

if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) {

perror("Invalid address");

close(sock);

exit(EXIT_FAILURE);

// Connect to the server

if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr))


== -1) {

perror("Connection failed");

close(sock);

exit(EXIT_FAILURE);

printf("Connected to the server\n");

// Send message to the server

const char* message = "Hello from client!";

send(sock, message, strlen(message), 0);

// Receive server response

int bytes_received = recv(sock, buffer, BUF_SIZE, 0);

if (bytes_received == -1) {

perror("Receive failed");

close(sock);

exit(EXIT_FAILURE);
}

buffer[bytes_received] = '\0'; // Null-terminate the string

printf("Received from server: %s\n", buffer);

// Close the socket

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

T2: gcc client.c -o client

./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.

You might also like