ioctlsocket的作用

ioctlsocket is short for io control socket,也就是指示SOCKET的IO mode.可以为阻塞或者非阻塞.

以下代码copy from MSDN。代码中iMode参数指示了阻塞或者非阻塞的模式。




===================================================================================================================================================

#include <winsock2.h> #include <stdio.h> #pragma comment(lib, "Ws2_32.lib") void main() { //------------------------- // Initialize Winsock WSADATA wsaData; int iResult; u_long iMode = 0; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) printf("Error at WSAStartup()\n"); //------------------------- // Create a SOCKET object. SOCKET m_socket; m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); WSACleanup(); return; } //------------------------- // Set the socket I/O mode: In this case FIONBIO // enables or disables the blocking mode for the // socket based on the numerical value of iMode. // If iMode = 0, blocking is enabled; // If iMode != 0, non-blocking mode is enabled. iResult = ioctlsocket(m_socket, FIONBIO, &iMode); if (iResult != NO_ERROR) printf("ioctlsocket failed with error: %ld\n", iResult); }

===================================================================================================================================================

你可能感兴趣的:(socket)