blob: dafd1e83455115930e7138b80a65cc699eff8fd1 (
plain)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "types.h"
void handle_client(int fd)
{
char buf[4096] = {0};
int *data;
proto_hdr_t *hdr = (proto_hdr_t*)buf;
read(fd, buf, sizeof(proto_hdr_t) + sizeof(int));
hdr->type = ntohl(hdr->type);
hdr->len = ntohs(hdr->len);
data = (int *)&hdr[1];
*data = ntohl(*data);
if (hdr->type != PROTO_HELLO)
printf("Protocol mismatch\n");
if (*data != 1)
printf("Protocol version mismatch\n");
printf("Client connected - proto: %d ver: %d payload: %d\n",
hdr->type, hdr->len, *data);
}
int main(int argc, char *argv[])
{
int err;
struct sockaddr_in serverInfo = {0};
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (argc != 2) {
printf("Usage: ./client <ip>\n");
exit(1);
}
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = inet_addr(argv[1]);;
serverInfo.sin_port = htons(SERVER_PORT);
if (fd < 0) {
perror("socket");
exit(1);
}
err = connect(fd, (struct sockaddr*)&serverInfo, sizeof(serverInfo));
if (err < 0) {
perror("connect");
close(fd);
exit(1);
}
handle_client(fd);
close(fd);
return 0;
}
|