WSAGetLastError()
-
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! -
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!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
-
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
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. -
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.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 ofSOCKET_ERROR
is returned, and a specific error code can be retrieved by callingWSAGetLastError()
.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
-
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 ofSOCKET_ERROR
is returned, and a specific error code can be retrieved by callingWSAGetLastError()
.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
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? -
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?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
-
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
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?
-
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?
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
-
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?
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 toERROR_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