/
kfilemond.cpp
45 lines (37 loc) · 1.15 KB
/
kfilemond.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
#include <unistd.h>
#include <sys/socket.h>
#include <QtCore/QByteArray>
#include <QtCore/QDebug>
#include <sys/un.h>
#include <errno.h>
#include "config.h"
int main(int argc, char **argv) {
int s = socket(AF_UNIX, SOCK_STREAM, 0);
sockaddr_un local;
local.sun_family = AF_UNIX;
char *sockPath = get_socket_path();
strcpy(local.sun_path, sockPath);
free(sockPath);
unlink(local.sun_path);
socklen_t len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (sockaddr *)&local, len) == -1) {
printf("Unable to bind! (%s)\n", strerror(errno));
exit(errno);
}
if (listen(s, 5) == -1){ // backlog of five connections possible
printf("Unable to listen! (%s)\n", strerror(errno));
exit(errno);
}
char buf[1024];
QByteArray data;
while (true) {
sockaddr_un client;
len = sizeof(sockaddr_un);
int clientSocket = accept(s, (sockaddr*)&client, &len);
data.clear();
while (len = recv(clientSocket, buf, sizeof(buf), 0))
data.append(buf, len);
qDebug() << "Got data:" << data.split(0);
close(clientSocket);
}
}