Computer Networks Lab 3
Index
- Main note
- Objective
- Materials and Equipment
- Procedure
- Conclusion
- References
Main note
Lab Experiment: Implementing TCP Server and Client in C
Objective
The objective of this lab experiment is to create a TCP server and a TCP client using C language. The client will send a message to the server, which will receive, display, and send the message back to the client.
Materials and Equipment
- A computer with a C compiler installed (e.g., GCC).
- Basic knowledge of C programming and networking concepts.
Procedure
1. Setting up the TCP Server
Code: TCPServer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
}
read(new_socket, buffer, BUFFER_SIZE);
printf("Message received: %s\n", buffer);
send(new_socket, buffer, strlen(buffer), 0);
printf("Message sent: %s\n", buffer);
close(new_socket);
close(server_fd);
return 0;
}
2. Setting up the TCP Client
Code: TCPClient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[BUFFER_SIZE] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
send(sock, hello, strlen(hello), 0);
printf("Message sent: %s\n", hello);
read(sock, buffer, BUFFER_SIZE);
printf("Message received: %s\n", buffer);
close(sock);
return 0;
}
Steps
-
Compile the Server and Client Programs
gcc TCPServer.c -o TCPServer gcc TCPClient.c -o TCPClient
-
Run the Server
./TCPServer
-
Run the Client
./TCPClient
Expected Output
- When the client sends a message, the server will receive and display the message.
- The server will then send the same message back to the client.
- The client will display the received message.
Example Output:
Server Side:
Message received: Hello from client
Message sent: Hello from client
Client Side:
Message sent: Hello from client
Message received: Hello from client
Conclusion
In this lab experiment, we successfully implemented a TCP server and client in C. The client sends a message to the server, which then displays and sends the message back to the client. This experiment demonstrates the basics of socket programming and inter-process communication over a network.
References
References
Information
- date: 2024.07.29
- time: 10:23](<## Lab Experiment: Implementing TCP Server and Client in C