我正在使用套接字传输压缩文件,但是发送的文件大小与接收的文件大小不同。
这是服务器:
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("socket_create() fallito: motivo: " . socket_strerror($this->sock) . "\n");
$ret = socket_bind($sock, "192.168.1.40", "9000") or die("socket_bind() fallito: motivo: " . socket_strerror($ret) . "\n");
$ret = socket_listen($this->sock) or die("socket_listen() fallito: motivo: " . socket_strerror($ret) . "\n");
while(true){
if($buf = @socket_accept($this->sock)){
echo "Call incoming\n";
// Read lenght of file
$lun = socket_read($buf, 1024);
// Write ok
socket_write($buf, "OkContinue", 1024) or die("Could not send data to server\n");
// Read file of lenght $lun
$lettura = socket_recv ($buf, $lun);
// Write dimension
echo "I need rec file of $lun size, but i rec: ".strlen($lettura)." size";
}
}
?>
这是客户端:
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, "192.168.1.40", "9000") or die("Could not connect to server\n");
socket_write($socket, strlen($message), 1024) or die("Could not send data to server\n");
while ($out = socket_read($socket, 1024)) {
if ($out=="OkContinue"){
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
echo "Send text of ".strlen($message)." size";
}
}
?>
但这是客户端上的 2 条消息:
Send text of 3741477 size
这是在服务器上:
Call incoming
I need rec file of 3741477 size, but i rec: 82536 size
那么为什么即使协议是 TCP,你也会丢失没有重传的数据呢?怎么了?