forked from lichao2014/name_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
executable file
·48 lines (37 loc) · 961 Bytes
/
client.c
File metadata and controls
executable file
·48 lines (37 loc) · 961 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "comm.h"
#include "net.h"
const char* localipaddr = "0.0.0.0";
int
main(int argc, char *argv[]) {
if (argc < 4) {
printf("invalid args");
return -1;
}
int c = create_tcp_socket(localipaddr, atoi(argv[2]), 0);
if (c < 0) {
return -1;
}
struct sockaddr_in peer = make_address4(argv[1], atoi(argv[3]));
int ret = connect(c, (struct sockaddr *)&peer, sizeof peer);
if (ret < 0) {
perror("connect");
close(c);
return -1;
}
char buf[1024];
struct name_msg_t *msg = (struct name_msg_t *)buf;
char line[1024];
while (fgets(line, 1024, stdin)) {
if (2 != sscanf(line, "%d %s", &msg->id, msg->data)) {
continue;
}
buf[1023] = 0;
msg->len = strlen(msg->data);
send(c, buf, sizeof(int) * 2 + msg->len, 0);
}
close(c);
return 0;
}