码迷,mamicode.com
首页 > 其他好文 > 详细

Centos7 多网卡抓包可以抓到UDP但程序recvfrom不到

时间:2018-05-23 11:51:21      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:接收   发送消息   防火   程序   port   bind   lte   2.x   通信   

问题:

     Centos7多网卡,抓包时发现某网卡上有UDP包,但是用程序recvfrom无法接收到消息。

解决步骤:

1.确认防火墙是否关闭;

已关闭

2.确认网卡是否开启过滤:cat /proc/sys/net/ipv4/conf/ethxxx/rp_filter    (ethxxx是网卡名称)

结果:已关闭(当然此配置项只影响组播,这是病急乱投医)

3.随便找一个服务端和客户端的代码,测试是否能正常进行udp通信;  

结果发现完全正常。

server.cpp

技术分享图片
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        printf("Usage: %s port\n", argv[0]);
        exit(1);
    }
    printf("Welcome! This is a UDP server, I can only received message from client and reply with same message\n");
    
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[1]));
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    int sock;
    if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind");
        exit(1);
    }
    char buff[409600];
    struct sockaddr_in clientAddr;
    int n;
    unsigned int len = sizeof(clientAddr);
    while (1)
    {
        n = recvfrom(sock, buff, 409600, 0, (struct sockaddr*)&clientAddr, &len);
        if (n>0)
        {
            buff[n] = 0;
            printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff);
            n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
            if (n < 0)
            {
                perror("sendto");
                break;
            }
        }
        else
        {
            perror("recv");
            break;
        }
    }
    return 0;
}
View Code

client.cpp

技术分享图片
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>


int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("Usage: %s ip port", argv[0]);
        exit(1);
    }
    printf("This is a UDP client\n");
    struct sockaddr_in addr;
    int sock;

    if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
    {
        perror("socket");
        exit(1);
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (addr.sin_addr.s_addr == INADDR_NONE)
    {
        printf("Incorrect ip address!");
        close(sock);
        exit(1);
    }

    char buff[512]="aaaaaaaaaaaaaaa";
    unsigned int len = sizeof(addr);
    while (1)
    {
        gets(buff);
        int n;
        n = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr));
        if (n < 0)
        {
            perror("sendto");
            close(sock);
            break;
        }
        n = recvfrom(sock, buff, 512, 0, (struct sockaddr *)&addr, &len);
        if (n>0)
        {
            buff[n] = 0;
            printf("received:");
            puts(buff);
        }
        else if (n==0)
        {
            printf("server closed\n");
            close(sock);
            break;
        }
        else if (n == -1)
        {
            perror("recvfrom");
            close(sock);
            break;
        }
    }
    
    return 0;
}
View Code

4.再次抓包查看

此次抓包发现,双网口的Android设备用两根独立的网线连到服务器不同的网卡,设置两个网段:192.168.1.xx和192.168.2.xx,其中192.168.1.xx是Android设备的默认路由网段。从Android设备发送UDP消息到192.168.2.xx网段,结果服务器端对应的网口抓包显示的源地址为192.168.1.xx,将UDP发送消息改为192.168.1.xx后,在服务器的相应网卡地址接收,问题解决。

问题原因:

Android设备的多网卡不共用同一路由表,因此造成路由失效;当路由生效时,又把默认的网卡地址组装到UDP中,造成此问题。

Centos7 多网卡抓包可以抓到UDP但程序recvfrom不到

标签:接收   发送消息   防火   程序   port   bind   lte   2.x   通信   

原文地址:https://www.cnblogs.com/bugutian/p/9075936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!