Solved

WSAENOTSOCK error

Posted on 2006-07-03
9
654 Views
Last Modified: 2008-01-09
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
Comment
Question by:cophi
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:e_tadeu
ID: 17033033
Are you checking connect() result value against SOCKET_ERROR ??
0
 

Author Comment

by:cophi
ID: 17033247
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
 
LVL 86

Expert Comment

by:jkr
ID: 17033683
>>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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:Infinity08
ID: 17035918
>> 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
 

Author Comment

by:cophi
ID: 17043654
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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17046424
>>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
 
LVL 53

Expert Comment

by:Infinity08
ID: 17048577
>> 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
 

Author Comment

by:cophi
ID: 17052437
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 17056404
>> 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question