-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoobserver.c
More file actions
68 lines (59 loc) · 1.34 KB
/
oobserver.c
File metadata and controls
68 lines (59 loc) · 1.34 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <fcntl.h>
int fd, cfd;
void
handle (int s)
{
char data[100];
if (s == SIGURG)
{
int r = recv (cfd, data, sizeof (data), MSG_OOB);
data[r] = 0;
printf ("接收%d字节的带外数据(oob data): %s\n", r, data);
}
}
int
main (int argc, char **argv)
{
fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1)
printf ("1:%m\n"), exit (-1);
struct sockaddr_in dr;
dr.sin_family = AF_INET;
dr.sin_port = htons (1200);
dr.sin_addr.s_addr = INADDR_ANY;
int result = bind (fd, (struct sockaddr *) &dr, sizeof (dr));
if (result == -1)
printf ("2:%m\n"), exit (-1);
result = listen (fd, 10);
if (result == -1)
printf ("3:%m\n"), exit (-1);
signal (SIGURG, handle); //添加SIGURG信号
cfd = accept (fd, 0, 0); //建立客户端连接
if (cfd == -1)
printf ("4:%m\n"), exit (-1);
fcntl (cfd, F_SETOWN, getpid ()); //SIGURG的前提条件是进程必须持有文件描述符cfd
char buf[100];
while (1)
{
int r = recv (cfd, buf, sizeof (buf), 0);
if (r > 0)
{
buf[r] = 0;
printf ("接收%d字节的正常数据(normal data):%s\n", r, buf);
sleep (1);
}
else
{
break;
}
}
close (cfd);
close (fd);
}