天天看點

Linux IPV6 socket 通訊

網上查了半天,好多都是重複的,以下是我根據網上的代碼,自己調試後解決的

以下内容,我是在虛拟機上 嵌入式 Linux上測試通過的,伺服器端程式一大把,随便百度下就可以用了

用戶端程式,我研究了,代碼如下,注意 IPV6 位址,接口裝置,端口需要修改

網上好多例子都沒有提到 這個接口裝置

接口裝置你就是運作用戶端程式的網卡

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <errno.h>

#define MAXBUF 1024
int main(int argc, char *argv[])
{
    int iFd;
    char arrcBuffer[MAXBUF + 1];
    char *pcIPV6 = "fe80::dd56:57f3:2b88:54cb";
    char *pcDev = "eth1";
    int iPort = 5000;

    printf("PAR: IPV6=%s DEV=%s PORT=%d\r\n Len = %d\r\n",pcIPV6, pcDev, iPort);
    //if (argc < 3)
    //{
    //	printf("usage: %s  netdev  ipv6_addr \n", argv[0]);
    //	return 1;
    //}

    iFd = socket(AF_INET6, SOCK_STREAM, 0);
    if (iFd < 0)
    {
        perror("socket");
        return 1;
    }

    struct ifreq tReq;
    strcpy(tReq.ifr_name, pcDev);
    if (ioctl(iFd, SIOCGIFINDEX, &tReq) < 0)
    {
        perror("SIOCGIFINDEX");
        return 2;
    }

    struct sockaddr_in6 tDestIPV6 = {AF_INET6, htons(iPort), 0};
    inet_pton(AF_INET6, pcIPV6, &tDestIPV6.sin6_addr);
    tDestIPV6.sin6_scope_id = tReq.ifr_ifindex;

    if (connect(iFd, (struct sockaddr*)&tDestIPV6, sizeof(tDestIPV6)) < 0)
    {
        perror("connect");
        return 3;
    }
    printf("connect OK IPV6=%s DEV=%s PORT=%d\r\n Len = %d\r\n",pcIPV6, pcDev, iPort);

    /* 接收伺服器來的消息 */
    int iLen = recv(iFd, arrcBuffer, MAXBUF, 0);
    if (iLen > 0)
        printf("Rcv Msg=%s \r\n Len = %d\r\n",arrcBuffer, iLen);
    else
        printf("Rcv ERR. CODE=%d\r\n MSG = %d\r\n",errno, strerror(errno));

    bzero(arrcBuffer, MAXBUF + 1);
    strcpy(arrcBuffer, "client ipv6 123adf\r\n");
    /* 發消息給伺服器 */
    iLen = send(iFd, arrcBuffer, strlen(arrcBuffer), 0);
    if (iLen < 0)
        printf("Snd ERR. CODE=%d  MSG = %d\r\n",errno, strerror(errno));
    else
        printf("Send Msg=%s Len = %d\r\n",arrcBuffer, iLen);

    close(iFd);
    return 0;

}
           

繼續閱讀