0% found this document useful (0 votes)
3 views3 pages

Socket Programming Experiment

The document outlines an experiment to establish communication between a client and a server using TCP socket programming in C. It includes steps for creating server and client files, compiling them with GCC, and executing them to demonstrate successful communication. The result confirms that the client and server successfully exchanged messages.
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)
3 views3 pages

Socket Programming Experiment

The document outlines an experiment to establish communication between a client and a server using TCP socket programming in C. It includes steps for creating server and client files, compiling them with GCC, and executing them to demonstrate successful communication. The result confirms that the client and server successfully exchanged messages.
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

Experiment: 9 Client–Server Model

using Socket Programming in C


Aim
To establish communication between a client and a server using TCP socket programming
in C.

Requirements
- System with Linux / macOS / Windows (MinGW/WSL)
- GCC compiler installed
- Terminal / Command Prompt

Procedure
Step 1: Create Working Folder
Create a folder named "socket_lab" and open terminal inside it.

Step 2: Create Server File


Create file "server.c" and paste the server code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *message = "Hello from Server";

server_fd = socket(AF_INET, SOCK_STREAM, 0);

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);

bind(server_fd, (struct sockaddr *)&address, sizeof(address));


listen(server_fd, 3);

printf("Server is waiting for connection...\n");

new_socket = accept(server_fd, NULL, NULL);

read(new_socket, buffer, 1024);


printf("Message from Client: %s\n", buffer);

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

close(new_socket);
close(server_fd);

return 0;
}

Step 3: Create Client File


Create file "client.c" and paste the client code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int sock;
struct sockaddr_in serv_addr;
char *message = "Hello from Client";
char buffer[1024] = {0};

sock = socket(AF_INET, SOCK_STREAM, 0);

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);

inet_pton(AF_INET, "[Link]", &serv_addr.sin_addr);

connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

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

read(sock, buffer, 1024);


printf("Message from Server: %s\n", buffer);

close(sock);

return 0;
}

Compilation
gcc server.c -o server
gcc client.c -o client

Execution
Run server first: ./server
Run client in another terminal: ./client

Output
Server: Message from Client: Hello from Client
Client: Message from Server: Hello from Server

Result
Client and server successfully communicated using TCP socket programming.

You might also like