Solved

WSAENOTSOCK error

Posted on 2006-07-03
9
649 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

803 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