Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views13 pages

Assignment 7

Uploaded by

tanaymitra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Assignment 7

Uploaded by

tanaymitra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

​ ​ ​ ASSIGNMENT -7

24BCE1287

24BCE1287 TANAY MITRA

24BCE1799 ARNAV THAPLIYAL

Assignment: TCP Client-Server Communication in C

1. Aim
To implement a TCP client-server communication system in C where the client sends messages
to the server and receives responses.

2. Objectives
●​ Create a TCP socket for both client and server.​

●​ Establish a connection between client and server.​

●​ Enable message exchange.​

●​ Close the connection gracefully.​

3. Algorithm
Server Side Algorithm

1.​ Start the server program.​


2.​ Create a socket using socket().​

3.​ Bind the socket to an IP address and port using bind().​

4.​ Put the server in listening mode using listen().​

5.​ Accept a client connection using accept().​

6.​ Receive messages from the client using recv().​

7.​ Send responses back to the client using send().​

8.​ Repeat until termination condition.​

9.​ Close the socket using close().​

Client Side Algorithm

1.​ Start the client program.​

2.​ Create a socket using socket().​

3.​ Connect to the server using connect().​

4.​ Take input from the user.​

5.​ Send the message to the server using send().​

6.​ Receive a response from the server using recv().​

7.​ Repeat until the user types exit.​

8.​ Close the socket using close().​

Client side (Client.c)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {


int socket_desc;
struct sockaddr_in server;
char *server_ip;
int port;
char message[BUFFER_SIZE];
char response[BUFFER_SIZE];

// Check for correct usage


if (argc != 3) {
fprintf(stderr, "Usage: %s <server-ip> <port>\n", argv[0]);
exit(1);
}

server_ip = argv[1];
port = atoi(argv[2]);

// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
perror("Socket creation failed");
return 1;
}

// Set up server address structure


server.sin_family = AF_INET;
server.sin_port = htons(port);

// Convert IP address from text to binary


if (inet_pton(AF_INET, server_ip, &server.sin_addr) <= 0) {
fprintf(stderr, "Invalid address: %s\n", server_ip);
close(socket_desc);
return 1;
}

// Connect to the server


if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Connect failed");
close(socket_desc);
return 1;
}

printf(" ✅ Connected to server %s on port %d.\n", server_ip, port);


printf(" 💬 Type messages to send (or 'exit' to quit):\n\n");
while (1) {
// Send message
printf("You: ");
fflush(stdout);
fgets(message, sizeof(message), stdin);

// Remove newline from input


message[strcspn(message, "\n")] = 0;

// Check if user wants to exit


if (strcmp(message, "exit") == 0 || strcmp(message, "quit") == 0) {
printf(" 👋 Disconnecting...\n");
break;
}

// Send message to server


if (send(socket_desc, message, strlen(message), 0) < 0) {
perror("Send failed");
break;
}

// Receive response from server


int bytes_received = recv(socket_desc, response, BUFFER_SIZE - 1, 0);
if (bytes_received > 0) {
response[bytes_received] = '\0';
printf("Server: %s\n", response);
} else if (bytes_received == 0) {
printf(" 🔌 Server disconnected.\n");
break;
} else {
perror("Receive failed");
break;
}
}

close(socket_desc);
printf("Connection closed.\n");
return 0;
}

Server Code(Server.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {


int server_fd, client_fd;
struct sockaddr_in server, client;
socklen_t client_len = sizeof(client);
char buffer[BUFFER_SIZE];
int port;

if (argc != 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);
}

port = atoi(argv[1]);

// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(1);
}

// Prepare server address


server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);

// Bind socket
if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Bind failed");
close(server_fd);
exit(1);
}

// Listen for incoming connections


if (listen(server_fd, 3) < 0) {
perror("Listen failed");
close(server_fd);
exit(1);
}

printf(" 🚀 Server started on port %d. Waiting for connection...\n", port);


// Accept a client
if ((client_fd = accept(server_fd, (struct sockaddr *)&client, &client_len)) < 0) {
perror("Accept failed");
close(server_fd);
exit(1);
}

printf(" ✅ Client connected: %s:%d\n",


inet_ntoa(client.sin_addr), ntohs(client.sin_port));
while (1) {
int bytes_received = recv(client_fd, buffer, BUFFER_SIZE - 1, 0);

if (bytes_received <= 0) {
printf(" 🔌 Client disconnected.\n");
break;
}

buffer[bytes_received] = '\0';
printf("Client: %s\n", buffer);

// Define server reply


char reply[BUFFER_SIZE];
if (strcmp(buffer, "hiiiii") == 0) {
strcpy(reply, "hellloooo");
} else if (strcmp(buffer, "greeaatt") == 0) {
strcpy(reply, "exit");
} else {
snprintf(reply, sizeof(reply), "Echo: %s", buffer);
}

// Send reply back


if (send(client_fd, reply, strlen(reply), 0) < 0) {
perror("Send failed");
break;
}

if (strcmp(reply, "exit") == 0) {
printf(" 👋 Server requested disconnect.\n");
break;
}
}

close(client_fd);
close(server_fd);
printf("Server shut down.\n");
return 0;
}
5. Sample Output
Server Terminal
$ gcc server.c -o server

🚀
$ ./server 4268


Server started on port 4268. Waiting for connection...
Client connected: 172.16.12.68:53022
Client: hiiiii

👋
Client: greeaatt

🔌
Server requested disconnect.
Client disconnected.
Server shut down.

Client Terminal
$ gcc client.c -o client


$ ./client 172.16.12.68 4268

💬
Connected to server 172.16.12.68 on port 4268.
Type messages to send (or 'exit' to quit):

You: hiiiii
Server: hellloooo
You: greeaatt
Server: exit

🔌
You: jiiii
Server disconnected.
Connection closed.

6. Result
Thus, the TCP client-server program was successfully implemented in C. The client can send
messages, and the server responds back accordingly, with proper connection establishment and
termination.

You might also like