Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

TCP Client/Server Communication Aim: Procedure: Server (Server.c)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Ex. No.

: 02
Date: 10.08.2024
TCP Client/Server Communication

Aim:
To implement TCP Client/Server communication using C.
Procedure:
Server (server.c):
1. Initialize: Define IP (`127.0.0.1`), port (`5566`), and create a TCP socket.
2. Bind: Bind the socket to IP and port, then listen for incoming connections.
3. Accept & Communicate: In a loop, accept client connections, receive a message,
respond with "HI, THIS IS SERVER. HAVE A NICE DAY!!!", and close the client
socket.
4. Repeat: Continue accepting new clients.
Client (client.c):
1. Initialize: Define server IP, port, and create a TCP socket.
2. Connect: Connect to the server at the specified IP and port.
3. Send & Receive: Send "HELLO, THIS IS CLIENT." to the server, receive the server’s
response, and print both messages.
4. Close: Close the connection to the server.
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(){
char *ip = "127.0.0.1";
int port = 5566;
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
int n;
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0){
perror("[-]Socket error");

1
2022503524
exit(1);
}
printf("[+]TCP server socket created.\n");
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (n < 0){
perror("[-]Bind error");
exit(1);
}
printf("[+]Bind to the port number: %d\n", port);
listen(server_sock, 5);
printf("Listening...\n");
while(1){
addr_size = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);
printf("[+]Client connected.\n");
bzero(buffer, 1024);
recv(client_sock, buffer, sizeof(buffer), 0);
printf("Client: %s\n", buffer);
bzero(buffer, 1024);
strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");
printf("Server: %s\n", buffer);
send(client_sock, buffer, strlen(buffer), 0);
close(client_sock);
printf("[+]Client disconnected.\n\n");
}
return 0;
}
Output:

2
2022503524
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(){
char *ip = "127.0.0.1";
int port = 5566;
int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("[-]Socket error");
exit(1);
}
printf("[+]TCP server socket created.\n");
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock, (struct sockaddr*)&addr, sizeof(addr));
printf("Connected to the server.\n");
bzero(buffer, 1024);
strcpy(buffer, "HELLO, THIS IS CLIENT.");
printf("Client: %s\n", buffer);
send(sock, buffer, strlen(buffer), 0);

3
2022503524
bzero(buffer, 1024);
recv(sock, buffer, sizeof(buffer), 0);
printf("Server: %s\n", buffer);
close(sock);
printf("Disconnected from the server.\n");
return 0;
}
Output:

Result:
The TCP Server/Client communication has been implemented successfully and the
message has been sent successfully.

4
2022503524

You might also like