Solved

winsock error returned from socket(AF_INET...

Posted on 1999-01-20
3
367 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
[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 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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

734 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