Home Backend Development PHP Tutorial UDP error package problem_PHP tutorial

UDP error package problem_PHP tutorial

Jul 14, 2016 am 10:07 AM
array conf udp word under code of look question

问题

看下面一段代码
$word = 'HELLO';
$conf = array(
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, $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);
}
就是连续用UPD向两个server收发数据(为说明问题,这里的server使用了最简单的回射逻辑),
如果一切流程正常,客户端会收到两次‘HELLO’。但是,如果服务端出了问题呢?
目前,客户端的超时时间是2秒,假设2001端口过了3秒发数据,而2002端口无法服务,
猜下结果会是什么呢?“两个NULL!”,直觉上应该是这个答案。如果你也这么认为,
那么恭喜你,答错了。
实际的答案是:
NULL
string(5) "HELLO"
分析
使用tcpdump抓包,得到如下结果
(133为服务端,163为客户端,客户端php版本5.3.1,Linux内核2.6.16)
12:01:39.014658 IP 10.1.146.163.40678 > 10.1.146.133.2001: UDP, length 5
12:01:41.015121 IP 10.1.146.163.40678 > 10.1.146.133.2002: UDP, length 5
12:01:42.016103 IP 10.1.146.133.2001 > 10.1.146.163.40678: UDP, length 5
 
按我的想法,两个请求应该使用不同的临时端口收发,但从抓包结果看,
客户端虽然进行了两次socket_create,但实际中却使用了同一临时端口(40678)
收发数据!这就使得第二个请求收到了第一个请求的回包。
 
感觉上这应该算是个系统的BUG,从实验中发现,此问题只在部分系统中存在,
比如Linux内核2.6.32+php5.2.3就没有此问题。
解决
每次指定socket端口,进行收发。如下面的红色代码所示。
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$sendPort = rand(10240, 60000);
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));
当然,rand的端口也有可能出现对撞,但毕竟这种机率不大,可以从很大程度上解决问题。
如果有更好的解决办法,欢迎大家交流~
 
�0�2
�0�2
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477851.htmlTechArticle问题 看下面一段代码 $word = HELLO; $conf = array( array(ip=10.1.146.133, port=2001), array(ip=10.1.146.133, port=2002) ); function udpGet($word, $ip, $port) { $sock = socke...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of how to display the ruler in Word and how to operate the ruler! Detailed explanation of how to display the ruler in Word and how to operate the ruler! Mar 20, 2024 am 10:46 AM

When we use Word, in order to edit the content more beautifully, we often use rulers. You should know that the rulers in Word include horizontal rulers and vertical rulers, which are used to display and adjust the document's page margins, paragraph indents, tabs, etc. So, how do you display the ruler in Word? Next, I will teach you how to set the ruler display. Students in need should quickly collect it! The steps are as follows: 1. First, we need to bring up the word ruler. The default word document does not display the word ruler. We only need to click the [View] button in word. 2. Then, we find the option of [Ruler] and check it. In this way, we can adjust the word ruler! Yes or no

How to add handwritten signature to word document How to add handwritten signature to word document Mar 20, 2024 pm 08:56 PM

Word documents are widely used due to their powerful functions. Not only can various formats be inserted into Word, such as pictures and tables, etc., but now for the integrity and authenticity of the files, many files require a manual signature at the end of the document. It sounds like this How to solve complex problems? Today I will teach you how to add a handwritten signature to a word document. Use a scanner, camera or mobile phone to scan or photograph the handwritten signature, and then use PS or other image editing software to perform necessary cropping on the image. 2. Select &quot;Insert - Picture - From File&quot; in the Word document where you want to insert the handwritten signature, and select the cropped handwritten signature. 3. Double-click the handwritten signature picture (or right-click the picture and select &quot;Set Picture Format&quot;), and the &quot;Set Picture Format&quot; pops up.

Where is the shading setting in word? Where is the shading setting in word? Mar 20, 2024 am 08:16 AM

We often use word for office work, but do you know where the shading settings are in word? Today I will share with you the specific operation steps. Come and take a look, friends! 1. First, open the word document, select a paragraph of text paragraph information that needs to be added with shading, then click the [Start] button on the toolbar, find the paragraph area, and click the drop-down button on the right (as shown in the red circle in the figure below) ). 2. After clicking the drop-down box button, in the pop-up menu options, click the [Border and Shading] option (as shown in the red circle in the figure below). 3. In the pop-up [Border and Shading] dialog box, click the [Shading] option (as shown in the red circle in the figure below). 4. In the filled column, select a color

How to draw a table in Word How to draw a table in Word Mar 19, 2024 pm 11:50 PM

Word is a very powerful office software. Compared with WPS, Word has more advantages in detail processing. Especially when the document description is too complex, it is generally more worry-free to use Word. Therefore, when you enter the society, you must learn some tips on using word. Some time ago, my cousin asked me a question like this. I often see other people drawing tables when using Word, and I feel very high-level. I laughed at that time. It seemed like high-level content, but actually it only took 3 steps to operate. Do you know how to draw a table in Word? 1. Open word, select the place where you want to insert the table, and find the &quot;Insert&quot; option in the upper menu bar. 2. Click the &quot;Table&quot; option, and densely packed small cubes will appear.

Do you know how to sum a Word table? Do you know how to sum a Word table? Mar 21, 2024 pm 01:10 PM

Sometimes, we often encounter counting problems in Word tables. Generally, when encountering such problems, most students will copy the Word table to Excel for calculation; some students will silently pick up the calculator. Calculate. Is there a quick way to calculate it? Of course there is, in fact the sum can also be calculated in Word. So, do you know how to do it? Today, let’s take a look together! Without further ado, friends in need should quickly collect it! Step details: 1. First, we open the Word software on the computer and open the document that needs to be processed. (As shown in the picture) 2. Next, we position the cursor on the cell where the summed value is located (as shown in the picture); then, we click [Menu Bar

How to use Copilot to generate code How to use Copilot to generate code Mar 23, 2024 am 10:41 AM

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for &quot;PowerPlatformTool&quot; and click the Install button

How to underline in Word How to underline in Word Mar 20, 2024 pm 03:16 PM

As a very commonly used word processing software, Word is used in our life, study and work all the time. Of course, if you want to make good use of Word to edit text, you must lay a good foundation. So today I will take you to learn how to underline in Word. You can do it together with the editor. It is very simple. 1. First, we open the file we need to edit. Here we take the following figure as an example. 2. Use the mouse to select the text we need to edit. In the pop-up tab, we select the [U] icon. The operation is as shown in the figure: 3. Let’s take a look at the effect: 4. In fact, we can use a more convenient and faster The method is to use the key combination [ctrl] + [U] on the keyboard to add and follow your text.

How to underline in word How to underline in word Mar 19, 2024 pm 10:49 PM

When you make a graduation thesis or resume cover, you will write your gender, age, class and other information on the cover, and draw a line behind it and write your own information. Then it involves the issue of underlining the name. That is what everyone is asking about how to underline in word. Below I will share with you the specific steps. Come and learn! First, open the document on your computer, click the [Start] button on the top toolbar of Word, and then click the underlined icon button under the letter [U], as shown in the red circle in the figure below. 2. Then, place the cursor where you want to underline, and then press the space bar on the keyboard. At this time, the underline will appear. The length of the underline can be controlled according to the number of spaces, as shown in the red circle in the figure below

See all articles