PHPudp error packets

Source: Internet
Author: User
See the following code $ word = & amp; #39; HELLO & amp; #39; $ conf = array (& amp; #39; ip & amp; #39; = & gt; & amp; #39; 10.1.146.20.& amp; #39;, & amp; #39; port & amp; #39 ;=& gt; 2001), array (& am

Problem

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_SOCKET, 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, $ Results, 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 );} it means that the UPD is used continuously to send and receive data to two servers (to illustrate the problem, the server here 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, 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 two requests should be sent and received using different temporary ports. But according to the packet capture results, although the client performs socket_create twice, the actual use of the same temporary port (40678) 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.SolutionSpecify the socket port each time for sending and receiving. 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 & prime;, $ 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 be hit, but after all, this probability is not big, and the problem can be solved to a large extent.

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.