云计算百科
云计算领域专业知识百科平台

UDP服务器简单实现(C)

 

        UDP服务端搭建过程         1、加载库         2、创建socket套接字

        3、绑定地址IP         4、收发数据         5、关闭套接字         6、卸载库

一、加载库

        要用到Winsock2库里的WSAStartup函数,需要包含相应的头文件。函数的返回值是一个整型,通过查看VS官方帮助文档,我们判断加载库是否成功。代码如下:

//加载库
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) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf(\”WSAStartup failed with error: %d\\n\”, err);
return 1;
}

/* 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) !=

赞(0)
未经允许不得转载:网硕互联帮助中心 » UDP服务器简单实现(C)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!