-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
50 lines (37 loc) · 1.04 KB
/
client.c
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int
main(void)
{
struct sockaddr_in server;
int sock;
char buf[32];
int n;
/* ソケットの作成 */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* 接続先指定用構造体の準備 */
server.sin_family = AF_INET;
server.sin_port = htons(11111);
// server.sin_len = sizeof(server); /* Linuxでは不要 */
/* 127.0.0.1はlocalhost */
inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
/* sockaddr_in 構造体の長さを指定(Linuxでは不要) */
// server.size_len = sizeof(server);
/* サーバに接続 */
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) != 0){
perror("connect");
return 1;
}
/* サーバからデータを受信 */
memset(buf, 0, sizeof(buf));
n = read(sock, buf, sizeof(buf));
printf("%d, %s\n", n, buf);
/* socketの終了 */
close(sock);
return 0;
}