Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Simple Winsock app listen/connect

StugotsStugots Member Posts: 1
I'm having difficulty getting a basic loopback socket interface working under Vista. I'm getting the same error on both sides of the transaction. On the server side, the bind fails returning an error WSAEAFNOSUUPORT (10047) - Address family not supported by protocol family. The code is shown below. Can anyone suggest what the problem might be?

main()
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed
"));
nRetCode = 1;
}
else
{
WSADATA WsaDat;
SOCKET Socket;
SOCKET ConnectSock = SOCKET_ERROR;
SOCKADDR_IN SockAddr;

printf("Initialize WSA
");
if (WSAStartup(MAKEWORD(2, 2), &WsaDat) != 0)
{
printf("WSA Initialization failed.");
exit(-1);
}

printf("Create socket
");
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
printf("Socket creation failed.");
exit(-1);
}

SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;

printf("Bind socket to 127.0.0.1
");
SockAddr.sin_port = 0;

if (bind(Socket, (sockaddr *)&SockAddr, sizeof(SockAddr)) != 0)
{
printf("Attempt to bind failed - error: %d.
", WSAGetLastError());
exit(-1);
}
listen(Socket, 1);

printf("Wait for connection....
");
while (ConnectSock == SOCKET_ERROR)
{
ConnectSock = accept(Socket, NULL, NULL);
}
Socket = ConnectSock;
printf("Connection accepted
");
}
}

Sign In or Register to comment.