首页 > 代码库 > C++ Socket UDP "Hello World!"
C++ Socket UDP "Hello World!"
这是C++ SOCKET网络程序中的C/S结构之UDP "Hello World !",共两个控制台工程:
[cpp] view plain copy print?
- //////////////////////////////////////////////////////////////////////////
- // UDPServer.cpp
- #include <stdio.h>
- #include <WINSOCK2.H>
- #pragma comment(lib,"WS2_32.lib")
- #define BUF_SIZE 64
- int main(void)
- {
- WSADATA wsd;
- SOCKET s;
- int nRet;
- // 初始化套接字动态库
- if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
- {
- printf("WSAStartup failed !/n");
- return 1;
- }
- // 创建套接字
- s = socket(AF_INET,SOCK_DGRAM,0);
- if(s == INVALID_SOCKET)
- {
- printf("socket() failed ,Error Code:%d/n",WSAGetLastError());
- WSACleanup();
- return 1;
- }
- SOCKET socketSrv = socket(AF_INET,SOCK_DGRAM,0);
- SOCKADDR_IN addrSrv;
- SOCKADDR_IN addrClient;
- char buf[BUF_SIZE];
- int len = sizeof(SOCKADDR);
- // 设置服务器地址
- ZeroMemory(buf,BUF_SIZE);
- addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
- addrSrv.sin_family = AF_INET;
- addrSrv.sin_port = htons(5000);
- // 绑定套接字
- nRet = bind(socketSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
- if(SOCKET_ERROR == nRet)
- {
- printf("bind failed !/n");
- closesocket(s);
- WSACleanup();
- return -1;
- }
- // 从客户端接收数据
- nRet = recvfrom(socketSrv,buf,BUF_SIZE,0,(SOCKADDR*)&addrClient,&len);
- if(SOCKET_ERROR == nRet)
- {
- printf("recvfrom failed !/n");
- closesocket(s);
- WSACleanup();
- return -1;
- }
- // 打印来自客户端发送来的数据
- printf("Recv From Client:%s/n",buf);
- // 向客户端发送数据
- sendto(socketSrv,"UDP Hello World !",sizeof("UDP Hello World !"),0,(SOCKADDR*)&addrClient,len);
- closesocket(s);
- WSACleanup();
- return 0;
- }
[cpp] view plain copy print?
- //////////////////////////////////////////////////////////////////////////
- // UDPClient.cpp
- #include <stdio.h>
- #include <WINSOCK2.H>
- #pragma comment(lib,"WS2_32.lib")
- #define BUF_SIZE 64
- int main(void)
- {
- WSADATA wsd;
- SOCKET s;
- // 初始化套接字动态库
- if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
- {
- printf("WSAStartup failed !/n");
- return 1;
- }
- // 创建套接字
- s = socket(AF_INET,SOCK_DGRAM,0);
- if(s == INVALID_SOCKET)
- {
- printf("socket() failed, Error Code:%d/n",WSAGetLastError());
- WSACleanup();
- return 1;
- }
- char buf[BUF_SIZE]; // 接受数据
- SOCKADDR_IN servAddr; // 服务器套接字地址
- SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0);
- int nRet;
- ZeroMemory(buf,BUF_SIZE);
- strcpy(buf,"UDP Hello World !");
- // 设置服务器地址
- servAddr.sin_family = AF_INET;
- servAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.254");
- servAddr.sin_port = htons(5000);
- // 向服务器发送数据
- int nServAddLen = sizeof(servAddr);
- if(sendto(sockClient,buf,BUF_SIZE,0,(sockaddr *)&servAddr,nServAddLen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed:%d/n",WSAGetLastError());
- closesocket(s);
- WSACleanup();
- return 1;
- }
- nRet = recvfrom(sockClient,buf,BUF_SIZE,0,(sockaddr *)&servAddr,&nServAddLen);
- if(SOCKET_ERROR == nRet)
- {
- printf("recvfrom failed !/n");
- closesocket(s);
- WSACleanup();
- return -1;
- }
- // 打印来自服务端发送来的数据
- printf("Recv From Server:%s/n",buf);
- closesocket(s);
- WSACleanup();
- return 0;
- }
程序运行效果:
源代码下载地址:
http://www.rayfile.com/files/733c832e-a2d8-11de-a838-0014221b798a/
http://blog.csdn.net/wangningyu/article/details/4560835
C++ Socket UDP "Hello World!"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。