?
Solved

winsock error returned from socket(AF_INET...

Posted on 1999-01-20
3
Medium Priority
?
372 Views
Last Modified: 2006-11-17
I am trying to write a simple DLL that opens a socket
to a Unix machine.  The routine keeps returning
10047 (notsupported) from the connect call.  Actually
I think it's coming from the socket call. TCP is up
on the LAN and all my other apps work great.  Can
anyone see my problem?

__declspec (dllexport) void _stdcall
woe_init (long *out1, long *out2)
{
      SOCKET H = socket(PF_INET,SOCK_STREAM,0);
      struct sockaddr_in sock;
      sock.sin_addr.s_addr = inet_addr("127.0.0.1");
      sock.sin_port=htons(9024);

      int u=0,y=0;
      y=connect(H,(LPSOCKADDR)&sock,sizeof(sock));
      *out1 = WSAGetLastError();
      l=closesocket(H);
      *out2 = WSAGetLastError();
      return;
}
0
Comment
Question by:timothynelson
3 Comments
 
LVL 4

Accepted Solution

by:
tflai earned 400 total points
ID: 1184632
I believe you forgot this line:
sock.sin_family = PF_INET;

0
 

Author Comment

by:timothynelson
ID: 1184633
Obvious... if I only checked my Unix code!

Do I also need to worry about WSAStartUp???

Thanks for the quick reply, I'll be giving you the points.

0
 
LVL 4

Expert Comment

by:pellep
ID: 1184634
Yes, you should call WSAStartUp. It initializes the winsock dll.
if you do not call it, the socket-functions is likely to behave
unpredictably. Furthermore WSAGetLastError() should defenitly not work.
//example
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );//which version do you need
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
    /* Tell the user that we couldn't find a usable */
    /* WinSock DLL.                                  */    return;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE(wsaData.wVersion ) != 0 ) //which version is available
{

WSACleanup( );
    return;
}
struct WSAData {
WORD wVersion;    
WORD wHighVersion;
char szDescription[WSADESCRIPTION_LEN+1];
char szSystemStatus[WSASYSSTATUS_LEN+1];
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char FAR *lpVendorInfo;};
0

Featured Post

Independent Software Vendors: 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!

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

850 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