-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathunixsrv.cpp
82 lines (69 loc) · 1.67 KB
/
unixsrv.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
//
// Created by jxq on 19-8-10.
//
// socket编程 16
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
void echosrv(int clientfd)
{
char recvbuf[1024];
while (1) {
memset(recvbuf, 0, sizeof recvbuf);
int ret;
if ((ret = read(clientfd, recvbuf, sizeof(recvbuf))) < 0) {
ERR_EXIT("read");
}
if (ret == 0) {
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
write(clientfd, &recvbuf, strlen(recvbuf));
}
close(clientfd);
}
int main(int argc, char** argv)
{
int listenfd;
unlink("/home/jxq/CLionProjects/NetworkProgramming/text_unix"); // 解除原有text_unix对象链接
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
ERR_EXIT("socket");
}
struct sockaddr_un servaddr;
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, "/home/jxq/CLionProjects/NetworkProgramming/text_unix");
if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof servaddr) < 0)
{
ERR_EXIT("bind");
}
if (listen(listenfd, SOMAXCONN) < 0)
{
ERR_EXIT("listen");
}
struct sockaddr_un cliaddr;
socklen_t clilen;
int clientfd;
while (1)
{
if ((clientfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen)) < 0)
{
ERR_EXIT("accept");
}
echosrv(clientfd);
}
close(listenfd);
return 0;
}