ipv6实现udp编程示例
开发测试环境:WIN7SP1、VS2005
UdpServer.cpp
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
using namespace std;
cout<<"Server Start..."<<endl;
WORD wVersion = 0;
WSADATA wsaData;
memset(&wsaData, 0, sizeof(wsaData));
wVersion = MAKEWORD(2, 2);
if (0 != WSAStartup(wVersion, &wsaData))
{
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
return -1;
}
struct addrinfo hints;
struct addrinfo* res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
if (0 != getaddrinfo("192.168.0.19", "6677", &hints, &res))
{
WSACleanup();
return -1;
}
SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sk, res->ai_addr, res->ai_addrlen);
char recvBuf[1024] = {'\0'};
int lenRecv = 0;
struct sockaddr_storage storage;
int len = sizeof(storage);
memset(&storage, 0, len);
lenRecv = recvfrom(sk, recvBuf, 1024, 0, (struct sockaddr*)&storage, &len);
cout<<"Recv From Client:"<<recvBuf<<" Len:"<<lenRecv<<endl;
sendto(sk, recvBuf, lenRecv, 0, (struct sockaddr*)&storage, len);
WSACleanup();
closesocket(sk);
return 0;
}
UdpClient.cpp
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
using namespace std;
cout<<"Client Start..."<<endl;
WORD wVersion = 0;
WSADATA wsaData;
memset(&wsaData, 0, sizeof(wsaData));
wVersion = MAKEWORD(2, 2);
if (0 != WSAStartup(wVersion, &wsaData))
{
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
return -1;
}
struct addrinfo hints;
struct addrinfo* res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_CANONNAME;
if (0 != getaddrinfo("192.168.0.19", "6677", &hints, &res))
{
WSACleanup();
return -1;
}
SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sk, res->ai_addr, res->ai_addrlen);
char recvBuf[1024] = {'\0'};
char sendBuf[1024] = {"abcd"};
int len = res->ai_addrlen;
int lenRecv = 0;
sendto(sk, sendBuf, strlen(sendBuf), 0, res->ai_addr, len);
lenRecv = recvfrom(sk, recvBuf, 1024, 0, res->ai_addr, &(len));
cout<<"Recv From Server:"<<recvBuf<<" Len:"<<lenRecv<<endl;
WSACleanup();
closesocket(sk);
return 0;
}
上一篇:c语言指针之二级指针示例
栏 目:C语言
下一篇:c++回调之利用函数指针示例
本文标题:ipv6实现udp编程示例
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3709.html
您可能感兴趣的文章
- 01-10数据结构课程设计-用栈实现表达式求值的方法详解
- 01-10使用OpenGL实现3D立体显示的程序代码
- 01-10求斐波那契(Fibonacci)数列通项的七种实现方法
- 01-10C语言 解决不用+、-、&#215;、&#247;数字运算符做加法
- 01-10使用C++实现全排列算法的方法详解
- 01-10用C++实现DBSCAN聚类算法
- 01-10深入全排列算法及其实现方法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10用C语言实现单链表的各种操作(一)
- 01-10用C语言实现单链表的各种操作(二)
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10SublimeText编译C开发环境设置
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子