本文整理汇总了PHP中stream_socket_recvfrom函数的典型用法代码示例。如果您正苦于以下问题:PHP stream_socket_recvfrom函数的具体用法?PHP stream_socket_recvfrom怎么用?PHP stream_socket_recvfrom使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stream_socket_recvfrom函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run($force = false)
{
if ($force) {
$this->killProgramsOnDreddPort();
}
$socket = sprintf('tcp://%s:%s', $this->host, $this->port);
$server = stream_socket_server($socket, $errno, $errorMessage);
if ($server === false) {
throw new UnexpectedValueException("Server could not bind to socket: {$errorMessage}");
}
$buffer = "";
for (;;) {
$client = stream_socket_accept($server);
while ($socketData = stream_socket_recvfrom($client, self::RECV_LENGTH)) {
$buffer .= $socketData;
// determine if message terminating character is present.
if (strpos($buffer, self::MESSAGE_END) === false) {
continue;
}
$messages = [];
foreach (explode(self::MESSAGE_END, $buffer) as $data) {
$message = json_decode($data);
// if not valid json the partial message needs saved
if (!$message) {
$buffer = $message;
continue;
}
$messages[] = $message;
}
foreach ($messages as $message) {
$this->processMessage($message, $client);
}
}
}
}
示例2: sw_server_handle_recvfrom
function sw_server_handle_recvfrom($server_socket, $events, $server)
{
$data = stream_socket_recvfrom($server_socket, $server->buffer_size, $server->flags, $peer);
if ($data !== false && $data != '') {
$server->protocol->onData($peer, $data);
}
}
示例3: read
/**
* {@inheritdoc}
*/
public function read($length)
{
if (!$this->isReadable()) {
throw new RuntimeException("Stream is not readable");
}
return stream_socket_recvfrom($this->getContext(), $length);
}
示例4: query
public function query($text)
{
$head = <<<_HEADER_
<?xml version="1.0" ?>
<wordsegmentation version="0.1">
<option showcategory="1" />
<authentication username="{$this->user}" password="{$this->passwd}" />
<text>
_HEADER_;
$footer = <<<_FOOT_
</text>
</wordsegmentation>
_FOOT_;
$this->data_send = $text;
$text = str_replace("&", " ", $text);
$querystr = $head . $text . $footer;
$tempxml = simplexml_load_string($querystr);
$resp = array();
if ($tempxml) {
if (stream_socket_sendto($this->sock, $tempxml->asXML())) {
do {
$ttt = stream_socket_recvfrom($this->sock, 65525);
$ttt = iconv('big5', 'utf-8', $ttt);
$resp[] = $ttt;
} while (!simplexml_load_string(implode($resp)));
return $this->data_recv = html_entity_decode(implode($resp));
}
} else {
$this->data_recv = 0;
return null;
}
}
示例5: enableCrypto
/**
* {@inheritdoc}
*/
public function enableCrypto(int $method, float $timeout = 0) : \Generator
{
$resource = $this->getResource();
if ($method & 1 || 0 === $method) {
yield from $this->await($timeout);
} else {
yield from $this->poll($timeout);
$raw = stream_socket_recvfrom($resource, 11, STREAM_PEEK);
if (11 > strlen($raw)) {
throw new FailureException('Failed to read crypto handshake.');
}
$data = unpack('ctype/nversion/nlength/Nembed/nmax-version', $raw);
if (0x16 !== $data['type']) {
throw new FailureException('Invalid crypto handshake.');
}
$version = $this->selectCryptoVersion($data['max-version']);
if ($method & $version) {
// Check if version was available in $method.
$method = $version;
}
}
do {
// Error reporting suppressed since stream_socket_enable_crypto() emits E_WARNING on failure.
$result = @stream_socket_enable_crypto($resource, (bool) $method, $method);
} while (0 === $result && !(yield from $this->poll($timeout)));
if ($result) {
$this->crypto = $method;
return;
}
$message = 'Failed to enable crypto.';
if ($error = error_get_last()) {
$message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
}
throw new FailureException($message);
}
示例6: send
public function send($socket)
{
$deferred = new Deferred();
stream_set_blocking($socket, false);
$data = $this->getRequest();
\Amp\onWritable($socket, function ($writer, $socket) use($deferred, &$data) {
if ($bytes = fwrite($socket, $data)) {
if ($bytes < \strlen($data)) {
$data = substr($data, $bytes);
return;
}
$size = 8192;
\Amp\onReadable($socket, function ($reader, $socket) use($deferred, &$size) {
/* make attention to not read too much data */
$data = stream_socket_recvfrom($socket, $size, STREAM_PEEK);
if (false === ($pos = strpos($data, "\r\n\r\n"))) {
if (\strlen($data) == $size) {
$size *= 2;
// unbounded??
}
return;
}
\Amp\cancel($reader);
$deferred->succeed($this->parseResponse(fread($socket, $pos + 4)));
});
} else {
$deferred->succeed(null);
}
\Amp\cancel($writer);
});
return $deferred->promise();
}
示例7: testThrowsExceptionOnProtocolDesynchronizationErrors
/**
* @medium
* @group connected
* @expectedException \Predis\Protocol\ProtocolException
*/
public function testThrowsExceptionOnProtocolDesynchronizationErrors()
{
$connection = $this->createConnection();
$stream = $connection->getResource();
$connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
stream_socket_recvfrom($stream, 1);
$connection->read();
}
示例8: handleData
public function handleData($stream)
{
$data = stream_socket_recvfrom($stream, $this->bufferSize);
if ('' === $data || false === $data || feof($stream)) {
$this->end();
} else {
$this->emit('data', array($data, $this));
}
}
示例9: read
/** {@inheritdoc} */
public function read(FramePickerInterface $picker, Context $context, $isOutOfBand)
{
stream_socket_recvfrom($this->socket->getStreamResource(), self::SOCKET_BUFFER_SIZE, STREAM_PEEK, $remoteAddress);
if (!$remoteAddress && !$this->isLocalIo) {
stream_socket_recvfrom($this->socket->getStreamResource(), self::SOCKET_BUFFER_SIZE);
throw new AcceptException($this->socket, 'Can not accept client: failed to receive remote address.');
}
$reader = new DatagramClientIo($this->socket, $this->isLocalIo ? null : $remoteAddress);
return new AcceptedFrame($remoteAddress, new UdpClientSocket($this->socket, $remoteAddress, $reader->read(new RawFramePicker(), $context, $isOutOfBand)));
}
示例10: handleData
public function handleData($socket)
{
$data = @stream_socket_recvfrom($socket, $this->bufferSize);
if ('' === $data || false === $data) {
$this->handleDisconnect($socket);
$this->loop->removeStream($socket);
} else {
$client = $this->getClient($socket);
$client->emit('data', array($data));
}
}
示例11: handleRecv
function handleRecv($sock)
{
$pkt = stream_socket_recvfrom($sock, self::MAX_PACKET_SIZE, 0, $peer);
if ($pkt == false) {
$this->emit('error', array("Reading packet from {$peer} failed"));
return;
}
if ($pkt != "") {
$this->emit('packet', array($pkt, $peer, $this));
}
}
示例12: process
function process()
{
$request = stream_socket_recvfrom($this->srv, 1500, 0, $this->cli);
if ($request == false) {
cy_log(CYE_ERROR, "read request from srv fd error.");
return false;
}
$this->request_init();
$this->request($request);
$this->request_shutdown();
}
示例13: read
public function read($stream)
{
// Socket is raw, not using fread as it's interceptable by filters
// See issues #192, #209, and #240
$data = stream_socket_recvfrom($stream, $this->bufferSize);
if ('' !== $data && false !== $data) {
$this->notifyNext(new StreamEvent("/stream/data", $data));
}
if ('' === $data || false === $data || !is_resource($stream) || feof($stream)) {
$this->notifyCompleted();
}
}
示例14: server
public function server()
{
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
die("{$errstr} ({$errno})");
}
do {
$pkt = stream_socket_recvfrom($socket, 1, 0, $peer);
echo $pkt, "\n";
echo "{$peer}\n";
//stream_socket_sendto($socket, date("D M j H:i:s Y\r\n"), 0, $peer);
} while (true);
}
示例15: read
public function read()
{
$data = stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peerAddress);
if ($data === false) {
// receiving data failed => remote side rejected one of our packets
// due to the nature of UDP, there's no way to tell which one exactly
// $peer is not filled either
$this->notifyError(new \Exception('Invalid message'));
return;
}
$this->notifyNext(new StreamEvent("/datagram/data", $data, ["peer" => $peerAddress]));
$this->close();
}