28 int ret = ::WSAStartup (MAKEWORD (2, 0), &
_wsaData);
41 _socket = ::socket (af, type, protocol);
51 ADDRESS_FAMILY family, u_short portNo, LPCTSTR lpctszAddress)
57 if (lpctszAddress != NULL)
59 INT ret = ::InetPton (family, lpctszAddress, &(
_SockAddrIn.sin_addr));
73 int ret = ::setsockopt (
74 _socket, level, optname, (
const char*)&value,
sizeof (value));
112 BOOL response = TRUE;
114 int ret = ::listen (
_socket, 5);
127 BOOL response = TRUE;
129 int ret = ::ioctlsocket (
_socket, cmd, &value);
139 LPCTSTR lpctszIpAddr, u_short portNo)
146 ret = this->
Socket (AF_INET, SOCK_DGRAM, 0);
147 if (ret == FALSE)
break;
149 ret = this->
SetSockAddr (AF_INET, portNo, lpctszIpAddr);
150 if (ret == FALSE)
break;
153 if (ret == FALSE)
break;
170 LPCTSTR lpctszIpAddr, u_short portNo,
int retryInterval,
int retryCount)
177 ret = this->
Socket (AF_INET, SOCK_STREAM, 0);
178 if (ret == FALSE)
break;
185 ret = this->
SetSockOpt (SOL_SOCKET, SO_KEEPALIVE, 1);
186 if (ret == FALSE)
break;
188 ret = this->
SetSockAddr (AF_INET, portNo, lpctszIpAddr);
189 if (ret == FALSE)
break;
195 ret = this->
Ioctl (FIONBIO, 1);
196 if (ret == FALSE)
break;
201 if (ret == TRUE || this->
GetErrNo () == WSAEISCONN)
206 ret = this->
Ioctl (FIONBIO, 0);
217 TCHAR tszMessage[80];
218 wsprintf (tszMessage, _T (
"[FAILED]connect() error. reason=%d\n"), this->
GetErrNo ());
219 OutputDebugString (tszMessage);
221 Sleep (retryInterval);
233 BOOL response = FALSE;
237 response = this->
Socket (AF_INET, SOCK_STREAM, 0);
238 if (response == FALSE)
247 response = this->
SetSockOpt (SOL_SOCKET, SO_REUSEADDR, 1);
248 if (response == FALSE)
253 response = this->
SetSockAddr (AF_INET, portNo, lpctszIpAddr);
254 if (response == FALSE)
259 response = this->
Bind ();
260 if (response == FALSE)
270 response = this->
SetSockOpt (SOL_SOCKET, SO_KEEPALIVE, 1);
271 if (response == FALSE)
276 response = this->
Listen ();
277 if (response == FALSE)
300 SOCKET socketArray[WSA_MAXIMUM_WAIT_EVENTS]{
_socket };
301 WSAEVENT eventArray[WSA_MAXIMUM_WAIT_EVENTS]{
_wsaEvent };
304 int iRet = ::WSAEventSelect (
_socket, eventArray[arrayIndex],
305 FD_ACCEPT | FD_CLOSE);
311 DWORD dwEvent = ::WSAWaitForMultipleEvents (++arrayIndex, eventArray,
312 FALSE, WSA_INFINITE, FALSE);
313 if (dwEvent == WSA_WAIT_FAILED)
317 dwEvent -= WSA_WAIT_EVENT_0;
319 WSANETWORKEVENTS events{ 0 };
320 int iEvent = ::WSAEnumNetworkEvents (socketArray[dwEvent],
321 eventArray[dwEvent], &events);
328 if (events.lNetworkEvents & FD_ACCEPT)
338 SOCKADDR_IN mySockAddrIn;
339 ZeroMemory (&mySockAddrIn,
sizeof (mySockAddrIn));
340 int length =
sizeof (mySockAddrIn);
341 SOCKET mySocket = ::accept (socketArray[dwEvent],
342 (PSOCKADDR)&mySockAddrIn, &length);
343 if (mySocket != INVALID_SOCKET)
345 TCHAR tszAcceptIPAddress[INET_ADDRSTRLEN];
346 PCWSTR ret = ::InetNtop (
347 AF_INET, &mySockAddrIn.sin_addr, tszAcceptIPAddress,
351 USHORT wAcceptPort = ::ntohs (mySockAddrIn.sin_port);
353 mySocket, tszAcceptIPAddress, wAcceptPort);
372 LPCTSTR lpctszHostName, LPCTSTR lptszIpAddr)
const
374 BOOL response = FALSE;
377 ZeroMemory (&addrInfo,
sizeof (addrInfo));
378 addrInfo.ai_family = AF_INET;
379 addrInfo.ai_socktype = SOCK_STREAM;
380 addrInfo.ai_protocol = IPPROTO_TCP;
381 ADDRINFO* result = NULL;
382 DWORD dwBufferLen = 16;
386 ret = ::GetAddrInfo (
389 (
const ADDRINFOW*)&addrInfo,
390 (PADDRINFOW*)&result);
393 ret = ::WSAAddressToString (
395 (DWORD)result->ai_addrlen,
ソケットに関するWindowsAPIを集約したクラス
int APIENTRY Startup()
Windowsソケットの使用準備
BOOL APIENTRY Prepare(u_short portNo, LPCTSTR lpctszIpAddr=NULL)
TCPポートのlisten(),accept()の準備
BOOL APIENTRY Socket(int af, int type, int protocol)
BOOL CancelWait()
Wait()待機中のキャンセル処理
APIENTRY ~SocketBuilder()
デストラクタ
BOOL APIENTRY Ioctl(long cmd, u_long arg) const
TcpConnector *APIENTRY CreateTcpConnector(LPCTSTR lpctszIpAddr, u_short portNo, int retryInterval, int retryCount)
TcpConnectorの作成
UdpConnector *APIENTRY CreateUdpConnector(LPCTSTR lpctszIpAddr, u_short portNo)
UdpConnectorの作成
BOOL APIENTRY Listen() const
BOOL APIENTRY SetSockAddr(ADDRESS_FAMILY family, u_short portNo, LPCTSTR lpctszAddress)
BOOL APIENTRY GetHostByName(LPCTSTR lpctszHostName, LPCTSTR lptszIpAddr) const
ホスト名からIPアドレスのDNS解決
BOOL APIENTRY SetSockOpt(int level, int optname, int value) const
TcpConnector *APIENTRY Wait()
TCP接続のlisten(),accept()待機関数
APIENTRY SocketBuilder()
コンストラクタ
BOOL APIENTRY Bind() const
BOOL APIENTRY Connect() const
DWORD APIENTRY GetErrNo() const
WinSock API呼び出し時にエラーとなった時、 エラーの詳細を返します。
int APIENTRY Close()
ソケットが使用されていた場合、クローズします。
SOCKET _socket
このクラスで管理するソケットオブジェクト
TCP通信に関するWindowsAPIを集約したクラス
UDP通信に関するWindowsAPIを集約したクラス