欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

ipv6实现udp编程示例

时间:2021-07-19 07:59:52|栏目:C代码|点击:

开发测试环境: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;
}

上一篇:opencv利用霍夫变换检测直线进行图片校正

栏    目:C代码

下一篇:使用devenv在命令行中编译项目的方法

本文标题:ipv6实现udp编程示例

本文地址:http://www.codeinn.net/misctech/159074.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有