先看几个结构体定义
typedef struct WSAData {
WORD wVersion;//版本号 通过MAKEWORD(2,2)返回该值 高位字节存储副版本号 第位字节存储主版本号
WORD wHighVersion;//DLL可以支持的windows Socket规范的最高版本
char szDescription[WSADESCRIPTION_LEN+1];//返回字符串 最多256字符
char szSystemStatus[WSASYS_STATUS_LEN+1];//返回配置信息
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char FAR* lpVendorInfo; } WSADATA,
*LPWSADATA;
1 #include <winsock2.h> 2 #pragma comment(lib, "ws2_32.lib") 3 #include <stdio.h> 4 int main(void) 5 { 6 WSADATA wsaData; 7 8 9 10 // Initialize Winsock version 2.2 11 //成功返回0 12 if (( WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)//调用WSAStartup初始化 13 { 14 // NOTE: Since Winsock failed to load we cannot use 15 // WSAGetLastError to determine the specific error for 16 // why it failed. Instead we can rely on the return 17 // status of WSAStartup. 18 19 printf("WSAStartup无法初始化"); 20 return 0; 21 } 22 23 // Setup Winsock communication code here 24 //显示wsadata的数据 25 printf("version:%d.%d ", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion)); 26 printf("High version: %d.%d ", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion)); 27 printf("Desription:%s ", wsaData.szDescription); 28 printf("System Status:%s", wsaData.szSystemStatus); 29 // When your application is finished call WSACleanup 30 if (WSACleanup() == SOCKET_ERROR) 31 { 32 printf("WSACleanup出错%d", WSAGetLastError());//返回错误代码 33 printf(" "); 34 system("pause"); 35 36 } 37 getchar(); 38 39 40 }
注意:引入静态库哦