winsock编程学习1
WSA prefix
WSAStarup()
WSACleanup()
WSARecvEx()
WSAGetLastError()
How to Add Winsock link library WS2_32.lib to the Visual C++ project?
enter :Visual .NET doc 1 and Visual .NET doc 2 articles
Initializing Winsock
int WSAStarup( WORD wVersionRequested, LPWSADATA lpWSAData );
typedef struct WSAData
{
WORD wVersion;
WORD wHighVersion;
char szDescription[WSADESCRIPTION_LEN+1];
char szSystemStatus[WSASYS_STATUS_LEN+1];
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char FAR *lpVendorInfo;
} WSADATA,*LPWSADATA;
To find the maximum number of concurrent sockets,you should query the protocol information through WSAEnumProtocols()
int WSACleanup(void)
Error Checking and Handling
int WSAGetLastError(void)
code:
#include<stdio.h>
#include <winsock2.h>
#include <mswsock.h>
int main(void)
{
WSADATA wsaData;
int RetCode;
//initialize Winsock version 2.2
if((RetCode=WSAStartup(MAKEWORD(2,2),&wsaData))!=0)
{
printf( "WSAStartup failed with error %d\n",RetCode);
return 1;
}
else
{
printf("The Winsock dll found!\n");
printf("The current status is:%s.n",wsaData.szSystemStatus);
}
if (LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
//tell the user that we could not find a usable WinSock DLL
printf("The dll do not support the Winsock version %u.%u!\n",
LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
//when your application is finished call WSACLeanup
WSACleanup();
//and exit
return 0;
}
else
{
printf("The dll supports the Winsock version %u.%u!\n",LOBYTE(wsaData.wVersion),
HIBYTE(wsaData.wVersion));
printf("The highest version this dll can support:%u.%u\n",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion));
//When your application is finished call WSACleanup
if(WSACleanup()==SOCKET_ERROR)
printf("WSACleanup failed with error%d\n",WSAGetLastError());
//and exit
return 1;
}
}
编译时候,出现一些错误是因为写错,
程序执行结果:
Well,after completing this exercise ,you should be familiar with the steps to create an empty Win32 console application project.
Those steps will be repeated fot almost all the Winsock2 projects in this tutorial .
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………….
1 Tutorials on 'Advanced' Winsock 2 Network Programming
3 net-gold
4 VC中Socket初始化以及释放,WSAStartup函数,WSACleanup函数
5 VC++网络编程
……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………..