Udp error packets

Source: Internet
Author: User
For more information, see the following code $ word & amp; #39; HELLO & amp; #39; $ confarray (array (& amp; #39; ip & amp; #39; & gt; & amp; #39; 10.1.146.20.& amp; #39;, & amp; #39; port & amp; #39; & gt; 2001 ), for more information about array (& amp; #3, see the following code $ word = 'hello'; $ conf = array ('IP' => '10. 1.146.133 ', 'port' => 2001), array ('IP' => '10. 1.146.133 ', 'port' => 2002); function udpGet ($ word, $ ip, $ port) {$ sock = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP ); socket_set_option ($ sock, SOL_SOCKE T, SO_SNDTIMEO, array ('SEC '=> 2, 'usec' => 0); socket_set_option ($ sock, SOL_SOCKET, SO_RCVTIMEO, array ('SEC '=> 2, 'usec '=> 0); socket_sendto ($ sock, $ word, strlen ($ word), 0x100, $ ip, $ port); socket_recvfrom ($ sock, $ result, 8192, 0, $ host, $ port); socket_close ($ sock); return $ result;} for ($ I = 0; $ I <2; $ I ++) {$ res = udpGet ($ word, $ conf [$ I] ['IP'], $ conf [$ I] ['port']); var_dump ($ res);} is the continuous use of UPD to send and receive data to two servers (Problem, the server uses the simplest callback logic). If all the processes are normal, the client will receive 'hello' twice '. But what if a problem occurs on the service end? Currently, the client timeout time is 2 seconds. suppose Port 2001 sends data 3 seconds later and Port 2002 cannot be used. what is the result? "Two NULL !", Intuitively, this is the answer. If you think so too, congratulations. The actual answer is: NULLstring (5) "HELLO" analysis uses tcpdump to capture packets. the following result is displayed: (133 is the server, 163 is the client, php 5.3.1 is the client, Linux kernel 2.6.16) 12:01:39. 014658 IP route> 10.1.146.133.2001: UDP, length 512: 01: 41.015121 IP 10.1.146.163.40678> 10.1.146.133.2002: UDP, length 512: 01: 42.016103 IP 10.1.146.133.2001> 10.1.146.163.40678: UDP, length 5 according to my idea, the two requests should use different temporary ports for sending and receiving, but from the packet capture results, although the client performs socket_create twice, however, the same temporary port (40678) is used to send and receive data! This enables the second request to receive the response packet of the first request. It seems that this is a system BUG. The experiment found that this problem only exists in some systems, such as Linux kernel 2.6.32 + php5.2.3. Each time you specify a socket port, send and receive packets. The following red code is displayed. $ Sock = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP); $ sendPort = rand (10240,600 00); socket_bind ($ sock, '10. 1.146.163', $ sendPort); socket_set_option ($ sock, SOL_SOCKET, SO_SNDTIMEO, array ('SEC '=> 2, 'usec' => 0); socket_set_option ($ sock, SOL_SOCKET, SO_RCVTIMEO, array ('SEC '=> 2, 'usec' => 0); of course, the rand port may also have a collision, but after all, this probability is not big, the problem can be solved to a large extent. If you have a better solution, please feel free to contact us ~ ��0 ��2 ��0 ��2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.