作者:mobiledu2502907897 | 来源:互联网 | 2023-07-31 09:41
转载自:https:www.cnblogs.comIntelligencePointerp14186691.htmlhttps:www.cnblogs.comInte
转载自:https://www.cnblogs.com/IntelligencePointer/p/14186691.htmlhttps://www.cnblogs.com/IntelligencePointer/p/14186691.html
Server
#include
#include
#include
#define HELLO_PORT 7905
#define HELLO_GROUP "127.0.0.1"
#pragma comment(lib,"ws2_32.lib")
//using namespace std;
int main()
{WSADATA wsaData;//初始化//初始化SocketWSAStartup(MAKEWORD(2, 2), &wsaData);sockaddr_in6 RecvAddr;//服务器地址sockaddr_in6 SenderAddr &#61; { AF_INET6, htons(6060) };//本地地址char RecvBuf[1024];//发送数据的缓冲区int BufLen &#61; 1024;//缓冲区大小//创建接收数据报的socketSOCKET RecvSocket &#61; socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);bind(RecvSocket, (SOCKADDR*)&SenderAddr, sizeof(SenderAddr));int SenderAddrSize &#61; sizeof(RecvAddr);printf("服务的接收&#xff1a;\n");while (1){strcpy_s(RecvBuf, "hello");int l_nLen &#61; recvfrom(RecvSocket, RecvBuf,BufLen, 0, (SOCKADDR*)&RecvAddr, &SenderAddrSize);if (l_nLen <0){perror("发送失败");exit(1);}printf("\nSend&#xff1a;");for (int i &#61; 0; i }
Client
include
#include
#include
#define HELLO_PORT 7905
#define HELLO_GROUP "127.0.0.1"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{WSADATA wsaData;//初始化//初始化SocketWSAStartup(MAKEWORD(2, 2), &wsaData);sockaddr_in6 RecvAddr &#61; { AF_INET6, htons(6060) }; //服务器地址inet_pton(AF_INET6, "fe80::4c09:7105:c377:4bc7", &RecvAddr.sin6_addr); //服务器IPsockaddr_in6 SenderAddr &#61; { AF_INET6, htons(7000) }; //本地地址char SendBuf[1024]; //发送数据的缓冲区int BufLen &#61; 1024; //缓冲区大小//创建Socket对象SOCKET SendSocket &#61; socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);bind(SendSocket, (SOCKADDR*)&SenderAddr, sizeof(SenderAddr));int l_naddLen1 &#61; sizeof(SenderAddr);printf("客户端发送&#xff1a;\n");while (1){strcpy_s(SendBuf, "hello");int l_nLen &#61; sendto(SendSocket, SendBuf, strlen(SendBuf), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));if (l_nLen <0){perror("发送失败");exit(1);}printf("\nSend&#xff1a;");for (int i &#61; 0; i
}