Raw套接字上没有收到正确的数据

时间:2016-05-17 19:14:14

标签: linux sockets

我编写了最简单的Raw套接字示例,通过RAOL套接字将数据从一个appl发送到另一个appl,其中protcol no = 5(标准协议没有用完)。

以下是我的代码:

发送方

void sendRawData(char sendString[] )
{

    int sock;
    struct sockaddr_in server_addr;
    struct hostent *host;                   //hostent predefined structure use to store info about host        host = (struct hostent *) gethostbyname(server);//gethostbyname returns a pointer to hostent
    if ((sock = socket(AF_INET, SOCK_RAW, 5)) == -1)
    {
            perror("socket");
            exit(1);
    }

    //destination address structure
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(NOT_REQ); // NOT_REQ is 2000 but for SOCK_RAW sockets, it should be useless.
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);       //host->h_addr gives address of host
    bzero(&(server_addr.sin_zero),8);
    sendto(sock, sendString, strlen(sendString), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

    //sendto() function shall send a message through a connectionless-mode socket.
    printf("\nFORWARD REQUEST : '%s' has been forwarded to server\n",sendString);
    close(sock);
}

reciever

int main(int argc, char **argv){
    int sock = 0, len, addr_len;
    char rec_buff[5000];

    struct sockaddr_in address,
                    server_addr_udp,
                    client_addr;

    if ((sock = socket(AF_INET, SOCK_RAW, 5)) == -1)          //Creating a UDP Socket
    {

            perror("Socket");
            exit(1);
    }

    server_addr_udp.sin_family = AF_INET;
    server_addr_udp.sin_port = htons(2004); // again it should be useless
    server_addr_udp.sin_addr.s_addr = INADDR_ANY;
    bzero(&(server_addr_udp.sin_zero),8);
    addr_len = sizeof(struct sockaddr);
     if (bind(sock,(struct sockaddr *)&server_addr_udp, sizeof(struct sockaddr)) == -1)//Binding UDP socket
    {
        perror("Bind");
        exit(1);
    }

    len = recvfrom(sock, rec_buff,5000,0,(struct sockaddr *)&client_addr, &addr_len);
    printf("ip = %s, port = %d\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
    printf("%s\n", rec_buff);
    main(0, NULL);
    return 0;
}

我观察到的是接收方,我收到的数据不正确。 如果我将套接字类型更改为SOCK_DGRAM和protocol = IPPROTO_UDP,那么一切都很完美。 我的目的是使用自定义协议号(5),因为世界上的每个数据包都不需要是UDP / TCP数据包。

有人可以告诉我哪里出错了。

输出

ip = 127.0.0.1, port = 0
E

1 个答案:

答案 0 :(得分:1)

答案非常简单:)。收到原始IP数据报时,它始终包含IP标头。 (提示 - 检查recvfrom()的返回值并将其与sendto()的返回值进行比较将有助于您实现这一点,正如我在之前的评论中指出的那样。)

因此,您可以正确解析IP标头以获得有效负载,或者您可以假设IP标头大小始终为20个字节,并且只是快进到它。