CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2005
    Posts
    31

    Unhappy Non-blocking socket - connection problem

    I have created a Dialog application. Acting as Server. And if I click a button the server will start listening for connections from the client.

    When I click the button to start listening ,
    (I am using the normal socket functions), so after issuing accept the dialog app hangs. So I modified the code in such a way that the socket call for accept does not block using the ioctl socket function on listening socket.
    Now the dialog app did not hang.

    My problem is , when I tried to connect with the client the connect is successful but the server side cannot accept the incoming connection instead I got an error when I used GetLastError as following:

    "A non-blocking socket operation could not be completed immediately."

    I want to know how should I able to achieve connection (or) How should I make the accept call succeeded.

    /*************************************************/
    Here is the part of Server code:

    ULONG NonBlock;
    listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    Address.sin_family = AF_INET;
    Address.sin_port = htons(listenPort);
    Address.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind the socket id to the local address
    if (bind(listenSocket,(struct sockaddr *) &WdxAddress, sizeof(WdxAddress)) == SOCKET_ERROR)
    {
    }
    if (listen(listenSocket, 100) == SOCKET_ERROR)
    {
    }

    NonBlock = 1;
    if (ioctlsocket(listenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
    {
    return;
    }

    clientAddressLength = sizeof(clientAddress);
    if ((connectSocket = accept(listenSocket,(struct sockaddr *) &clientAddress, &clientAddressLength)) < 0)
    {
    }

    /*************************************************/
    Here is the Client code:

    hostInfo = gethostbyname(szIPAddress);
    serverAddress.sin_family = hostInfo->h_addrtype;
    memcpy((char *) &serverAddress.sin_addr.s_addr,
    hostInfo->h_addr_list[0], myApp->hostInfo->h_length);
    serverAddress.sin_port = htons(myApp->serverPort);
    if (connect(clientSocket,(struct sockaddr *) &serverAddress,
    sizeof(serverAddress)) < 0)
    {
    return FALSE;
    }

    /*************************************************/

    Can anybody help me in this regard?????
    I dont want my Server Dialog app to be hanging until there is a connection is made by the client. Because I want to do some other tasks also with the dialog app meanwhile waiting for connections.

  2. #2
    Join Date
    Jan 2005
    Location
    Gothenburg, Sweden
    Posts
    134

    Re: Non-blocking socket - connection problem

    You could run the blocking system calls in another thread. That way your dialog thread still runs and your networking thread sleeps until something happens on the network.

    Another approach (if you're running a os that supports signals) is to use asynchronous io and set up a signal handler for the SIGIO signal and do the networking from that handler.
    If you try and take a cat apart to see how it works,
    the first thing you'll have on your hands is a nonworking cat

  3. #3
    Join Date
    Jan 2005
    Posts
    31

    Re: Non-blocking socket - connection problem

    Thank you very much.


    Now I am using the thread to listen for the incoming connections. So my dialog app is having no problem. It can respond , meanwhile waiting for connection from client.

    Thank you again for your tips.

  4. #4
    Join Date
    Feb 2005
    Location
    Pittsburgh
    Posts
    17

    Re: Non-blocking socket - connection problem

    Is this the preferred method for setting a socket to non blocking? I've added it to my code and it errors. Any ideas? Everything works fine as a blocking socket.

    unsigned long noblock;

    if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
    handle_error();

    //Set to non-blocking socket
    noblock = 1;
    if ( (ioctlsocket(sock, FIONBIO, &noblock)) == INVALID_SOCKET )
    handle_error();

  5. #5
    Join Date
    Feb 2005
    Location
    Pittsburgh
    Posts
    17

    Re: Non-blocking socket - connection problem

    nevermind. i moved it down after the "connect" part of the socket initialize code and its working fine.
    sorry

+ Reply to 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



HTML5 Development Center

Click Here to Expand Forum to Full Width