Solved

WSAENOTSOCK error

Posted on 2006-07-03
9
635 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now