在一个socket使用的过程中,可不可以切换?

解决方案 »

  1.   

    可以切换啊。不过要进行相应的处理。
    unsigned long ul = 1;

    while(true)
    {
    AcceptSocket = accept(ListenSocket, NULL, NULL);

    if ( 0 == ul )
    {
    ul = 1;
    }
    else
    {
    ul = 0;
    }
    //切换模式
    iRet = ioctlsocket(ListenSocket, FIONBIO, &ul); closesocket(AcceptSocket);
    }
      

  2.   

    int ioctlsocket(SOCKET s, long cmd, u_long FAR *argp);cmd:FIONBIO 
    Use with a nonzero argp parameter to enable the nonblocking mode of socket s. The argp parameter is zero if nonblocking is to be disabled. The argp parameter points to an unsigned long value. When a socket is created, it operates in blocking mode by default (nonblocking mode is disabled). This is consistent with BSD sockets. 
    The WSAAsyncSelect and WSAEventSelect functions automatically set a socket to nonblocking mode. If WSAAsyncSelect or WSAEventSelect has been issued on a socket, then any attempt to use ioctlsocket to set the socket back to blocking mode will fail with WSAEINVAL. To set the socket back to blocking mode, an application must first disable WSAAsyncSelect by calling WSAAsyncSelect with the lEvent parameter equal to zero, or disable WSAEventSelect by calling WSAEventSelect with the lNetworkEvents parameter equal to zero. 
      

  3.   

    切换模式会影响所有的socket吗?还是只影响当前的?还有,看了楼上的代码,切换模式以后是重新打开socket才有效??
      

  4.   

    iRet = ioctlsocket(ListenSocket, FIONBIO, &ul);
    对指定的SOCKET进行切换啊。