• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

WSAENOTSOCK error

I am getting a WSAENOTSOCK error when I do a recv().  I want to test the socket before hand so I don't get a WSAENOTSOCK error on the recieve.  Basically right now I'm doing

code.....
recv();
handle WSAENOTSOCK
code.....

I want to do

code...
check for WSAENOTSOCK
recv();
//No need to handle WSAENOTSOCK cause we already checked right before the recv
code....
0
cophi
Asked:
cophi
  • 3
  • 3
  • 2
  • +1
1 Solution
 
e_tadeuCommented:
Are you checking connect() result value against SOCKET_ERROR ??
0
 
cophiAuthor Commented:
yeah i'm not gettin an error.   the socket is being disconnected occasinoaly.  it doesn't matter... all i'm asking for is how to check for wsaenotsock?
thanks!
0
 
jkrCommented:
>>all i'm asking for is how to check for wsaenotsock?

Use 'WSAGetLastError()': http://msdn.microsoft.com/library/en-us/winsock/winsock/wsagetlasterror_2.asp

E.g.

int n = recv(s,buf,len,flags);

if (SOCKET_ERROR == n) {

  int nErr = WSAGetLastError();

  if (WSAENOTSOCK == nErr) {

    //...
  }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Infinity08Commented:
>> code...
>> check for WSAENOTSOCK
>> recv();
>> //No need to handle WSAENOTSOCK cause we already checked right before the recv
>> code....

I just want to note that this is not the good way to handle error situations. Don't omit the error checks after any function call. So, either do this :

   code ...
   if WSAENOTSOCK -> handle
   else recv();
   if WSAENOTSOCK -> handle
   code ...

or :

   code ...
   recv();
   if WSAENOTSOCK -> handle
   code ...

The reason is that the test before recv() doesn't give you 100% certainty that the socket will still be OK when recv() is executed. If you leave out the check after recv(), you might get into a situation where recv() caused an error, but you still handle the buffer as if it contained valid data.

I'll repeat : always check for errors on each function return !! It will save you a lot of head aches trying to trace down nasty bugs ...
0
 
cophiAuthor Commented:
I guess i'm not getting my question off right.  I'm calling closesocket on a socket.  Occasinoally the socket try's to be reused on a recv().  This causes an error.  I'm using appverifier which gives an exception of basically WSAENOTSOCK.  Since the exception occurs within the recv, I obvioulsy can't trap it afterwards, I have to find out how to tell if my socket is closed before the recv.

How do you check for a socket to be closed or not 'valid'

Thanks!
0
 
jkrCommented:
>>How do you check for a socket to be closed or not 'valid'

You could just try to get the socket's error status, e.g.

int nStat;
int nErr = getsockopt(s,SO_ERROR,(char*)&nStat,sizeof(int));

if (WSAENOTSOCK == nErr) {

    //...
}
0
 
Infinity08Commented:
>> Occasinoally the socket try's to be reused on a recv().
Can't you re-design the code so this will never happen ? Seems a much cleaner solution to me

>> Since the exception occurs within the recv, I obvioulsy can't trap it afterwards,
Why not ? If recv() detects an error, it will return SOCKET_ERROR, and you can retrieve the exact error code using WSAGetLastError(). You should always check for SOCKET_ERROR anyway, as I said earlier.
0
 
cophiAuthor Commented:
Infinity08, not sure if you have ever used AppVerifier, but once the exception occurs, program execution stops.  So I can't trap anything afterwards. That's why I was looking for a solution before the recv.
0
 
Infinity08Commented:
>> Infinity08, not sure if you have ever used AppVerifier
Apparently not :) My apologies.

AppVerifier doesn't give you the opportunity to recover from an error ? Sounds strange to me ... especially if some of your code depends on errors to verify eg. availability of a resource.
Would in that case a ping application crash when used with AppVerifier and an unreachable host ? If that's the case, I wouldn't depend on it too much, because it requires modifications to your code that only make it more complicated, serving only to keep AppVerifier happy ...

Or maybe I'm missing something ... :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now