-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_socket.cpp
137 lines (124 loc) · 4.17 KB
/
server_socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/************************************************************/
/* 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. */
/************************************************************/
#include <unistd.h>
#include <stdio.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, len, rc;
int bytes_rec = 0;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
int backlog = 10;
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
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:\n");
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);
unlink(SOCK_PATH);
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
if (rc == -1){
printf("BIND ERROR:\n");
close(server_sock);
exit(1);
}
/*********************************/
/* Listen for any client sockets */
/*********************************/
rc = listen(server_sock, backlog);
if (rc == -1){
printf("LISTEN ERROR: \n");
close(server_sock);
exit(1);
}
printf("socket listening...\n");
/*********************************/
/* Accept an incoming connection */
/*********************************/
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr,(socklen_t *) &len);
if (client_sock == -1){
printf("ACCEPT ERROR:\n");
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, (socklen_t *)&len);
if (rc == -1){
printf("GETPEERNAME ERROR:\n");
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:\n");
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:");
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;
}