Client-Server application using Unix File Socket communication in C

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>
int main(){
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 server
if(result == -1){
perror("connection failed!!");
exit(1);
}
printf("connected\n");
//user input
printf("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 sockfd
write(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 connection
close(sockfd);
exit(0);
return 0;
}

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 sorting
void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
// Function to perform Selection Sort
void selectionSort(int arr[], int n)
{
    int i, j, min_idx;
    // One by one move boundary of unsorted subarray
    for (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 element
        swap(&arr[min_idx], &arr[i]);
    }
}
int main(){
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 file
strcpy(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 returned
bind(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 zero
listen(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);
//read
read(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 operation
selectionSort(receivedMessage,n);
printf("numbers sent: ");
for(int i=0;i<n;i++){
printf("%d ",receivedMessage[i]);
}
printf("\n");
//write
write(client_sockfd,&receivedMessage,n*sizeof(int));
// close the socket connection
close(client_sockfd);
}
return 0;
}

Output

output of the unix file socket communication

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

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…

Leave a Reply

Your email address will not be published. Required fields are marked *