略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: getprotobynumber

2024-12-22

getprotobynumber

(PHP 4, PHP 5, PHP 7, PHP 8)

getprotobynumberGet protocol name associated with protocol number

说明

getprotobynumber(int $protocol): string|false

getprotobynumber() returns the protocol name associated with protocol protocol as per /etc/protocols.

参数

protocol

The protocol number.

返回值

Returns the protocol name as a string, 或者在失败时返回 false.

参见

add a noteadd a note

User Contributed Notes 3 notes

up
4
i at tiankonguse dot com
8 years ago
getprotobynumber()
=====================
getprotobynumber() gets the protocol name for the given protocol number on the local system.

If the protocol number is not recognized,the function returns FALSE.

The given code snippet gives the list of protocols along with their protocol numbers

=====================
code

<?
$format = "number <---> protocol\n<br />";
$format = "%6s <---> %s\n<br />";
for($number = 0; $number < 255; $number ++) {
    if (getprotobynumber ( $number ))
       
        printf ( $format, " $number", getprotobynumber ( $number ) );
}
?>

=====================
OUTPUT:
0 <---> ip
1 <---> icmp
2 <---> igmp
3 <---> ggp
4 <---> ipencap
5 <---> st
6 <---> tcp
8 <---> egp
9 <---> igp
12 <---> pup
17 <---> udp
20 <---> hmp
22 <---> xns-idp
27 <---> rdp
29 <---> iso-tp4
33 <---> dccp
36 <---> xtp
37 <---> ddp
38 <---> idpr-cmtp
41 <---> ipv6
43 <---> ipv6-route
44 <---> ipv6-frag
45 <---> idrp
46 <---> rsvp
47 <---> gre
50 <---> esp
51 <---> ah
57 <---> skip
58 <---> ipv6-icmp
59 <---> ipv6-nonxt
60 <---> ipv6-opts
73 <---> rspf
81 <---> vmtp
88 <---> eigrp
89 <---> ospf
93 <---> ax.25
94 <---> ipip
97 <---> etherip
98 <---> encap
103 <---> pim
108 <---> ipcomp
112 <---> vrrp
115 <---> l2tp
124 <---> isis
132 <---> sctp
133 <---> fc
135 <---> mobility-header
136 <---> udplite
137 <---> mpls-in-ip
138 <---> manet
139 <---> hip
140 <---> shim6
141 <---> wesp
142 <---> rohc
up
2
recycling dot sp dot am at gmail dot com
11 years ago
Note that by the time of writing this note (5.3.3), there seems to be only two defined constants for the protocol numbers: SOL_UDP and SOL_TCP.
For all other protocols (like ICMP,... ) you'll have to provide the exact number (EG: 1 for ICMP, ... )
<?php
   
echo getprotobynumber(SOL_TCP) . "\n"; // prints tcp
   
echo getprotobynumber(SOL_UDP) . "\n"; // prints udp
   
var_dump(getprotobyname('tcp') == SOL_TCP); // bool(true)
   
var_dump(getprotobyname('udp') == SOL_UDP); // bool(true)
?>
On a UNIX system (don't know if this is true for Windows), the php functions getprotobyname and getprotobynumber are just wrapper for their corresponding system calls. These functions returns the protocol name or protocol number based on the definition from IANA. If you are looking for a protocol and you don't know its number, you can find the exact protocol number here: http://www.iana.org/assignments/protocol-numbers
up
1
Vipindas K.S
14 years ago
getprotobynumber()
=====================
getprotobynumber() gets the protocol name for the given protocol number on the local system. If the protocol number is not recognized,the function returns FALSE.The given code snippet gives the list of protocols along with their protocol numbers

<?
$format = "%6s %s\n<br />";
for ($number = 0; $number < 255; $number++)
{
   if (getprotobynumber ($number))

      printf ($format, " $number-->", getprotobynumber ($number));
}

?>

OUTPUT:

0--> ip
1--> icmp
3--> ggp
6--> tcp
8--> egp
12--> pup
17--> udp
20--> hmp
22--> xns-idp
27--> rdp
66--> rvd

官方地址:https://www.php.net/manual/en/function.getprotobynumber.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3