+ Reply to Thread
Results 1 to 2 of 2

Thread: How can I create a non-blocking socket using TCP?

  1. #1

    How can I create a non-blocking socket using TCP?

    I want to make a non-blokcing socket.

    I heard that by default, in winsock, the connection we make is blocking.

    I want to me this non-blocking.

    How to do this? I have searched for setsockopts but it did not help.

    Please advice.

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    49
    Use ioctlsocket API for this.

    Following is what you want:

    Code:
    void CreateSocket(bool bIsBlocking)
    {
    	WSADATA wsaData = { 0 } ;
    
    	WSAStartup(MAKEWORD(2, 2), &wsaData) ;
    
    	SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
    	if (INVALID_SOCKET != hSocket)
    	{
    		unsigned long ulBlockingMode = bIsBlocking ? 0 : 1 ;
    
    		int nResult = ioctlsocket(hSocket, FIONBIO, &ulBlockingMode) ;
    
    		closesocket(hSocket) ;
    		hSocket = INVALID_SOCKET ;
    	}
    
    	WSACleanup();
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts