0% found this document useful (0 votes)
16 views7 pages

Week 12

The document contains C programs for client-server communication using both Unix domain sockets and Internet domain sockets. It includes a server program that listens for connections, receives messages from clients, and sends responses back. Additionally, it provides a client program that connects to the server, sends a message, and receives a response.

Uploaded by

dhanushkuna333
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)
16 views7 pages

Week 12

The document contains C programs for client-server communication using both Unix domain sockets and Internet domain sockets. It includes a server program that listens for connections, receives messages from clients, and sends responses back. Additionally, it provides a client program that connects to the server, sends a message, and receives a response.

Uploaded by

dhanushkuna333
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/ 7

WEEK-12

NAME:k.Dhanush ROLLNO:22R21A66F8
CLASS:CSM-C DATE:25/11/24
A. Write Client server program in C for connection oriented communication between
server and client process using unix domain sockets to perform the following.

PROGRAM :Server

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCKET_PATH "/tmp/server_socket"
#define BUFFER_SIZE 1024
int main() {
int server_fd, client_fd;
socklen_t client_len;
struct sockaddr_un server_addr, client_addr;
char buffer[BUFFER_SIZE];
int nbytes;

// Create a Unix domain socket


if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// Set the server address structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);

// Bind the socket to the server address


if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}

// Listen for connections


if (listen(server_fd, 5) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on %s\n", SOCKET_PATH);

// Accept connections
client_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (stru PROGRAM :Server ct sockaddr *)&client_addr,
&client_len)) == -1) {
perror("accept");
exit(EXIT_FAILURE);
}

// Receive data from the client


nbytes = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (nbytes == -1) {
perror("recv");
exit(EXIT_FAILURE);
}

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

// Send response to the client


strcpy(buffer, "Message received by server");
if (send(client_fd, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}
// Close the sockets
close(client_fd);
close(server_fd);

// Remove the socket file


unlink(SOCKET_PATH);

return 0;
}
OUTPUT:

PROGRAM: Client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCKET_PATH "/tmp/server_socket"


#define BUFFER_SIZE 1024

int main() {
int client_fd;
struct sockaddr_un server_addr;
char buffer[BUFFER_SIZE];
int nbytes;

// Create a Unix domain socket


if ((client_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}

// Set the server address structure


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);

// Connect to the server


if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}

// Send data to the server


strcpy(buffer, "Hello from client");
if (send(client_fd, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}

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

// Receive response from the server


nbytes = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (nbytes == -1) {
perror("recv");
exit(EXIT_FAILURE);
}

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

// Close the socket


close(client_fd);

return 0;
}
OUTPUT:

B. Write Client server program in C for connection oriented communication between


server and client process using internet domain sockets.
PROGRAM :Server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 8080


#define BUFFER_SIZE 1024

int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
int nbytes;

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

// Set the server address structure


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

// Bind the socket to the server address


if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}

// Listen for connections


if (listen(server_fd, 5) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}

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


// Accept connections
socklen_t client_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len)) == -1) {
perror("accept");
exit(EXIT_FAILURE);
}

// Receive data from the client


nbytes = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (nbytes == -1) {
perror("recv");
exit(EXIT_FAILURE);
}

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

// Send response to the client


strcpy(buffer, "Message received by server");
if (send(client_fd, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}

// Close the sockets


close(client_fd);
close(server_fd);

return 0;
}
OUTPUT:

PROGRAM :Client

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT 8080


#define BUFFER_SIZE 1024

int main() {
int client_fd;
struct sockaddr_in server_addr;
struct hostent *server;
char buffer[BUFFER_SIZE];
int nbytes;

// Create a socket
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}

// Get server IP address


if ((server = gethostbyname("localhost")) == NULL) {
perror("gethostbyname");
exit(EXIT_FAILURE);
}

// Set the server address structure


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(PORT);

// Connect to the server


if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}

// Send data to the server


strcpy(buffer, "Hello from client");
if (send(client_fd, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}

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

// Receive response from the server


nbytes = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (nbytes == -1) {
perror("recv");
exit(EXIT_FAILURE);
}

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

// Close the socket


close(client_fd);

return 0;
}
OUTPUT:

You might also like