Solved

WSAENOTSOCK error

Posted on 2006-07-03
9
679 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

688 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