PHP 套接字类型和 TCP 窗口大小

发布于 2024-11-08 16:01:23 字数 1073 浏览 0 评论 0原文

所以这实际上是一个由两部分组成的问题,第一个问题通向第二个问题。

我正在开发一个 PHP 服务器项目,我对创建套接字的所有不同方法感到有点困惑。我已经成功创建了三个套接字,每个套接字都使用一种新方法。有谁知道这三种方法之间的根本区别(如果有的话)?

使用“socket_create”的方法1

$Socket1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
socket_bind($Socket1, $LocalIP, $LocalPort)
socket_connect($Socket1, $DestIP, $DestPort)
//Method 1 Read
socket_read($Socket1)
//Method 1 Write
socket_write($Socket1, $WriteMsg, strlen($WriteMsg))

使用“fsockopen”的方法2

$Socket2 = fsockopen($Server, $Port)
//Method 2 Read
fgets($Socket2)
//Method 2 Write
fputs($Socket2, $PutMsg, strlen($PutMsg))

使用“stream_socket_client”的方法3

$Socket3 = stream_socket_client('tcp://'.$DestIP.':'.$DestPort)
//Method 3 Read
stream_socket_recvfrom($Socket3, $RecSize)
//Method 3 Write
stream_socket_sendto($Socket3, $SendMsg)

虽然我不明白我正在探索的区别所有三个选项都在寻找控制 TCP 窗口大小的方法。我试图通过 LAN 将数据包推送到客户端,该数据包的数据负载为 1460 字节,在查看数据包捕获后,数据包的 TCP 数据部分始终被缩短为 1448 字节。有什么想法吗?

提前致谢!

So this is really a two part question with the first leading to the second.

I'm working on a PHP server project and I'm a bit confused with all the different ways I can create a socket. I've managed to create three sockets each using a new method. Does anyone know the fundamental difference between these three methods if any?

Method 1 using 'socket_create'

$Socket1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
socket_bind($Socket1, $LocalIP, $LocalPort)
socket_connect($Socket1, $DestIP, $DestPort)
//Method 1 Read
socket_read($Socket1)
//Method 1 Write
socket_write($Socket1, $WriteMsg, strlen($WriteMsg))

Method 2 using 'fsockopen'

$Socket2 = fsockopen($Server, $Port)
//Method 2 Read
fgets($Socket2)
//Method 2 Write
fputs($Socket2, $PutMsg, strlen($PutMsg))

Method 3 using 'stream_socket_client'

$Socket3 = stream_socket_client('tcp://'.$DestIP.':'.$DestPort)
//Method 3 Read
stream_socket_recvfrom($Socket3, $RecSize)
//Method 3 Write
stream_socket_sendto($Socket3, $SendMsg)

Although I don't understand the difference I was exploring all three options looking for a way to control the TCP Window Size. I'm trying to push a packet to a client over my LAN that has a data payload of 1460 bytes and after reviewing a packet capture the TCP data portion of my packet is always cut short at 1448 bytes. Any ideas?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-11-15 16:01:23

您无法控制一次接收的实际数据量。 TCP 是一种流式传输协议。它向应用程序提供了一个字节流 API。你只需要做好阅读和重读的准备,直到得到你想要的东西。

You can't control the actual amount of data received at a time. TCP is a streaming protocol. It presents a byte-stream API to the application. You just have to be prepared to read and re-read until you have got what you want.

久光 2024-11-15 16:01:23

您应该能够使用 socket_set_option 来完成此操作命令(其中选项记录在 socket_get_option 命令):

$Socket1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
// send window
socket_set_option($Socket1, SOL_SOCKET, SO_SNDBUF, 1460);

请确保在 socket_create 之后立即调用它。如果需要修改接收窗口:

socket_set_option($Socket1, SOL_SOCKET, SO_RCVBUF, 1460);

You should be able to do this with the socket_set_option command (where options are documented in the socket_get_option command):

$Socket1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
// send window
socket_set_option($Socket1, SOL_SOCKET, SO_SNDBUF, 1460);

Just be sure to call it right after socket_create. If you need the receive window modified:

socket_set_option($Socket1, SOL_SOCKET, SO_RCVBUF, 1460);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文