unix_soket_server:
/************************************************************/
/* This is a stream socket server sample program for UNIX */
/* domain sockets. This program listens for a connection */
/* from a client program, accepts it, reads data from the */
/* client, then sends data back to connected UNIX socket. */
/* https://www.ibm.com/support/knowledgecenter/SSB23S_1.1.0.15/gtpc1/unixsock.html */
/************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCK_PATH "tpf_unix_sock.server"
#define DATA "Hello from server"
int main(void){
int server_sock, client_sock, rc;
int bytes_rec = 0;
socklen_t len;
/** Unix sockets */
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
int backlog = 10;
// Fill the serversocket and client sockadd with 0s
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
// Fill buffer with 0
memset(buf, 0, 256);
/**************************************/
/* Create a UNIX domain stream socket */
/**************************************/
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1){
printf("SOCKET ERROR: %d\n", errno);
exit(1);
}
/***************************************/
/* Set up the UNIX sockaddr structure */
/* by using AF_UNIX for the family and */
/* giving it a filepath to bind to. */
/* */
/* Unlink the file so the bind will */
/* succeed, then bind to that file. */
/***************************************/
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
len = sizeof(server_sockaddr);
printf("%s\n",server_sockaddr.sun_path);
unlink(SOCK_PATH);
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
if (rc == -1){
printf("BIND ERROR: %d\n", errno);
close(server_sock);
exit(1);
}
/*********************************/
/* Listen for any client sockets */
/*********************************/
rc = listen(server_sock, backlog);
if (rc == -1){
printf("LISTEN ERROR: %d\n", errno);
close(server_sock);
exit(1);
}
printf("socket listening...\n");
/*********************************/
/* Accept an incoming connection */
/*********************************/
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
if (client_sock == -1){
printf("ACCEPT ERROR: %d\n", errno);
close(server_sock);
close(client_sock);
exit(1);
}
/****************************************/
/* Get the name of the connected socket */
/****************************************/
len = sizeof(client_sockaddr);
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
printf("peerno %d\n",rc);
if (rc == -1){
perror("getpeername");
printf("GETPEERNAME ERROR: %d\n", errno);
close(server_sock);
close(client_sock);
exit(1);
}
else {
printf("Client socket filepath: %s\n", client_sockaddr.sun_path);
}
/************************************/
/* Read and print the data */
/* incoming on the connected socket */
/************************************/
printf("waiting to read...\n");
bytes_rec = recv(client_sock, buf, sizeof(buf), 0);
if (bytes_rec == -1){
printf("RECV ERROR: %d\n", errno);
close(server_sock);
close(client_sock);
exit(1);
}
else {
printf("DATA RECEIVED = %s\n", buf);
}
/******************************************/
/* Send data back to the connected socket */
/******************************************/
memset(buf, 0, 256);
strcpy(buf, DATA);
printf("Sending data...\n");
rc = send(client_sock, buf, strlen(buf), 0);
if (rc == -1) {
printf("SEND ERROR: %d", errno);
close(server_sock);
close(client_sock);
exit(1);
}
else {
printf("Data sent!\n");
}
/******************************/
/* Close the sockets and exit */
/******************************/
close(server_sock);
close(client_sock);
return 0;
}
unix_soket_client:
/************************************************************/
/* This is a stream socket client sample program for UNIX */
/* domain sockets. This program creates a socket, connects */
/* to a server, sends data, then receives and prints a */
/* message from the server. */
/************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SERVER_PATH "tpf_unix_sock.server"
#define CLIENT_PATH "tpf_unix_sock.client"
#define DATA "Hello from client"
void sendMessage(int client_sock,char *buf,int buflen,int flag);
int main(void){
int client_sock, rc;
socklen_t len;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
/**************************************/
/* Create a UNIX domain stream socket */
/**************************************/
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (client_sock == -1) {
printf("SOCKET ERROR = %d\n", errno);
exit(1);
}
/***************************************/
/* Set up the UNIX sockaddr structure */
/* by using AF_UNIX for the family and */
/* giving it a filepath to bind to. */
/* */
/* Unlink the file so the bind will */
/* succeed, then bind to that file. */
/***************************************/
client_sockaddr.sun_family = AF_UNIX;
strcpy(client_sockaddr.sun_path, CLIENT_PATH);
len = sizeof(client_sockaddr);
unlink(CLIENT_PATH);
rc = bind(client_sock, (struct sockaddr *) &client_sockaddr, len);
if (rc == -1){
printf("BIND ERROR: %d\n", errno);
close(client_sock);
exit(1);
}
/***************************************/
/* Set up the UNIX sockaddr structure */
/* for the server socket and connect */
/* to it. */
/***************************************/
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SERVER_PATH);
printf("server is :%s\n",server_sockaddr.sun_path);
rc = connect(client_sock, (struct sockaddr *) &server_sockaddr, len);
if(rc == -1){
printf("CONNECT ERROR = %d\n", errno);
close(client_sock);
exit(1);
}
/************************************/
/* Copy the data to the buffer and */
/* send it to the server socket. */
/************************************/
strcpy(buf, DATA);
printf("Sending data...\n");
sendMessage(client_sock, buf, strlen(buf), 0);
sendMessage(client_sock, buf, strlen(buf), 0);
sendMessage(client_sock, buf, strlen(buf), 0);
/**************************************/
/* Read the data sent from the server */
/* and print it. */
/**************************************/
printf("Waiting to recieve data...\n");
memset(buf, 0, sizeof(buf));
rc = recv(client_sock, buf, sizeof(buf),0);
if (rc == -1) {
printf("RECV ERROR = %d\n", errno);
close(client_sock);
exit(1);
}
else {
printf("DATA RECEIVED = %s\n", buf);
}
/******************************/
/* Close the socket and exit. */
/******************************/
close(client_sock);
return 0;
}
void sendMessage(int client_sock,char *buf,int buflen,int flag){
int rc;
rc = send(client_sock, buf, strlen(buf), 0);
if (rc == -1) {
printf("SEND ERROR = %d\n", errno);
close(client_sock);
exit(1);
}
else {
printf("Data sent!\n");
}
}