与Windows使用PHP中的套接字有什么区别?

我写了一个应用程序在使用套接字的PHP。 突然间,有必要在Windows上运行它,在这之前它只在Linux上没有问题。

目前的问题是使用socket_recv函数,就像$bytes = @socket_recv($socket, $data, 2048, MSG_DONTWAIT); 。 首先在窗口上没有任何MSG_DONTWAIT常量,因为我得到了nocite。 我发现它的一个小的修复,如:

 if (!defined('MSG_DONTWAIT')) define('MSG_DONTWAIT', 0x40); 

然后它说:

 Warning: socket_recv(): unable to read from socket [0]: The operation completed successfully. 

之后,我决定问,可能会有与Windows和Linux上的套接字工作有一些区别?

我相信,当你在windows中创建socket而不是linux时,是有区别的。

尝试这样的事情:

 <?php // Init error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '127.0.0.1'; $port = 10000; // On Windows we need to use AF_INET $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX); // Create socket if (($sock = socket_create($domain, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } // Bind socket to port if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; } // start listening if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; } do { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; break; } /* Send instructions. */ $msg = "\nWelcome to the PHP Test server. \n" . "To quit, type 'quit'. To shut down the server type 'shutdown'.\n"; socket_write($msgsock, $msg, strlen($msg)); do { if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; break 2; } if (!$buf = trim($buf)) { continue; } if ($buf == 'quit') { break; } if ($buf == 'shutdown') { socket_close($msgsock); break 2; } $talkback = "PHP: You said '$buf'.\n"; socket_write($msgsock, $talkback, strlen($talkback)); echo "$buf\n"; } while (true); socket_close($msgsock); } while (true); socket_close($sock); ?>