-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_client.cpp
57 lines (43 loc) · 966 Bytes
/
socket_client.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
int main()
{
struct sockaddr_in server;
int sock;
char buf[32];
int n;
/* ソケットの作成 */
printf("socket \n");
sock = socket(AF_INET, SOCK_STREAM, 0);
if( sock<0 ){
perror("socket");
return 0;
}
/* 接続先指定用構造体の準備 */
server.sin_family = AF_INET;
server.sin_port = htons(12345);
server.sin_addr.s_addr = inet_addr("192.168.1.70"); // IPアドレスを設定
/* サーバに接続 */
printf("connect \n");
if( connect(sock, (struct sockaddr *)&server, sizeof(server))<0 ){
perror("connect");
return 0;
}
memset(buf, 0, sizeof(buf));
/* サーバからデータを受信 */
printf("read \n");
n = read(sock, buf, sizeof(buf));
if( n <0 ){
perror("read");
return 0;
}
printf("%d, %s\n", n, buf);
/* socketの終了 */
close(sock);
return 0;
}