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

Unix File Socket Lab Manual

The document describes an experiment to implement a simple client-server application using Unix file sockets for communication, where a client will send integers to a server, the server will sort the integers and send the result back to the client, and the client will print the output. Unix file sockets provide fast inter-process communication without network overhead by using files on the same host operating system. Code snippets are provided for both the client and server using Unix file sockets to read and write data between the processes.

Uploaded by

kumaritishu45
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)
14 views

Unix File Socket Lab Manual

The document describes an experiment to implement a simple client-server application using Unix file sockets for communication, where a client will send integers to a server, the server will sort the integers and send the result back to the client, and the client will print the output. Unix file sockets provide fast inter-process communication without network overhead by using files on the same host operating system. Code snippets are provided for both the client and server using Unix file sockets to read and write data between the processes.

Uploaded by

kumaritishu45
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/ 5

Exp No.

:2

Exp Name : Unix File socket

Problem Statement : Write C Programs to implement a simple client-server application. A client


will send N integers to the server, which will sort the integers and send back to client. The client will
print the result. Use Unix File socket for communication.

Theory:

A Unix domain socket or IPC socket (inter-process communication socket) is a data communications
endpoint for exchanging data between processes executing on the same host operating system.
Unix sockets are a form of communication between two processes that appears as a file on disk.
This file can be used by other programs to establish very fast connections between two or more
processes without any network overhead.

A Unix Socket is used in a client-server application framework. A server is a process that performs
some functions on request from a client.

Client Process

This is the process, which typically makes a request for information. After getting the response, this
process may terminate or may do some other processing.

Server Process

This is the process which takes a request from the clients. After getting a request from the client,
this process will perform the required processing, gather the requested information, and send it to
the requestor client. Once done, it becomes ready to serve another client. Server processes are
always alert and ready to serve incoming requests.

Coding :

//Client

#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; char ch = 'A'; 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("oops:


client1");
exit(1);
}

write(sockfd, &ch, 1);


read(sockfd, &ch, 1);
//read and write via sockfd

printf("char from server = %c\n", ch);


close(sockfd);
// close the socket connection

exit(0);
}

//Server

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

/* predefined structure struct sockaddr_un {


sa_family_t sun_family; //AF_UNIX
char sun_path[108]; //Pathname
};

*/
int main()
{ int server_sockfd, client_sockfd; int
server_len, client_len; struct
sockaddr_un server_address; struct
sockaddr_un client_address;

unlink("server_socket");

server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

server_address.sun_family = AF_UNIX;

strcpy(server_address.sun_path, "server_socket");

server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

listen(server_sockfd, 5);

while(1) { char ch;


printf("server waiting\n");

client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,
&client_len);
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
//read and write to client on client_sockfd

close(client_sockfd); // close
the socket connection
}
}
Conclusion:
1. What is connection oriented and connection less communication ?

You might also like