-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient1.cc
61 lines (45 loc) · 1.3 KB
/
client1.cc
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
#include <mdnspub.h>
#include <mdnssub.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CLIENT1_PORT 5059
#define BACKLOG 100
void StartMDNS(mdns::MDnsPub &pub) {
pub.AddRecordValue("port", "5059");
pub.Register();
}
int main() {
int client1_fd;
struct sockaddr_in client1_addr;
mdns::MDnsPub pub("client1", "_http._tcp");
StartMDNS(pub);
client1_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client1_fd == -1) {
printf("create socket error\n");
exit(0);
}
memset(&client1_addr, 0, sizeof(client1_addr));
client1_addr.sin_family = AF_INET;
client1_addr.sin_port = htons(CLIENT1_PORT);
client1_addr.sin_addr.s_addr = INADDR_ANY;
int opt = 1;
setsockopt(client1_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(client1_fd, (struct sockaddr *) &client1_addr, sizeof(client1_addr)) == -1) {
printf("binding error\n");
exit(0);
}
if (listen(client1_fd, BACKLOG) == -1) {
printf("listen error\n");
exit(0);
}
int new_fd = accept(client1_fd, NULL, NULL);
uint8_t byte_num;
read(new_fd, &byte_num, sizeof(byte_num));
printf("bytenum: %d\n", byte_num);
while (1) ;
return 0;
}