blob: a664221d50eb350214c39d0a74cda0b9af9d8477 (
plain) (
blame)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static inline void die(const char* msg)
{
perror(msg);
exit(-1);
}
int main()
{
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(8000),
.sin_addr.s_addr = inet_addr("127.0.0.1")
};
char buf[256];
int cfd;
if ((cfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("Couldn't open the socket");
if (connect(cfd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
die("Couldn't connect to socket");
for (int i = 0; i < 5; ++i)
{
if (write(cfd, "Ping", 4) < 0)
die("Couldn't send message");
printf("[send] Ping\n");
if (read(cfd, buf, sizeof(buf)) < 0)
die("Couldn't receive message");
printf("[recv] %s\n", buf);
}
close(cfd);
return 0;
}
|