多路径udp连接

时间:2017-04-16 16:42:46

标签: c++ sockets networking loopback tap

我编写了客户端和服务器代码。客户端和服务器都有两个接口和两个独立的IP地址。为了检查结果,我做了两个tap环回。

enter image description here

然后,我检查服务器和客户端。似乎客户端通过两个套接字发送数据包,但服务器只接收来自一个套接字的数据包。

您能否指导一下我的代码或loopbaks的问题是什么?

客户端

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <iostream>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <netinet/in.h>


#include <netdb.h>
#include <sys/socket.h>


#define BUFLEN 2048
#define MSGS 5  /* number of messages to send */

int main(void)
{
    struct sockaddr_in myaddr, remaddr;
    int fd, i, slen=sizeof(remaddr);
    char buf[BUFLEN];   /* message buffer */
    int recvlen;        /* # bytes in acknowledgement message */
    char *server = "192.168.10.10"; /* change this to use a different server */

    //////////////////////////////////
    struct sockaddr_in myaddr2, remaddr2;
        int fd2, slen2=sizeof(remaddr2);
        char buf2[BUFLEN];  /* message buffer */
        int recvlen2;       /* # bytes in acknowledgement message */
        char *server2 = "192.168.11.11";    /* change this to use a different server */


    //////////////////////////////



    /* create a socket */

    if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
        printf("socket created\n");
/////////////////////////////
    if ((fd2=socket(AF_INET, SOCK_DGRAM, 0))==-1)
            printf("socket2 created\n");

/////////////////////////////

    /* bind fd and fd2 */

    memset((char *)&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    //myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    inet_pton(AF_INET, "192.168.10.10", &myaddr.sin_addr.s_addr);
    myaddr.sin_port = htons(2000);

    /////////////////////////////

    memset((char *)&myaddr2, 0, sizeof(myaddr2));
        myaddr2.sin_family = AF_INET;
        //myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        inet_pton(AF_INET, "192.168.11.11", &myaddr2.sin_addr.s_addr);
        myaddr2.sin_port = htons(2001);
        /////////////////////////////

    if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind failed");
        return 0;
    }


    /////////////////////////////


    if (bind(fd2, (struct sockaddr *)&myaddr2, sizeof(myaddr2)) < 0) {
        perror("bind2 failed");
        return 0;
    }
    /////////////////////////////
//  printf("binding ip %s to port %d\n", myaddr.sin_addr.s_addr, myaddr.sin_port);

    /* now define remaddr, the address to whom we want to send messages */
    /* For convenience, the host address is expressed as a numeric IP address */
    /* that we will convert to a binary format via inet_aton */

    memset((char *) &remaddr, 0, sizeof(remaddr));
    remaddr.sin_family = AF_INET;
    remaddr.sin_port = htons(3000);

    if (inet_aton(server, &remaddr.sin_addr)==0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    /////////////////////////////
    memset((char *) &remaddr2, 0, sizeof(remaddr2));
        remaddr2.sin_family = AF_INET;
        remaddr2.sin_port = htons(4000);

        if (inet_aton(server2, &remaddr2.sin_addr)==0) {
            fprintf(stderr, "inet_aton2() failed\n");
            exit(1);
        }

    /////////////////////////////




    /* now let's send the messages */

    for (i=0; i < MSGS; i++) {
        printf("Sending packet %d to %s port %d\n", i, server, remaddr.sin_port);
        sprintf(buf, "This is packet %d", i);
        if (sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, slen)==-1) {
            perror("sendto");
            exit(1);
        }
        printf("socket %i SENT A PACKET \n", fd);



        printf("Sending packet %d to %s port %d\n", i, server2, remaddr2.sin_port);
        sprintf(buf2, "This is packet %d", i);
        if (sendto(fd2, buf2, strlen(buf2), 0, (struct sockaddr *)&remaddr2, slen2)==-1) {
            perror("sendto2");
            exit(1);
                }
        printf("socket %i SENT A PACKET \n", fd2);

    }
    close(fd);
    close(fd2);
    return 0;
}

服务器

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/select.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>


#define BUFSIZE 2048

int
main(int argc, char **argv)
{
    int n;
    fd_set readfds;
    struct timeval tv;


    struct sockaddr_in myaddr;  /* our address */
    struct sockaddr_in remaddr; /* remote address */
    socklen_t addrlen = sizeof(remaddr);        /* length of addresses */
    int recvlen;            /* # bytes received */
    int fd;             /* our socket */
    unsigned char buf[BUFSIZE]; /* receive buffer */

    /////////////////////
    struct sockaddr_in myaddr2; /* our address */
        struct sockaddr_in remaddr2;    /* remote address */
        socklen_t addrlen2 = sizeof(remaddr2);      /* length of addresses */
        int recvlen2;           /* # bytes received */
        int fd2;                /* our socket */
        unsigned char buf2[BUFSIZE];    /* receive buffer */
    /////////////////////


    /* create a UDP socket */

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("cannot create socket\n");
        return 0;
    }
    printf("socket %i is open \n", fd);

    ///////////////////////////////
    if ((fd2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
            perror("cannot create socket2\n");
            return 0;
        }
    printf("socket %i is open \n", fd2);
    ///////////////////////////////


    // clear the set ahead of time
    FD_ZERO(&readfds);



    // add our descriptors to the set
    FD_SET(fd, &readfds);
    FD_SET(fd2, &readfds);

    // since we got s2 second, it's the "greater", so we use that for
    // the n param in select()
    n = fd2 + 1;



    /* bind the socket to any valid IP address and a specific port */

    memset((char *)&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    //myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    inet_pton(AF_INET, "192.168.10.10", &myaddr.sin_addr.s_addr);
    myaddr.sin_port = htons(3000);

    if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind failed");
        return 0;
    }

    ///////////////////////////
    memset((char *)&myaddr2, 0, sizeof(myaddr2));
        myaddr2.sin_family = AF_INET;
        //myaddr2.sin_addr.s_addr = htonl(INADDR_ANY);
        inet_pton(AF_INET, "192.168.11.11", &myaddr2.sin_addr.s_addr);
        myaddr2.sin_port = htons(4000);

        if (bind(fd2, (struct sockaddr *)&myaddr2, sizeof(myaddr2)) < 0) {
            perror("bind2 failed");
            return 0;
        }

        // wait until either socket has data ready to be recv()d (timeout 10.5 secs)
        tv.tv_sec = 50;
        tv.tv_usec = 500000;
        for (;;) {
        int rv = select(n, &readfds, NULL, NULL, &tv);

        if (rv == -1) {
            perror("select"); // error occurred in select()
        } else if (rv == 0) {
            printf("Timeout occurred!  No data after 10.5 seconds.\n");
        } else {
            // one or both of the descriptors have data




            if (FD_ISSET(fd, &readfds)) {
                                printf("socket %i RECEIVED A PACKET \n", fd);
                                printf("waiting on port %d\n", myaddr.sin_port);
                                recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
                                if (recvlen > 0) {
                                    buf[recvlen] = 0;
                                    printf("received message: \"%s\" (%d bytes)\n", buf, recvlen);
                                }
                                else
                                    printf("uh oh - something went wrong!\n");
                                //sprintf(buf, "ack %d", msgcnt++);

                            }


            if (FD_ISSET(fd2, &readfds)) {
                                printf("socket %i RECEIVED A PACKET \n", fd2);
                                printf("waiting on port %d\n", myaddr2.sin_port);
                                recvlen2 = recvfrom(fd2, buf2, BUFSIZE, 0, (struct sockaddr *)&remaddr2, &addrlen2);
                                if (recvlen2 > 0) {
                                    buf[recvlen2] = 0;
                                    printf("received message2: \"%s\" (%d bytes)\n", buf2, recvlen2);
                                }
                                else
                                    printf("uh oh - something went wrong2!\n");
                                //sprintf(buf, "ack %d", msgcnt++);


                    }

            }



        }


}

结果显示在图片中:

enter image description here

===== 现在我检查了ns3模拟器网络中的程序。它工作正常。所以似乎问题在于环回。请指导我如何进行环回并通过localhost进行测试。

0 个答案:

没有答案