Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. NonBlocking Client Socket - Connection Status

NonBlocking Client Socket - Connection Status

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminquestion
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark Salsbery

    I should have qualified that with "a successful" FD_CONNECT. Check for an error:

    WSAEnumNetworkEvents(client_socket, g_hClientEvent, &NetworkClientEvents);
    switch ( NetworkClientEvents.lNetworkEvents )
    {
    ...
    case FD_CONNECT:
    if (0 != NetworkClientEvents.iErrorCode[FD_CONNECT_BIT])
    printf("Connect Failed Message: \n\r");
    else
    printf("Client Connected Message: \n\r");
    break;
    ...
    }

    Out of curiosity, why would you poll if you are using an event? It just needlessly eats CPU time.

    S Offline
    S Offline
    ScotDolan
    wrote on last edited by
    #5

    Thanks Mark, that seem to work! I found in the MSDN that a FD_CONNECT error can be used to extract more information but, i am have not had success getting that information. I am guessing that FD_CONNECT_BIT is the indexing for the 10 array element of iErrorCode. Where can i find the other meanings of the array elements. Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using the WSAGETSELECTERROR macro. Possible error codes for each network event are listed in the following table. Event: FD_CONNECT Error code Meaning WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was rejected. WSAENETUNREACH The network cannot be reached from this host at this time. WSAEFAULT The namelen parameter is invalid. WSAEINVAL The socket is already bound to an address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTCONN The socket is not connected. WSAETIMEDOUT Attempt to connect timed out without establishing a connection.

    Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

    M 3 Replies Last reply
    0
    • S ScotDolan

      Thanks Mark, that seem to work! I found in the MSDN that a FD_CONNECT error can be used to extract more information but, i am have not had success getting that information. I am guessing that FD_CONNECT_BIT is the indexing for the 10 array element of iErrorCode. Where can i find the other meanings of the array elements. Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using the WSAGETSELECTERROR macro. Possible error codes for each network event are listed in the following table. Event: FD_CONNECT Error code Meaning WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was rejected. WSAENETUNREACH The network cannot be reached from this host at this time. WSAEFAULT The namelen parameter is invalid. WSAEINVAL The socket is already bound to an address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTCONN The socket is not connected. WSAETIMEDOUT Attempt to connect timed out without establishing a connection.

      Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #6

      ScotDolan wrote:

      I am guessing that FD_CONNECT_BIT is the indexing for the 10 array element of iErrorCode. Where can i find the other meanings of the array elements.

      Correctemundo. In the platform SDK, Windows Sockets 2, under WSANETWORKEVENTS struct: iErrorCode Array that contains any associated error codes, with an array index that corresponds to the position of event bits in lNetworkEvents. The identifiers FD_READ_BIT, FD_WRITE_BIT and others can be used to index the iErrorCode array. They were too lazy to list them :)

      ScotDolan wrote:

      Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using the WSAGETSELECTERROR macro.

      That would be if you were using windows-based events (WSAAsyncSelect) I believe. Since you are using WSAEventSelect, you get the error messages codes from the WSANETWORKEVENTS struct. Mark -- modified at 18:35 Monday 22nd January, 2007

      1 Reply Last reply
      0
      • S ScotDolan

        Thanks Mark, that seem to work! I found in the MSDN that a FD_CONNECT error can be used to extract more information but, i am have not had success getting that information. I am guessing that FD_CONNECT_BIT is the indexing for the 10 array element of iErrorCode. Where can i find the other meanings of the array elements. Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using the WSAGETSELECTERROR macro. Possible error codes for each network event are listed in the following table. Event: FD_CONNECT Error code Meaning WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was rejected. WSAENETUNREACH The network cannot be reached from this host at this time. WSAEFAULT The namelen parameter is invalid. WSAEINVAL The socket is already bound to an address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTCONN The socket is not connected. WSAETIMEDOUT Attempt to connect timed out without establishing a connection.

        Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #7

        In my post above I stated "Since you are using WSAEventSelect, you get the error messages from the WSANETWORKEVENTS struct." I forgot to mention that it only applies when WSAGetLastError() after a failed non-blocking socket op returns WSAEWOULDBLOCK. Sometimes the error is reported immediately and you'll never get the FD_xxx event. Mark

        S 1 Reply Last reply
        0
        • M Mark Salsbery

          I should have qualified that with "a successful" FD_CONNECT. Check for an error:

          WSAEnumNetworkEvents(client_socket, g_hClientEvent, &NetworkClientEvents);
          switch ( NetworkClientEvents.lNetworkEvents )
          {
          ...
          case FD_CONNECT:
          if (0 != NetworkClientEvents.iErrorCode[FD_CONNECT_BIT])
          printf("Connect Failed Message: \n\r");
          else
          printf("Client Connected Message: \n\r");
          break;
          ...
          }

          Out of curiosity, why would you poll if you are using an event? It just needlessly eats CPU time.

          M Offline
          M Offline
          Mike ONeill
          wrote on last edited by
          #8

          Don't use a switch statement to check the network event. More than one event can be signalled at a time. The events will be OR'd together. Mike

          M 1 Reply Last reply
          0
          • M Mike ONeill

            Don't use a switch statement to check the network event. More than one event can be signalled at a time. The events will be OR'd together. Mike

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #9

            Thanks Mike! I didn't even think about that....I just copy/pasted from the OP's code :) Mark

            M 1 Reply Last reply
            0
            • S ScotDolan

              Thanks Mark, that seem to work! I found in the MSDN that a FD_CONNECT error can be used to extract more information but, i am have not had success getting that information. I am guessing that FD_CONNECT_BIT is the indexing for the 10 array element of iErrorCode. Where can i find the other meanings of the array elements. Additional error codes can be set when an application window receives a message. This error code is extracted from the lParam in the reply message using the WSAGETSELECTERROR macro. Possible error codes for each network event are listed in the following table. Event: FD_CONNECT Error code Meaning WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was rejected. WSAENETUNREACH The network cannot be reached from this host at this time. WSAEFAULT The namelen parameter is invalid. WSAEINVAL The socket is already bound to an address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTCONN The socket is not connected. WSAETIMEDOUT Attempt to connect timed out without establishing a connection.

              Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #10

              Also, see Mike's post below :)

              1 Reply Last reply
              0
              • M Mark Salsbery

                Thanks Mike! I didn't even think about that....I just copy/pasted from the OP's code :) Mark

                M Offline
                M Offline
                Mike ONeill
                wrote on last edited by
                #11

                I didn't realize it was the OP's code (which was so long and unformatted that I barely even glanced at it). Sorry. Mike

                M 1 Reply Last reply
                0
                • M Mark Salsbery

                  In my post above I stated "Since you are using WSAEventSelect, you get the error messages from the WSANETWORKEVENTS struct." I forgot to mention that it only applies when WSAGetLastError() after a failed non-blocking socket op returns WSAEWOULDBLOCK. Sometimes the error is reported immediately and you'll never get the FD_xxx event. Mark

                  S Offline
                  S Offline
                  ScotDolan
                  wrote on last edited by
                  #12

                  For what i am learning, it seems like WSAAsyncSelect() with WSAGETSELECTERROR marco might be a better solution than WSAEnumNetworkEvents(client_socket, g_hClientEvent, &NetworkClientEvents); It seems that WSAAsyncSelect() will provide me with more information and less chance of missing a event. A FD_CLOSE event produce the below error codes, but from what I understand if want to use WSAEnumNetworkEvents( ) I will not be able to get access to the below error message without a call to WSAGetLastError() and even that might miss a error code. WSAEAFNOSUPPORT, Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED, The attempt to connect was rejected. WSAENETUNREACH, The network cannot be reached from this host at this time. WSAEFAULT, The namelen parameter is invalid. WSAEINVAL, The socket is already bound to an address. WSAEISCONN, The socket is already connected. WSAEMFILE, No more file descriptors are available. WSAENOBUFS, No buffer space is available. The socket cannot be connected. WSAENOTCONN, The socket is not connected. WSAETIMEDOUT, Attempt to connect timed out without establishing a connection.

                  Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

                  M 1 Reply Last reply
                  0
                  • S ScotDolan

                    For what i am learning, it seems like WSAAsyncSelect() with WSAGETSELECTERROR marco might be a better solution than WSAEnumNetworkEvents(client_socket, g_hClientEvent, &NetworkClientEvents); It seems that WSAAsyncSelect() will provide me with more information and less chance of missing a event. A FD_CLOSE event produce the below error codes, but from what I understand if want to use WSAEnumNetworkEvents( ) I will not be able to get access to the below error message without a call to WSAGetLastError() and even that might miss a error code. WSAEAFNOSUPPORT, Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED, The attempt to connect was rejected. WSAENETUNREACH, The network cannot be reached from this host at this time. WSAEFAULT, The namelen parameter is invalid. WSAEINVAL, The socket is already bound to an address. WSAEISCONN, The socket is already connected. WSAEMFILE, No more file descriptors are available. WSAENOBUFS, No buffer space is available. The socket cannot be connected. WSAENOTCONN, The socket is not connected. WSAETIMEDOUT, Attempt to connect timed out without establishing a connection.

                    Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #13

                    ScotDolan wrote:

                    For what i am learning, it seems like WSAAsyncSelect() with WSAGETSELECTERROR marco might be a better solution than WSAEnumNetworkEvents(client_socket, g_hClientEvent, &NetworkClientEvents); It seems that WSAAsyncSelect() will provide me with more information and less chance of missing a event.

                    Unless you're using a buggy socket implementation, you won't miss any events using either method. The difference is, WSAAsyncSelect uses a window to notify you. WSAEventSelect uses an event. The window method is only really useful if you want to do asynchronous socket ops in a single threaded app. If you have a separate thread for socket communication then an event is easier and more efficient (the thread can wait on the event). I'm not sure what you're getting at with the FD_CLOSE errors. I wouldn't expect any error (or even look for one). After receiving the FD_CLOSE notification there's nothing that can be done on the socket except reading any remaining unread/buffered receive data. Mark

                    1 Reply Last reply
                    0
                    • M Mike ONeill

                      I didn't realize it was the OP's code (which was so long and unformatted that I barely even glanced at it). Sorry. Mike

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #14

                      No problem. I always appreciate a heads up when I'm wrong :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups