Thread: Create a blocked/unblocked socket?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    57

    Create a blocked/unblocked socket?

    Hello,
    How does one create a blocked socket?
    How does one create an unblocked socket?
    Where is the Win API documentation for this?
    Thank you.

    Here is the standard generic code for creating a socket.

    The generic code:
    Code:
    //gcc e1.c -lws2_32 -o e1.exe
    #include <windows.h>
    #include <stdio.h>
    #include <winsock.h>
    
    #define PORT (u_short) 44444
    #define DEST_IP_ADDR "127.0.0.1"
    
    int main()
    {
      WSADATA Data;
      SOCKADDR_IN destSockAddr;
      SOCKET destSocket;
      unsigned long destAddr;
      int status;
    
      status = WSAStartup(MAKEWORD(1, 1), &Data);
      if (status != 0)
        { printf("WSAStartup unsuccessful\n"); }
    
      destAddr = inet_addr(DEST_IP_ADDR);
      memcpy(&destSockAddr.sin_addr, &destAddr, sizeof(destAddr));
      destSockAddr.sin_port = htons(PORT);
      destSockAddr.sin_family = AF_INET;
    
      destSocket = socket(AF_INET, SOCK_STREAM, 0);
      if (destSocket == INVALID_SOCKET) {
        printf("socket unsuccessful\n");
        return(1);
      }
    
      printf("socket created.\n");
    }
    Thanks for any help.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    I found this:

    http://msdn.microsoft.com/library/de...tlsocket_2.asp
    Code:
    //-------------------------
    // 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.
    int iMode = 0;
    ioctlsocket(m_socket, FIONBIO, (u_long FAR*) &iMode);

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's how it's done. Sockets are blocking by default until you change them with ioctlsocket. Another option you have which I find easier to work with than nonblocking sockets is asynchronous sockets. Read the MSDN documentation for WSAAsyncSelect for more information on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Asynchronous Socket Thread closes after first receive
    By AndreasS in forum C# Programming
    Replies: 1
    Last Post: 04-16-2009, 08:31 AM
  3. Socket abruptly closes after function ends
    By Sfpiano in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-08-2005, 04:49 PM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Socket Problems
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 08-18-2001, 06:59 AM