php - 如何正确使用 PHP 监听传入的套接字连接?

标签 php sockets

我需要创建一个套接字服务器来监听一些传入的数据。我创建了一个名为 socketSetup 的 PHP,并在页面加载后执行它。该文件本身位于一个子目录中,我需要将套接字绑定(bind)到一个地址。

这可能吗?我的主要查询在于我如何绑定(bind)它......我只是将它绑定(bind)到主域还是子目录?

$sock = socket_create(AF_INET, SOCK_STREAM, 0);

socket_bind($sock, "http://mydomain.com/mysubdirectory, 80) or die('Could not bind to address');

谢谢

最佳答案

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...:::我应该将我的服务器应用程序绑定(bind)到哪个地址?:::...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
推荐:0.0.0.0 //在每个可用地址绑定(bind)。

如果你知道你在做什么,你应该使用 路由器分配给你机器的ip .仅当您在本地网络之外等待数据包时才需要这样做。否则您应该绑定(bind)在 127.0.0.1

分配给您机器的 IP 将如下所示。
示例:192.168.1.5
网络掩码:255.255.255.0 <--忽略这个

找到它,简单的方法:

You can retrieve the address with $_SERVER['SERVER_ADDR'] but you shouldn't rely on it.



找到它,硬编码的方式:

On windows: type ipconfig /all and search for the IPv4 Address entry.
On linux : type ifconfig
If you don't have access to the console then use phpinfo(); <- not sure about that



绑定(bind) http://www.example.com/dir/ 是错误的,它永远不会起作用。

CAUTION: The machine doesn't know anything about the external ip ( the one assigned to you by the DSLAM i.e 128.31.53.90 ), mainly because that's the routers job.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...:::我应该将我的服务器应用程序绑定(bind)到哪个端口?:::...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When a client sends data to you, they arrive first at your router who then decides what to do with them. The decision is based on your routers NAT table and the incoming port.



NAT 表通常如下所示:
+==============================================+
|   LAN IP   |  Incoming Port  | Outgoing Port |
+============+=================+===============+
|192.168.1.5 |       8080      |       80      |
+------------+-----------------+---------------+
|192.168.1.2 |       25        |       25      |
+------------+-----------------+---------------+

让 X 成为具有以下结构的数据包:
         Header            Data
 +===================+===============+
|-| Your External IP |      ...      |
|X|                  |      ...      |
|-| Destination Port |      ...      |
 +===================+===============+

路由器将占用destination port并搜索类似的 incoming port表中的条目

如果它找到一个,那么它将 重定向 到匹配的 LAN IP 的数据包:传出端口

例如:

A packet arrives with destination port 8080, it will be forwarded to 192.168.1.5:80.

       >>                    25          >>       192.168.1.2:25


因此,要捕获某人在 8080 发送的数据包,您需要在 192.168.1.5:80 绑定(bind),
如果您在端口 25 等待数据包,则为 25。

CAUTION: Binding at port 80 WILL FAIL if you're running a webserver because
they are bound by default to that port (unless you changed it).

关于php - 如何正确使用 PHP 监听传入的套接字连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555540/

相关文章:

php - codeigniter中的 session 错误?

php - Laravel 5 : Use different database for testing and local

C 网络 : Sendto() returning Errno 22, EINVAL

java - 如何在 Java 中将数组写入 OutputStream?

python - 接收器的scapy?

c - 用户到套接字的高效映射

php - 使用jquery从插入到src标记的html输入字段中获取值

php - 如何将 yii2 web 应用程序中的文件夹压缩为 zip?

php - MySQL-排序后插入行

android - 根据我对 Ejabberd 的请求获取 SSL 相关错误