本文整理汇总了PHP中socket_write函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_write函数的具体用法?PHP socket_write怎么用?PHP socket_write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: http_request
function http_request($host, $data)
{
if (!($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
echo "socket_create() error!\r\n";
exit;
}
if (!socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1)) {
echo "socket_set_option() error!\r\n";
exit;
}
if (!socket_connect($socket, $host, 80)) {
echo "socket_connect() error!\r\n";
exit;
}
if (!socket_write($socket, $data, strlen($data))) {
echo "socket_write() errror!\r\n";
exit;
}
while ($get = socket_read($socket, 1024, PHP_NORMAL_READ)) {
$content .= $get;
}
socket_close($socket);
$array = array('HTTP/1.1 404 Not Found', 'HTTP/1.1 300 Multiple Choices', 'HTTP/1.1 301 Moved Permanently', 'HTTP/1.1 302 Found', 'HTTP/1.1 304 Not Modified', 'HTTP/1.1 400 Bad Request', 'HTTP/1.1 401 Unauthorized', 'HTTP/1.1 402 Payment Required', 'HTTP/1.1 403 Forbidden', 'HTTP/1.1 405 Method Not Allowed', 'HTTP/1.1 406 Not Acceptable', 'HTTP/1.1 407 Proxy Authentication Required', 'HTTP/1.1 408 Request Timeout', 'HTTP/1.1 409 Conflict', 'HTTP/1.1 410 Gone', 'HTTP/1.1 411 Length Required', 'HTTP/1.1 412 Precondition Failed', 'HTTP/1.1 413 Request Entity Too Large', 'HTTP/1.1 414 Request-URI Too Long', 'HTTP/1.1 415 Unsupported Media Type', 'HTTP/1.1 416 Request Range Not Satisfiable', 'HTTP/1.1 417 Expectation Failed', 'HTTP/1.1 Retry With');
for ($i = 0; $i <= count($array); $i++) {
if (eregi($array[$i], $content)) {
return "{$array[$i]}\r\n";
break;
} else {
return "{$content}\r\n";
break;
}
}
}
示例2: flushAll
/**
* {@inheritdoc}
*/
public function flushAll()
{
if ($this->currentOnly) {
return apc_clear_cache('user') && apc_clear_cache();
}
$result = true;
foreach ($this->servers as $server) {
if (count(explode('.', $server['ip'])) == 3) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} else {
$socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
}
// generate the raw http request
$command = sprintf("GET %s HTTP/1.1\r\n", $this->getUrl());
$command .= sprintf("Host: %s\r\n", $server['domain']);
if ($server['basic']) {
$command .= sprintf("Authorization: Basic %s\r\n", $server['basic']);
}
$command .= "Connection: Close\r\n\r\n";
// setup the default timeout (avoid max execution time)
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 2, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 2, 'usec' => 0));
socket_connect($socket, $server['ip'], $server['port']);
socket_write($socket, $command);
$content = '';
do {
$buffer = socket_read($socket, 1024);
$content .= $buffer;
} while (!empty($buffer));
if ($result) {
$result = substr($content, -2) == 'ok';
}
}
return $result;
}
示例3: sock_write
function sock_write(&$sock, $data, $length = 0, $flush = false)
{
$sock->error = "";
$sock->errno = 0;
if (empty($length)) {
$length = strlen($data);
}
$ret = false;
$retries = 5;
while (($ret === false || $ret < $length) && $retries > 0) {
$ret += @socket_write($sock->socket, substr($data, $ret), $length - $ret);
if ($ret === false || $ret < $length) {
usleep(500);
}
$retries--;
}
if ($ret === false || $ret < $length) {
$sock->errno = 0;
if ($ret < $length && $ret !== false) {
$sock->error = "Socket write timeout";
} else {
$sock->error = "Socket write error";
}
}
/* else if ($flush)
sock_flush($sock);*/
}
示例4: send
public function send($data)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == null) {
return false;
}
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0));
if (!socket_connect($socket, $this->host, $this->port)) {
return false;
}
$packed = json_encode($data);
echo $packed;
//Write Header
socket_write($socket, "ZBXD");
//Write Data length
$length = strlen($packed);
socket_write($socket, pack("V*", $length, $length >> 32));
socket_write($socket, $packed);
$return_header = socket_read($socket, 5);
$return_length = unpack("V*", socket_read($socket, 8));
$return_length = $return_length[1] + ($return_length[2] << 32);
$return_data = socket_read($socket, $return_length);
$return_data = json_decode($return_data);
socket_close($socket);
return $return_data;
}
示例5: run_loop
function run_loop()
{
//This will stall the thread / request
$i = 0;
while ($_SESSION['running'] != -1) {
#read from local socket and put on session variable
while ($out = socket_read($this->socket, 8192)) {
if ($out === false) {
exit("[Server] Unable to read from local socket");
}
$this->met_data .= $out;
}
#If data on SESSION variable write data to local socket
if ($this->handler_data != "") {
$in = socket_write($this->socket, $this->handler_data, strlen($this->handler_data));
if ($in === false) {
exit("[Server] Unable to write to local socket");
}
$this->handler_data = "";
}
$this->update_session_data();
if (!stristr(PHP_OS, "linux")) {
sleep(1);
}
//added to work with apache/IIS on windows otherwise the consecutive reads DoS the socket
}
}
示例6: hg_sendCmd
function hg_sendCmd($cmd, $ip, $port)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
return false;
}
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
return false;
}
if (!isset($cmd['charset'])) {
$cmd['charset'] = '';
}
$str = json_encode($cmd);
//echo ($str);
//$str = base64_encode($str);
socket_write($socket, $str, strlen($str));
$data = '';
while ($out = socket_read($socket, 256)) {
$data .= $out;
if (strlen($out) < 256) {
break;
}
}
socket_close($socket);
//$data = base64_decode($data);
return $data;
}
示例7: ircReport
function ircReport($stuff)
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, Settings::pluginGet("host"), Settings::pluginGet("port"));
socket_write($sock, $stuff . "\n");
socket_close($sock);
}
示例8: listenForClients
function listenForClients()
{
$this->serviceConnection = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->serviceConnection, $this->serviceHost, $this->servicePort);
socket_listen($this->serviceConnection, 10);
echo 'LISTENING ',$this->servicePort,"\n";
while($clientSocket = socket_accept($this->serviceConnection))
{
socket_write($clientSocket, "OK\n");
$deviceToken = trim(socket_read($clientSocket, 512, PHP_NORMAL_READ));
$message = trim(socket_read($clientSocket, 512, PHP_NORMAL_READ));
if(!empty($deviceToken) && !empty($message))
{
$this->sendNotification($deviceToken, $message);
socket_write($clientSocket, "SENT\n");
}
else
{
socket_write($clientSocket, "ERROR\n");
}
socket_close($clientSocket);
}
}
示例9: run
public function run()
{
$ret = array();
while (true) {
$inArr = $this->get_files_by_ext($this->urlFilePath, 'url');
if (!empty($inArr)) {
sleep(2);
foreach ($inArr as $in) {
if (!in_array($in, $ret)) {
$ret[] = $in;
exec("cp {$this->urlFilePath}{$in} {$this->backupFilePath}");
$fileContent = file_get_contents($this->urlFilePath . $in);
if (socket_write($this->socket, $fileContent, strlen($fileContent))) {
$this->addLog("发送到服务器信息成功!发送的内容为: {$fileContent}");
sleep(2);
} else {
$this->addLog("发送失败! reason :" . socket_strerror($this->socket));
}
}
}
} else {
$this->addLog("无URL文件");
sleep(1);
}
}
}
示例10: flushAll
public function flushAll()
{
$result = true;
foreach ($this->servers as $server) {
if (count(explode('.', $server['ip']) == 3)) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} else {
$socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
}
// generate the raw http request
$command = sprintf("GET %s HTTP/1.1\r\n", $this->router->generate('sonata_page_apc_cache', array('token' => $this->token)));
$command .= sprintf("Host: %s\r\n", $server['domain']);
$command .= "Connection: Close\r\n\r\n";
// setup the default timeout (avoid max execution time)
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 2, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 2, 'usec' => 0));
socket_connect($socket, $server['ip'], $server['port']);
socket_write($socket, $command);
$content = socket_read($socket, 1024);
if ($result) {
$result = substr($content, -2) == 'ok' ? true : false;
}
}
return $result;
}
示例11: sendAll
/**
* send summary to graphite
* @return bool - status of operation (TRUE = success, FALSE = failure)
*/
public function sendAll()
{
$bucket_prefix = $this->buildBucketPrefix();
$address = gethostbyname($this->cfg['host']);
$cmdBuf = '';
foreach ($this->stats_summary as $stat) {
// action name mapping
$action = preg_replace('/[\\. ]/', '', $stat['action']);
$action = preg_replace('/_ACTION$/', '', $action);
// HACK to prevent old event names to appear in graphite
if (!preg_match('/_/', $action)) {
continue;
}
$cmdBuf .= $bucket_prefix . $action . ' ' . $stat['value'] . ' ' . $stat['time'] . "\n";
}
// var_dump($cmdBuf);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $address, $this->cfg['port']);
if (!$result) {
return false;
}
$write_result = socket_write($socket, $cmdBuf, strlen($cmdBuf));
if (!$write_result) {
return false;
}
return true;
}
示例12: query
function query($query_string, $timeout)
{
$tmp = "";
$string = "";
$stime = array();
$this->timer($stime);
$n = strlen($query_string);
if ($this->is_connected == 0) {
$this->reconnect();
}
if ($this->sock && $n > 0 && socket_write($this->sock, $query_string, $n) == $n) {
while (1) {
if ($tmp = socket_read($this->sock, 81920, PHP_BINARY_READ)) {
if (($n = strpos($tmp, SOCK_END)) === FALSE) {
$string .= $tmp;
} else {
$string .= substr($tmp, 0, $n + strlen(SOCK_END));
break;
}
} else {
$this->close();
return false;
}
$usec = $this->timer($stime);
if ($usec >= $timeout) {
$this->close();
return false;
}
usleep(1);
}
return $string;
}
$this->close();
return false;
}
示例13: write
/**
* Write data to socket
*
* @param $data
*
* @throws Exception\IO\ReadException
*/
function write($data)
{
if (!socket_write($this->socketResource, $data, strlen($data))) {
throw new WriteException($this, $data);
}
$this->getEventDispatcher()->dispatch(WriteEvent::getEventName(), new WriteEvent($this, $data));
}
示例14: await
private function await()
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock < 0) {
echo "Error:" . socket_strerror(socket_last_error()) . "\n";
}
$ret = socket_bind($sock, $this->ip, $this->port);
if (!$ret) {
echo "BIND FAILED:" . socket_strerror(socket_last_error()) . "\n";
exit;
}
echo "OK\n";
$ret = socket_listen($sock);
if ($ret < 0) {
echo "LISTEN FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
do {
$new_sock = null;
try {
$new_sock = socket_accept($sock);
} catch (Exception $e) {
echo $e->getMessage();
echo "ACCEPT FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
try {
$request_string = socket_read($new_sock, 1024);
$response = $this->output($request_string);
socket_write($new_sock, $response);
socket_close($new_sock);
} catch (Exception $e) {
echo $e->getMessage();
echo "READ FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
} while (TRUE);
}
示例15: refresh_user
function refresh_user($discord_id)
{
global $socket, $error;
$so = socket_create(AF_UNIX, SOCK_DGRAM, 0);
if ($so === false) {
$msg = socket_strerror(socket_last_error());
$error = "Socket failed: {$msg}";
} else {
$res = socket_connect($so, $socket);
if ($res === false) {
$msg = socket_strerror(socket_last_error());
$error = "Connect failed: {$msg}";
} else {
$payload = json_encode(array('action' => 'refresh', 'user_id' => $discord_id));
$res = socket_write($so, $payload);
if ($res === false) {
$error = "Socket send failed";
} else {
if ($res < strlen($payload)) {
$error = "Socket did not send all data";
}
}
socket_shutdown($so);
socket_close($so);
}
}
}