windows下IPv6通信

转载自:https://www.cnblogs.com/IntelligencePointer/p/14186691.htmlicon-default.png?t=LA92https://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;//初始化
	//初始化Socket
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	sockaddr_in6 RecvAddr;//服务器地址
	sockaddr_in6 SenderAddr = { AF_INET6, htons(6060) };//本地地址
	char RecvBuf[1024];//发送数据的缓冲区
	int BufLen = 1024;//缓冲区大小
	//创建接收数据报的socket
	SOCKET RecvSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
	bind(RecvSocket, (SOCKADDR*)&SenderAddr, sizeof(SenderAddr));
	int SenderAddrSize = sizeof(RecvAddr);
	printf("服务的接收:\n");
	while (1)
	{
		strcpy_s(RecvBuf, "hello");
		int l_nLen = recvfrom(RecvSocket, RecvBuf,BufLen, 0, (SOCKADDR*)&RecvAddr, &SenderAddrSize);
			if (l_nLen < 0)
			{
				perror("发送失败");
				exit(1);
			}
			printf("\nSend:");
			for (int i = 0; i < strlen(RecvBuf); i++)
			{
				printf("%02x ", (UCHAR)RecvBuf[i]);
			}
			int l_nReadLen = sendto(RecvSocket, RecvBuf, strlen(RecvBuf), 0, (SOCKADDR*)&RecvAddr, SenderAddrSize);
			printf("\nread:");
			for (int i = 0; i < l_nReadLen; i++)
			{
				printf("%02x ", RecvBuf[i]);
			}
			Sleep(1000);
	}
	//调用Recvfrom函数在绑定的socket上接收数据
	//关闭socket,结束接收数据
	closesocket(RecvSocket);
	//释放资源,退出
	WSACleanup();
	return 0;
}

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;//初始化
    //初始化Socket
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    sockaddr_in6 RecvAddr = { AF_INET6, htons(6060) };         //服务器地址
    inet_pton(AF_INET6, "fe80::4c09:7105:c377:4bc7", &RecvAddr.sin6_addr);       //服务器IP
    sockaddr_in6 SenderAddr = { AF_INET6, htons(7000) };       //本地地址
    char SendBuf[1024];           //发送数据的缓冲区
    int BufLen = 1024;            //缓冲区大小
    //创建Socket对象
    SOCKET SendSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    bind(SendSocket, (SOCKADDR*)&SenderAddr, sizeof(SenderAddr));
    int l_naddLen1 = sizeof(SenderAddr);
    printf("客户端发送:\n");
    while (1)
    {
        strcpy_s(SendBuf, "hello");
        int l_nLen = sendto(SendSocket, SendBuf, strlen(SendBuf), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
        if (l_nLen < 0)
        {
            perror("发送失败");
            exit(1);
        }
        printf("\nSend:");
        for (int i = 0; i < strlen(SendBuf); i++)
        {
            printf("%02x ", (UCHAR)SendBuf[i]);
        }
        int l_nReadLen = recvfrom(SendSocket, SendBuf, BufLen, 0, (struct sockaddr*)&SenderAddr, &l_naddLen1);
        printf("\nread:");
        for (int i = 0; i < l_nReadLen; i++)
        {
            printf("%02x ", SendBuf[i]);
        }
        Sleep(1000);
    }
    //发送完成,关闭Socket
    closesocket(SendSocket);
    WSACleanup();
    return 0;
​
}

 

你可能感兴趣的:(基础,windows,ipv6)