Write C programs to implement a simple Client-Server application using Unix File Socket communication. A client will send N integers to the server, which will sort them and send them back to the client. The client will print the result.
Client Program
//This code is written by Souvik Ghosh and Debmitra Chatterjee//Client Program in C#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>intmain(){int sockfd;int len;struct sockaddr_un address;int result;int size;
sockfd =socket(AF_UNIX, SOCK_STREAM,0);
address.sun_family = AF_UNIX;strcpy(address.sun_path,"server_socket");
len =sizeof(address);
result =connect(sockfd,(struct sockaddr *)&address, len);//Connect to the serverif(result ==-1){perror("connection failed!!");exit(1);}printf("connected\n");//user inputprintf("please enter the number of integers you want to send: ");scanf("%d",&size);int numbers[size];int sortedNumbers[size];printf("please enter %d numbers: ",size);for(int i=0;i<size;i++){scanf("%d",&numbers[i]);}//read and write via sockfdwrite(sockfd,&size,sizeof(int));write(sockfd,&numbers,size*sizeof(int));printf("numbers sent\n");printf("waiting for the server...\n");read(sockfd,&sortedNumbers,size*sizeof(int));printf("numbers received: ");for(int i=0;i<size;i++){printf("%d ",sortedNumbers[i]);}printf("\n");// close the socket connectionclose(sockfd);exit(0);return0;}
Server Program
//This code is written by Souvik Ghosh and Debmitra Chatterjee//Server Program in C#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>#define maxSize 100/*predefined structure
struct sockaddr_un{
//AF_UNIX
sa_family_t sun_family;
//Pathname
char sun_path[108];
};
*///swap function for sortingvoidswap(int* xp,int* yp){int temp =*xp;*xp =*yp;*yp = temp;}// Function to perform Selection SortvoidselectionSort(int arr[],int n){int i, j, min_idx;// One by one move boundary of unsorted subarrayfor(i =0; i < n -1; i++){// Find the minimum element in unsorted array
min_idx = i;for(j = i +1; j < n; j++)if(arr[j]< arr[min_idx])
min_idx = j;// Swap the found minimum element// with the first elementswap(&arr[min_idx],&arr[i]);}}intmain(){int server_sockfd, client_sockfd;int server_len, client_len;struct sockaddr_un server_address;struct sockaddr_un client_address;//The file should be deleted by the caller when it is no longer needed. AF_UNIX sockets can be deleted with unlink().unlink("server_socket");//SOCK_STREAM/SOCK_DGRAM, AF_UNIX(Unix file system )/AF_INET(Internet domain )
server_sockfd =socket(AF_UNIX, SOCK_STREAM,0);//This socket family is used to communicate between processes on the same machine.
server_address.sun_family = AF_UNIX;//The file name referred to in addr.sun_path is created as a socket in the system filestrcpy(server_address.sun_path,"server_socket");
server_len =sizeof(server_address);// bind a name to a socket// Server ask the OS to enter information in the socket// RETURN VALUE- On success, zero is returned. On error, -1 is returnedbind(server_sockfd,(struct sockaddr *)&server_address, server_len);//listen the requests comming from the client// 5,backlog - the no of connections allowed in the queue. It can never be zerolisten(server_sockfd,5);while(1){int n;printf("server waiting.....\n");
client_len =sizeof(client_address);//connection gets established here
client_sockfd =accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);//readread(client_sockfd,&n,sizeof(int));int receivedMessage[n];read(client_sockfd,&receivedMessage,n*sizeof(int));printf("numbers received: ");for(int i=0;i<n;i++){printf("%d ",receivedMessage[i]);}printf("\n");//perform operationselectionSort(receivedMessage,n);printf("numbers sent: ");for(int i=0;i<n;i++){printf("%d ",receivedMessage[i]);}printf("\n");//writewrite(client_sockfd,&receivedMessage,n*sizeof(int));// close the socket connectionclose(client_sockfd);}return0;}
Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…