如果创建控制台程序的时候选择支持MFC就没问题 不过一般还是直接用API,其实AfxSocketInit基本跟下面代码差不多,不过用的是1.1WORD wVersionRequested; WSADATA wsaData; int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; }
/* 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 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; }
如果你使用的是vc6.0,通过下列步骤是你的控制台程序支持MFC 1. 选择Project-》Setting 2. 弹出Peoject Setting对话框,选择General。 3. 在Microsoft Foundation Classes选择Use MFC in a Shared DLL或Use MFC in a Static Library
不过一般还是直接用API,其实AfxSocketInit基本跟下面代码差不多,不过用的是1.1WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* 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 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
1. 选择Project-》Setting
2. 弹出Peoject Setting对话框,选择General。
3. 在Microsoft Foundation Classes选择Use MFC in a Shared DLL或Use MFC in a Static Library