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. WSAGetLastError()

WSAGetLastError()

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminregexhelpquestion
9 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.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all. I was trying to make something that will try to reconnect on a severed connection from the server. Now i know WSAGetLastError only works if a message is trying to be sent from client to server, and a error code is returned in case the message doesnt go through. So i did the following: Started a server. Connected client Turned server off. And i got error code 183 in return. So i figured, if i can match it then i should be able to reconnect. if(WSAGetLastError()==183){ ...// Reconnect } But to no avail it didnt work. So what should i do in a situation like this? Any suggestions? Thanx in advance!

    D 1 Reply Last reply
    0
    • D dellthinker

      Hi all. I was trying to make something that will try to reconnect on a severed connection from the server. Now i know WSAGetLastError only works if a message is trying to be sent from client to server, and a error code is returned in case the message doesnt go through. So i did the following: Started a server. Connected client Turned server off. And i got error code 183 in return. So i figured, if i can match it then i should be able to reconnect. if(WSAGetLastError()==183){ ...// Reconnect } But to no avail it didnt work. So what should i do in a situation like this? Any suggestions? Thanx in advance!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Where are you checking for this value at?


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      D 1 Reply Last reply
      0
      • D David Crow

        Where are you checking for this value at?


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        D Offline
        D Offline
        dellthinker
        wrote on last edited by
        #3

        I setup a function thread to loop over and over to send request from the server as long as Connected=true; once thats set to true the function starts to send request. I made a simple ofstream function to test whether or not it works if the connection was severed. ofstream out; out.open("error.txt",ios::app); out << WSAGetLastError << endl; out.close(); Soon as i turned the server off it made the file with error code 183 in it. (Several times too because of the loop) I need to know how to use this in a if statement.

        D 1 Reply Last reply
        0
        • D dellthinker

          I setup a function thread to loop over and over to send request from the server as long as Connected=true; once thats set to true the function starts to send request. I made a simple ofstream function to test whether or not it works if the connection was severed. ofstream out; out.open("error.txt",ios::app); out << WSAGetLastError << endl; out.close(); Soon as i turned the server off it made the file with error code 183 in it. (Several times too because of the loop) I need to know how to use this in a if statement.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          dellthinker wrote:

          I setup a function thread to loop over and over to send request...

          If no error occurs, send() returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError().

          dellthinker wrote:

          out << WSAGetLastError << endl;

          You do realize that this will print the address of WSAGetLastError() rather than actually call the function, don't you?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          D 1 Reply Last reply
          0
          • D David Crow

            dellthinker wrote:

            I setup a function thread to loop over and over to send request...

            If no error occurs, send() returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError().

            dellthinker wrote:

            out << WSAGetLastError << endl;

            You do realize that this will print the address of WSAGetLastError() rather than actually call the function, don't you?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            D Offline
            D Offline
            dellthinker
            wrote on last edited by
            #5

            Ok, and error code 183 was returned. if(WSAGetLastError==183){ } That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?

            D 1 Reply Last reply
            0
            • D dellthinker

              Ok, and error code 183 was returned. if(WSAGetLastError==183){ } That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              dellthinker wrote:

              if(WSAGetLastError==183){

              You do realize this compares the address of WSAGetLastError() to 183 rather than actually call the function, don't you?

              dellthinker wrote:

              That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?

              Your question is really confusing. Do you have something akin to:

              while (more_data_exists)
              {
              int nRet = send(...);
              if (SOCKET_ERROR == nRet)
              DWORD dwError = WSAGetLastError();
              }


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              D 1 Reply Last reply
              0
              • D David Crow

                dellthinker wrote:

                if(WSAGetLastError==183){

                You do realize this compares the address of WSAGetLastError() to 183 rather than actually call the function, don't you?

                dellthinker wrote:

                That doesnt work. If 183 is some code indicating that the data sent was lots and or not sent then there should be a way to use this in a if statement. Correct?

                Your question is really confusing. Do you have something akin to:

                while (more_data_exists)
                {
                int nRet = send(...);
                if (SOCKET_ERROR == nRet)
                DWORD dwError = WSAGetLastError();
                }


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                D Offline
                D Offline
                dellthinker
                wrote on last edited by
                #7

                DavidCrow wrote:

                our question is really confusing. Do you have something akin to: while (more_data_exists) { int nRet = send(...); if (SOCKET_ERROR == nRet) DWORD dwError = WSAGetLastError(); }

                Ok let me be more specific. Im going to go step-by-step of what im trying to do here. If a program that is 'connected' to a server all of a sudden gets disconnected for whatever reason it has to do on the server side, im trying to get it 'reconnect'. In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?

                J D 2 Replies Last reply
                0
                • D dellthinker

                  DavidCrow wrote:

                  our question is really confusing. Do you have something akin to: while (more_data_exists) { int nRet = send(...); if (SOCKET_ERROR == nRet) DWORD dwError = WSAGetLastError(); }

                  Ok let me be more specific. Im going to go step-by-step of what im trying to do here. If a program that is 'connected' to a server all of a sudden gets disconnected for whatever reason it has to do on the server side, im trying to get it 'reconnect'. In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?

                  J Offline
                  J Offline
                  JudyL_MD
                  wrote on last edited by
                  #8

                  Here's my usual test for socket closure. Once you detect the socket closure, set some flag and exit from your routine. In your main program, check for that flag and call your connect code. I wouldn't put it inside the if/else code block - connection code is too problematic and time-consuming.

                  iResult = send (m_hSocket, pBuffer, iLength, 0);
                  if (iResult == SOCKET_ERROR)
                  {
                  iResult = WSAGetLastError ()
                  if ((iResult == WSAENETDOWN) ||
                  (iResult == WSAENETRESET) ||
                  (iResult == WSAECONNABORTED) ||
                  (iResult == WSAETIMEDOUT) ||
                  (iResult == WSAECONNRESET))
                  {
                  // socket closed
                  }
                  else
                  {
                  // some funky send error
                  }

                  Judy

                  1 Reply Last reply
                  0
                  • D dellthinker

                    DavidCrow wrote:

                    our question is really confusing. Do you have something akin to: while (more_data_exists) { int nRet = send(...); if (SOCKET_ERROR == nRet) DWORD dwError = WSAGetLastError(); }

                    Ok let me be more specific. Im going to go step-by-step of what im trying to do here. If a program that is 'connected' to a server all of a sudden gets disconnected for whatever reason it has to do on the server side, im trying to get it 'reconnect'. In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    dellthinker wrote:

                    In order for me to do that im looking for whatever error WSAGetLastError might spit out. Compare that error with an if statement and do whatever is necessary to reconnect to the server. Understand me now?

                    Yes, it makes sense, but still does not explain why you are having trouble comparing the return value from WSAGetLastError() with some constant, like:

                    DWORD dwError = WSAGetLastError();
                    if (183 == dwError)
                    ...

                    Furthermore, error 183 equates to ERROR_ALREADY_EXISTS, which I doubt your socket functions are returning when a disconnect has happened.


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    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