0% found this document useful (0 votes)
18 views

Assignment 6

Assignment 3 cn

Uploaded by

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

Assignment 6

Assignment 3 cn

Uploaded by

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

Assignment-6

WAP to implement TCO client server communication using socket


programming.
Server Code:-
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions (e.g., exit)
#include <string.h> // For string manipulation functions
#include <unistd.h> // For close function
#include <arpa/inet.h> // For socket-related functions and definitions

#define PORT 8080 // Port number where the server will listen for connections

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
const char *hello = "Hello from server";

// Creating a socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}

// Set options for the socket to reuse the port and address
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))) {
perror("Setsockopt failed");
exit(EXIT_FAILURE);
}

// Define the server address


address.sin_family = AF_INET; // IPv4 family
address.sin_addr.s_addr = INADDR_ANY; // Bind to any available network interface
address.sin_port = htons(PORT); // Convert port number to network byte order

// Bind the socket to the defined address and port


if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}

// Listen for incoming connections (maximum backlog of 3)


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

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

// Accept an incoming connection


if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
perror("Accept failed");
exit(EXIT_FAILURE);
}

// Read data from the client


read(new_socket, buffer, 1024);
printf("Message from client: %s\n", buffer);

// Send a response to the client


send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");

// Close the socket


close(new_socket);
close(server_fd);

return 0;
}

Client code:-
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions (e.g., exit)
#include <string.h> // For string manipulation functions
#include <unistd.h> // For close function
#include <arpa/inet.h> // For socket-related functions and definitions

#define PORT 8080 // Port number to connect to the server

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

// Creating a socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\nSocket creation error\n");
return -1;
}

// Define the server address


serv_addr.sin_family = AF_INET; // IPv4 family
serv_addr.sin_port = htons(PORT); // Convert port number to network byte order

// Convert IPv4 address from text to binary and set it to localhost


if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address or address not supported\n");
return -1;
}

// Connect to the server


if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed\n");
return -1;
}

// Send a message to the server


send(sock, hello, strlen(hello), 0);
printf("Hello message sent to server\n");

// Read the server's response


read(sock, buffer, 1024);
printf("Message from server: %s\n", buffer);

// Close the socket


close(sock);

return 0;
}

SERVER TERMINAL:-
Server is listening on port 8080
Message from client: Hello from client
Hello message sent

CLIENT TERMINAL:-
Hello message sent to server
Message from server: Hello from server

Explanation of Functions and Headers


 #include <arpa/inet.h>: Provides definitions for network-related functions (e.g., socket,
connect).
 socket(): Creates an endpoint for communication.
 bind(): Assigns a local address to the socket.
 listen(): Marks the socket as passive to accept incoming connections.
 accept(): Accepts a connection request from a client.
 connect(): Initiates a connection on a socket.
 send(): Sends data to the connected socket.
 read(): Reads data from the connected socket.

You might also like