Link to home
Start Free TrialLog in
Avatar of gerhardschoeman
gerhardschoeman

asked on

Winsock 10047 error in release build

I have an application that connects to a socket using AF_INET family and SOCK_STREAM proto. This seems to work 100% when I compile and run the debug build. But as soon as I change to release build in VC++ i can no longer connect to the very same socket. I get a 10047 error?????? I have made sure as far as possible that my compile directives are correct and my link includes are the same for debug and release builds. I am using #include <winsock2.h> and WSAStartup( MAKEWORD(2,2), &wsaData ); What could be the difference between debug and release that is causing this error???????. Thank you in advance for your answers
Avatar of gerhardschoeman
gerhardschoeman

ASKER

both WSAStartup and socket(AF_INET, SOCK_STREAM, 0 ); return success. The error is returned by connect(...);
This is your error code:

// MessageId: WSAEAFNOSUPPORT
//
// MessageText:
//
//  An address incompatible with the requested protocol was used.
//
#define WSAEAFNOSUPPORT                  10047L

Add debug information to Release build as described here:

http://www.codeguru.com/Cpp/V-S/debug/article.php/c1255/

run the program under debugger and try to understand what happens.
It seems that an address of the wrong family is used for a socket.
Please post the code for connect(..) and its parameters initialization.
Usually conncet(..) is called like:

sockaddr_in sai;

sai.sin_family = AF_INET;
sai.sin_addr.s_addr = htonl(dest_address);
sai.sin_port = htons(dest_port);
s = socket( AF_INET , SOCK_STREAM , 0 );
connect(s , (sockaddr *)&sai , sizeof(sai) ) );

nonubik:

I am calling connect as follows:

connect( m_socket, &addr, sizeof(addr) );

with sockaddr declared as:
 
sockaddr      addr;

How do you initialize the 'addr' variable?
AlexFM:

I have added the debug settings as described and have tried to debug the code. A funny things is happening though. I can trace through all the code until I get to my connect() statement which looks like:

int retval = connect(...);

For some reason the debugger does not recognise the retval symbol. It has no problem with any other local variables but it is unable to show retval. Could this be part of the problem in release. Is there something underlying causing a problem??
I have an addr_in struc that I populate as follows

sockaddr_in addr_in;

addr_in.sin_family=AF_INET;
addr_in.sin_port=htons( p );
etc...

and then

memcpy(&addr, &addr_in, sizeof(addr_in) );
Debugger does not recognise the retval symbol because of compiler optimizations - variable doesn't exist. Make something which prevents compiler to exclude retval value from final executable code. This means, use it by some way. For example, for debug output (OutputDebugString, printf etc.)
thanks Alex (stupid me) in which case retval is just SOCKET_ERROR and I still don't really know why the connect is failing. All the params for connect looks ok.

Although mentioning optimizations got me looking at that. And I have found the following:
If I compile release version but instead of using standard release build optimization settings I use customize and select "almost" anything except Full optimization, then it all works fine. As soon as I select full optimization then it doesn't work. What could this indicate?????
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>Add call to WSAGetLastError to get additional information.
I thought WSAGetLastError gets the 10047  error code.
yes WSAGetLastError does then get 10047
To both AlexFM and nonubik thank you very much for your quick responses. I have found the article by J M Newcomer on codeproject most useful. It is clear that there is something going on with the optimizations but as always there is no time and I have followed the advice in the article re. selectively disabling optimizations and this seems to have done the trick. Thanks to you both!!!!