Unix File Socket Lab Manual
Unix File Socket Lab Manual
:2
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);
exit(0);
}
//Server
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
*/
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_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);
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 ?