采用的是VC2008,控制台应用程序,使用UDP编写。
1、服务端代码
//UDPServer.cpp
#include <WinSock2.h>
#include <stdio.h>
#define SERVERPORT 6000 //服务端口号
#pragma comment(lib, "WS2_32.lib");
int main(int argc, char *argv[])
{
//加载套接字库
WORD 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 -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 ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return -1;
}
/* The WinSock DLL is acceptable. Proceed. */
//创建套接字
SOCKET sockServer = socket(AF_INET, SOCK_DGRAM, 0);
if (INVALID_SOCKET == sockServer)
{
printf("socket() called failed! The error code is: %d
", WSAGetLastError());
return -1;
}
else
{
printf("socket() called succesful!
");
}
//服务器端
SOCKADDR_IN addrServer;
addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(SERVERPORT);
//绑定套接字
err = bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
if (SOCKET_ERROR == err)
{
printf("bind() called failed! The error code is: %d
", WSAGetLastError());
return -1;
}
else
{
printf("bind() called successful!
");
}
//等待并接收数据
SOCKADDR_IN addrClient;//用于接收发送端的地址信息
int len = sizeof(SOCKADDR);
char recvBuf[100];
recvfrom(sockServer, recvBuf, 100, 0, (SOCKADDR*)&addrClient, &len);
//打印接收到的数据
printf("receive data:%s from client [%s,%d]", recvBuf, inet_ntoa(addrClient.sin_addr), addrClient.sin_port);
//关闭套接字
closesocket(sockServer);
//终止套接字库的使用
WSACleanup();
return 0;
}
2、客户端代码
//UDPClient.cpp
#include <WinSock2.h>
#include <stdio.h>
#define SERVERPORT 6000 //服务端口号
#pragma comment(lib, "WS2_32.lib");
int main(int argc, char *argv[])
{
//加载套接字库
WORD 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 -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 ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return -1;
}
//创建套接字
SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);
if (INVALID_SOCKET == sockClient)
{
printf("socket() called failed! The error code is: %d
", WSAGetLastError());
return -1;
}
else
{
printf("socket() called succesful!
");
}
SOCKADDR_IN addrServer;
addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(SERVERPORT);
//发送数据
err = sendto(sockClient, "Hello", strlen("Hello")+1, 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
if (SOCKET_ERROR == err)
{
printf("sendto() called failed! The error code is: %s
", WSAGetLastError());
return -1;
}
else
{
printf("sendto() called successful!
");
}
//关闭套接字
closesocket(sockClient);
//终止套接字库的使用
WSACleanup();
return 0;
}
先启动服务端UDPServer程序,再启动客户端UDPClient程序,运行结果如下:
服务端UDPServer
客户端UDPClient
此时服务端UDPServer的结果会发生变化,会受到客户端发送过来的数据,如下图所示:
参考资料:
1、《VC++深入详解》 第14章网络编程 ,孙鑫主编
2、MSDN帮助文档