Solved

winsock error returned from socket(AF_INET...

Posted on 1999-01-20
3
364 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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.

863 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

18 Experts available now in Live!

Get 1:1 Help Now