简体   繁体   中英

In the simple python program I lost udp packets

Operating System: Windows 8

IDE Eclise Juno + Aptana Plugin for python toolchain

Language Python 3.3

I try to send two udp packets from one computer to other. This is source code

from socket import *
import time

port=5000
ip_addr='192.168.4.122'
my_ip='192.168.4.6'

sock = socket(AF_INET,SOCK_DGRAM)
sock.bind((my_ip,port))

data=bytes([0xc9,0x01,0,0,0xc8,port & 0x00ff,(port >> 8) & 0x00ff,0,1,1,0,1,0,ord('F'),ord('P')])
sock.sendto(data,(ip_addr,port))
data=bytes([0xc9,0x01,0,0,0xc8,port & 0x00ff,(port >> 8) & 0x00ff,0,1,1,0,1,0,ord('k'),ord('f'),ord('?')])
sock.sendto(data,(ip_addr,port))

time.sleep(3)

print("end of the programm");

But if I run wireshark (in the both computers) I see only first packet. Where I lost second packet??? What's I do wrong ??? In my switch being connected only two computers. I add output of the result of the sendto calls. These results are possitive.

Try using socket.send instead of socket.sendto. In addition to that, UDP is a "send and forget" protocol. If you want to be sure if your data is arriving you need to use TCP.

@Ayy如果我在“ sendto”之间添加time.sleep(1),则所有数据包均已成功发送。

Specially for @ayy I continue investigation. I write this programm in C language.

   int main(void) {
        struct sockaddr_in udp_addr;
        struct sockaddr_in remote_addr;
        int on=1;
        int snd_rcv_buf = 131072;
        int sock_fd;
        socklen_t udp_addr_len;

        char data[] = { 0xc9,0x01,0,0,0xc8,0x88,0x13,0,1,1,0,1,0,0x46,0x50 };
        char data1[] = { 0xc9,0x01,0,0,0xc8,0x88,0x13,0,1,1,0,1,0,0x6b,0x66,0x3f };

        sock_fd = socket(PF_INET, SOCK_DGRAM, 0);

        setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
        setsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &snd_rcv_buf, sizeof(snd_rcv_buf));
        setsockopt(sock_fd, SOL_SOCKET, SO_SNDBUF, &snd_rcv_buf, sizeof(snd_rcv_buf));

        memset(&udp_addr, 0, sizeof(udp_addr));
        udp_addr.sin_family = AF_INET;
        udp_addr.sin_port = htons(5000);
        udp_addr_len = sizeof(udp_addr);

        if (inet_aton("192.168.4.66", &udp_addr.sin_addr) == 0)
        {
            printf( (char *)"Address translate error\n");
            return -1;
        }

        if (bind(sock_fd, (struct sockaddr*)&udp_addr, udp_addr_len)!=0)
        {
            printf((char *)"Error bind udp-socket\n");
            return -1;
        }

        memset(&remote_addr, 0, sizeof(remote_addr));
        remote_addr.sin_family = AF_INET;
        if (inet_aton("192.168.4.122", &remote_addr.sin_addr)==0)
            printf((char *)"remote address translate error");
        remote_addr.sin_port = htons(5000);


        int res = sendto(sock_fd, data, /*len*/15, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
        printf("%d ",res);
        res = sendto(sock_fd, data1, /*len*/16, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
        printf("%d ",res);

        puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
        return EXIT_SUCCESS;
}

And situation have been repeated. But..................Atention!!!!!!! If I invoke this Code in the RedHat 6.5 virtual machine (Host Machine is the Windows 8) that all packet have been sent successfully. Wauuuuuu. All work correctly. It turns out that problem in the OS Windows 8???????????

I tried your code locally it works

[root@localhost#]# tcpdump -i lo src 127.1.1.1 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes 19:32:10.905035 IP 127.1.1.1.commplex-main > 127.1.1.1.commplex-main: UDP, length 54 19:32:10.905069 IP 127.1.1.1.commplex-main > 127.1.1.1.commplex-main: UDP, length 60

@HSEM Please , see this excerpt of code.

int res = sendto(sock_fd, data, /*len*/15, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
printf("%d ",res);
res = sendto(sock_fd, data1, /*len*/16, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
printf("%d ",res);

If I comment printf then.

   int res = sendto(sock_fd, data, /*len*/15, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
//    printf("%d ",res);
    res = sendto(sock_fd, data1, /*len*/16, 0, (struct sockaddr*)&remote_addr, udp_addr_len);
//    printf("%d ",res);

I start to lost packets . It turns out that the problem in delay between "sendto" calls. If it exists than all work perfectly. If delay is absent then I start to lost packets. Similar situation is in the python code https://stackoverflow.com/a/31889368/1936079 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM