-
Notifications
You must be signed in to change notification settings - Fork 8
/
tcp.cpp
60 lines (50 loc) · 1.24 KB
/
tcp.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
58
59
60
#include "hiveodbc.h"
/* サーバ接続 */
int tcp_connect(char *server_name,short server_port){
int ret;
int flags;
struct sockaddr_in client_addr;
WSADATA data;
WSAStartup(MAKEWORD(2,0), &data);
/*client_addr構造体に、接続するサーバのアドレス・ポート番号を設定 */
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr(server_name);
client_addr.sin_port = htons(server_port);
/* ソケットを生成 */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
tcp_disconnect();
sockfd=(-1);
return -1;
}
/* サーバに接続 */
ret=connect(sockfd, (struct sockaddr *)&client_addr, sizeof(client_addr));
if ( ret < 0 ){
tcp_disconnect();
sockfd=(-1);
return -2;
}
return 0;
}
/* 送信 */
int tcp_write(char *buf, int buf_len){
int ret;
if ( sockfd == (-1) ){ return -1; }
ret=send(sockfd, buf, buf_len, 0);
return ret;
}
/* 受信 */
int tcp_read(char *buf, int buf_len){
int ret;
if ( sockfd == (-1) ){ return -1; }
ret=recv(sockfd, buf, buf_len, 0);
return ret;
}
/* ソケットをクローズ */
int tcp_disconnect(){
if ( sockfd == (-1) ){ return -1; }
closesocket(sockfd);
WSACleanup();
sockfd=(-1);
return 0;
}