-
Notifications
You must be signed in to change notification settings - Fork 0
/
cTcpSock.cpp
executable file
·386 lines (302 loc) · 9.35 KB
/
cTcpSock.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "cTcpSock.h"
/** @file
@brief TCPソケット
@author TATSUYA
@date 2007/03/01
*/
/******************************************************************************/
/** @brief コンストラクタ
クラスの初期化を行います。
*/
cTcpSock::cTcpSock(){
// 接続フラグを未接続に設定
m_bIsConnect = FALSE;
}
/******************************************************************************/
/** @brief デストラクタ
クラスの解放を行います。
*/
cTcpSock::~cTcpSock(){
// 接続中の場合は切断する
if(IsConnect()){
Disconnect();
}
}
/******************************************************************************/
/**
接続先ホストのIPアドレスを取得します。
@return 接続先ホストのIPアドレス
*/
LPCSTR cTcpSock::GetPeerIpAddress(){
return inet_ntoa(m_Addr.sin_addr);
}
/******************************************************************************/
/**
接続先ホストのポート番号を取得します。
@return 接続先ホストのポート番号
*/
USHORT cTcpSock::GetPeerPort(){
return ntohs(m_Addr.sin_port);
}
/******************************************************************************/
/**
ソケットのコネクションが確立しているか確認します。
@return コネクションが確立している場合にはTRUEを返す。
コネクションが確立していない場合にはFALSEを返す。
*/
BOOL cTcpSock::IsConnect(){
return m_bIsConnect;
}
/******************************************************************************/
/**
サーバに対してコネクションの確率要求を行います。
@param[in] szHost 接続先ホストのホスト名またはIPアドレス
@param[in] nPort 接続先ホストのポート番号
@param[in] hWnd 非同期モードで利用する場合は、通知先のウィンドウハンドル<br>
同期モードで利用する場合はNULL
@param[in] nEventNo 非同期モードで利用する場合は、通知するメッセージID<br>
同期モードで利用する場合はNULL
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::Connect(LPCSTR szHost, USHORT nPort, HWND hWnd, USHORT nEventNo){
// 変数宣言
const int iYes = 1;
HOSTENT *host = NULL;
// 構造体に接続先を登録する
m_Addr.sin_family = AF_INET;
m_Addr.sin_port = htons(nPort);
m_Addr.sin_addr.s_addr = inet_addr(szHost);
if(m_Addr.sin_addr.S_un.S_addr == 0xffffffff){
host = gethostbyname(szHost);
if(host == NULL){
wsprintf(m_szErrMsg, "ホスト名からIPアドレスを取得できませんした。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
m_Addr.sin_addr.S_un.S_addr = *(PUINT)host->h_addr_list[0];
}
// ソケットの生成
m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// エラーチェック
if(m_Socket == INVALID_SOCKET){
wsprintf(m_szErrMsg, "ソケットの作成に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
// 非同期設定
if(!SetAsync(hWnd, nEventNo)){
return FALSE;
}
// TCP_NODELAY
if(setsockopt(m_Socket, IPPROTO_TCP, TCP_NODELAY, (LPCSTR)&iYes, sizeof(iYes)) == SOCKET_ERROR){
wsprintf(m_szErrMsg, "ソケットオプション(TCP_NODELAY)の設定に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
// エラーチェック
if(connect(m_Socket, (LPSOCKADDR)&m_Addr, sizeof(SOCKADDR)) == SOCKET_ERROR){
if(WSAGetLastError() != WSAEWOULDBLOCK){
wsprintf(m_szErrMsg, "接続失敗に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
}
// 接続フラグを接続中に設定
m_bIsConnect = TRUE;
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
非同期モードの設定を行います。
@param[in] hWnd 非同期モードで利用する場合は、通知先のウィンドウハンドル<br>
同期モードで利用する場合はNULL
@param[in] nEventNo 非同期モードで利用する場合は、通知するメッセージID<br>
同期モードで利用する場合はNULL
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::SetAsync(HWND hWnd, USHORT nEventNo){
// 非同期処理を設定
if(hWnd != NULL && nEventNo != 0){
if(WSAAsyncSelect(m_Socket, hWnd, nEventNo, FD_READ | FD_CLOSE) == SOCKET_ERROR){
wsprintf(m_szErrMsg, "非同期処理の設定に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
}
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
確立済みのコネクションを切断します。
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::Disconnect(){
// 接続中でなければ実行しない
if(!IsConnect()){
wsprintf(m_szErrMsg, "ソケットが接続中ではないため切断できません。");
return FALSE;
}
// ソケットのシャットダウン
if(shutdown(m_Socket, 2) == SOCKET_ERROR){
wsprintf(m_szErrMsg, "ソケットのシャットダウンに失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
// ソケットを閉じる
if(closesocket(m_Socket) == SOCKET_ERROR){
wsprintf(m_szErrMsg, "ソケットの解放に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
// 接続フラグを未接続に設定
m_bIsConnect = FALSE;
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
データの送信を行います。
指定されたバイト数、送信するまで処理はブロッキングされます。
@param[in] lpData 送信するデータの先頭ポインタ
@param[in] dwBytes 送信するデータのバイト数
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::SendData(LPVOID lpData, DWORD dwBytes){
// 変数宣言
int iSendBytes = 0;
LPSTR lpBuff = (LPSTR)lpData;
// 全てのデータを送信するまでループを続ける
do{
iSendBytes = send(m_Socket, lpBuff, dwBytes, 0);
if(iSendBytes == SOCKET_ERROR){
wsprintf(m_szErrMsg, "データの送信に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
// ポインタの移動
lpBuff += iSendBytes;
dwBytes -= iSendBytes;
}while(dwBytes > 0);
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
文字列の送信を行います。
@param[in] szStr 送信する文字列
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::SendStr(LPCSTR szStr){
return SendData((LPVOID)szStr, (DWORD)strlen(szStr) + 1);
}
/******************************************************************************/
/**
指定されたバイト数データの受信を行います。
指定されたバイト数、受信するまで処理はブロッキングされます。
@param[in] lpBuffer 受信したデータを格納する先頭ポインタ
@param[in] dwBytes 格納先のバイト数
@param[out] pdwRecvLen 実際に受信したバイト数
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::RecvData(LPVOID lpBuffer, DWORD dwBytes, DWORD *pdwRecvLen){
// 変数宣言
DWORD dwRecvBytes = 0;
LPSTR lpBuff = (LPSTR)lpBuffer;
int iByteRecv = 0;
// 指定したバイト数、受信するまでループを続ける
while(1){
iByteRecv = recv(m_Socket, (LPSTR)lpBuff, dwBytes - dwRecvBytes, 0);
if(iByteRecv == SOCKET_ERROR || iByteRecv == 0) {
break;
}
dwRecvBytes += iByteRecv;
lpBuff = (LPSTR)lpBuffer + dwRecvBytes;
if(dwBytes <= dwRecvBytes){
break;
}
}
// 受信したバイト数が0バイトであった場合
if(dwRecvBytes == 0){
wsprintf(m_szErrMsg, "データの受信に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
if(pdwRecvLen != NULL){
*pdwRecvLen = dwRecvBytes;
}
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
受信できるだけデータの受信を行います。
@param[in] lpBuffer 受信したデータを格納する先頭ポインタ
@param[in] dwBytes 格納先のバイト数
@param[out] pdwRecvLen 実際に受信したバイト数
@return 成功したときはTRUEを返す。
失敗したときはFALSEを返す。
*/
BOOL cTcpSock::RecvDataOnce(LPVOID lpBuffer, DWORD dwBytes, DWORD* pdwRecvLen){
// 変数宣言
int iByteRecv = 0;
iByteRecv = recv(m_Socket, (LPSTR)lpBuffer, dwBytes, 0);
// 受信したバイト数が0バイトであった場合
if(iByteRecv == 0){
wsprintf(m_szErrMsg, "データの受信に失敗しました。\n"
"ErrCode:%d",
WSAGetLastError());
return FALSE;
}
if(pdwRecvLen != NULL){
*pdwRecvLen = (DWORD)iByteRecv;
}
// 正常終了 バッファをクリア
ZeroMemory(m_szErrMsg, sizeof(m_szErrMsg));
return TRUE;
}
/******************************************************************************/
/**
引数のソケットハンドルと同一のソケットか判定する。
@param[in] sock 判定対象のソケットハンドル
@return 一致したときはTRUEを返す。
一致しなかったときはFALSEを返す。
*/
BOOL cTcpSock::MatchSock(SOCKET sock){
if(m_Socket == sock){
return TRUE;
}else{
return FALSE;
}
}
/******************************************************************************/
/**
エラーメッセージの表示をします。
*/
void cTcpSock::ErrMsgBox(){
if(strlen(m_szErrMsg) > 0){
MessageBox(NULL, m_szErrMsg, "Error[cTcpSock]", MB_OK | MB_ICONERROR);
}
return;
}