I am attempting to code a simple peer-to-peer local network for a DirectX gaming environment. For all of my DirectX coding, I have been working with Windows API's no problem. However, while starting to code the basic Winsock code for the (very basic) local network, I seem to have swerved into MFC programming.
As soon as I began coding for Socket initialization, Visual Studio gave me a compiler error saying I needed to add #define _AFXDLL, which I did, but then I got the compile error:
c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afxv_w32.h(16): fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
I have done very little programming with MFC - since DirectX deals directly with the API's, so I would like to ask how I can sort out Winsock programming in a non-MFC environment?
Also, since I am new to Microsoft Q&A forums, and I have not been able to find any guidance in the Q&A FAQ's for providing code snippets in questions, I am attaching a code snippet,
enter code here
// File: DOP Comm Utility.cpp
include "stdafx.h"
define _AFXDLL
ifndef FD_SETSIZE
define FD_SETSIZE 1024
endif
include <winsock2.h>
include <windows.h>
include <stdio.h>
include <math.h>
include <iostream>
include <time.h>
include <stdlib.h>
include <sys/types.h>
include <sys/timeb.h>
include <string.h>
using namespace std;
include <ws2tcpip.h>
define _CRT_SECURE_NO_WARNINGS 1
define _CRT_NONSTDC_NO_WARNINGS
include <iphlpapi.h>
include <afxsock.h>
// Need to link with Ws2_32.lib
define ISVALIDSOCKET(s) (s != INVALID_SOCKET)
define CLOSESOCKET(S) closesocket(s)
define GETSOCKETERRNO() (WSAGetLastError())
define TIMEOUT 3.0
int _tmain(int argc, _TCHAR* argv[])
{
int j, k;
int AdapterFlag = 0;
char Term;
char AdapterFind[200];
char AddressFind[60];
// Winsock WSAStartup & Initialization ----------------------------------------------------------------------------------------------
WORD wVersionRequested;
WSADATA wsaData;
int err;
//Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
cin.clear();
cin >> Term;
return 0;
}
//Confirm that the WinSock DLL supports 2.2.
//Note that if the DLL supports versions greater than 2.2 in addition to 2.2, it will still return
//2.2 in wVersion since that is the version we requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
cin.clear();
cin >> Term;
return 0;
}
else
printf("The Winsock 2.2 dll has been initialized - WSAStartup completed\n");
PIP_ADAPTER_ADDRESSES OwnAdapters;
PIP_ADAPTER_ADDRESSES OurAdapter;
DWORD Asize = 20000;
do {
OwnAdapters = (PIP_ADAPTER_ADDRESSES) malloc (Asize);
if( !OwnAdapters )
{
printf( "could not allocate memory for Adapters \n" );
WSACleanup();
cin.clear();
cin >> Term;
return 0;
}
int R = GetAdaptersAddresses( AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, 0, OwnAdapters, &Asize );
if( R == ERROR_BUFFER_OVERFLOW )
{
printf("GetAdaptersAddresses needs larger buffer.\n" );
free( OwnAdapters );
}
else
if( R == ERROR_SUCCESS )
{
break;
}
else
{
printf( "Error from GetAdaptersAddresses call: %d\n", R );
WSACleanup();
free( OwnAdapters );
cin.clear();
cin >> Term;
return 0;
}
} while (!OwnAdapters);
// Loop through the OwnAdapters linked list and print information for each adapter and address
PIP_ADAPTER_ADDRESSES Adapters = OwnAdapters;
char AP[100];
while (Adapters)
{
printf( "\nAdapter mame: %S", Adapters->FriendlyName );
printf( "\n" );
PIP_ADAPTER_UNICAST_ADDRESS Address = Adapters->FirstUnicastAddress;
while( Address )
{
printf( "\t%s", Address->Address.lpSockaddr->sa_family == AF_INET ? "IPv4" : "IPv6");
//char AP[100];
getnameinfo( Address->Address.lpSockaddr, Address->Address.iSockaddrLength, AP, sizeof(AP), 0, 0, NI_NUMERICHOST );
printf( "\t%s\n", AP );
if( ( AdapterFlag == 1 ) && ( Address->Address.lpSockaddr->sa_family == AF_INET ) )
{
for( j = 0; j < sizeof(AP); j++ )
{
OurAddress[j] = AP[j];
}
AdapterFlag = -1;
printf( "\t", "Our Address: " );
printf( "\t%s\n", OurAddress);
}
Address = Address->Next;
}
Adapters = Adapters->Next;
}
printf("\n\nFD_SETSIZE is %d.\n", FD_SETSIZE);
SOCKET MainClient;
bool SocketErr;
struct sockaddr_in MainData;
char localIP[50] = "155.33.5.100";
MainClient = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
if( !ISVALIDSOCKET( MainClient ) )
{
printf( "\nThe MainClient Socket did not validate. \n" );
cin.clear();
cin >> Term;
free( OwnAdapters );
WSACleanup();
return 0;
}
MainData.sin_family = AF_INET;
MainData.sin_addr.s_addr = inet_addr(localIP);
MainData.sin_port = htons(4400);
if( bind( MainClient, (SOCKADDR *) &MainData, sizeof( MainData) ) == SOCKET_ERROR )
{
printf( "\nThe MainClient Socket did not successfuly BIND. \n" );
cin.clear();
cin >> Term;
free( OwnAdapters );
WSACleanup();
return 0;
}
cin.clear();
cin >> Term;
free( OwnAdapters );
WSACleanup();
return 0;
}