-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
45 lines (39 loc) · 901 Bytes
/
client.c
File metadata and controls
45 lines (39 loc) · 901 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
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static int set_nonblocking(int fd)
{
int flag = fcntl(fd, F_GETFL);
return fcntl(fd, F_SETFL, flag|O_NONBLOCK);
}
int main(int argc, const char *argv[])
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("socket");
return -1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(8888);
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
perror("connect");
close(sock);
return -1;
}
char *str = "hello world";
ssize_t nret = write(sock, str, strlen(str)+1);
if (nret < 0)
{
perror("write");
}
close(sock);
return 0;
}