本文整理汇总了PHP中socket_last_error函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_last_error函数的具体用法?PHP socket_last_error怎么用?PHP socket_last_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_last_error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: connect
/**
* Connects the TCP socket to the host with the given IP address and port
* number
*
* Depending on whether PHP's sockets extension is loaded, this uses either
* <var>socket_create</var>/<var>socket_connect</var> or
* <var>fsockopen</var>.
*
* @param string $ipAddress The IP address to connect to
* @param int $portNumber The TCP port to connect to
* @param int $timeout The timeout in milliseconds
* @throws SocketException if an error occurs during connecting the socket
*/
public function connect($ipAddress, $portNumber, $timeout)
{
$this->ipAddress = $ipAddress;
$this->portNumber = $portNumber;
if ($this->socketsEnabled) {
if (!($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
throw new SocketException(socket_last_error($this->socket));
}
socket_set_nonblock($this->socket);
@socket_connect($this->socket, $ipAddress, $portNumber);
$write = array($this->socket);
$read = $except = array();
$sec = floor($timeout / 1000);
$usec = $timeout % 1000;
if (!socket_select($read, $write, $except, $sec, $usec)) {
$errorCode = socket_last_error($this->socket);
} else {
$errorCode = socket_get_option($this->socket, SOL_SOCKET, SO_ERROR);
}
if ($errorCode) {
throw new SocketException($errorCode);
}
socket_set_block($this->socket);
} else {
if (!($this->socket = @fsockopen("tcp://{$ipAddress}", $portNumber, $socketErrno, $socketErrstr, $timeout / 1000))) {
throw new SocketException($socketErrstr);
}
stream_set_blocking($this->socket, true);
}
}
示例2: send_data
function send_data($_remote_ip, $_remote_port, $_message)
{
if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $_remote_ip)) {
// echo 'IP ist gültig.';
} else {
$_remote_ip = gethostbyname($_remote_ip);
}
if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
$log_update = mysql_query("INSERT INTO `log`(`message`) VALUES ('" . $errormsg . "')");
}
echo "Socket created \n";
//Connect socket to remote server
if (!socket_connect($sock, $_remote_ip, $_remote_port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \n");
$log_update = mysql_query("INSERT INTO `log`(`message`) VALUES ('" . $errormsg . "')");
}
echo "Connection established \n";
//Send the message to the server
if (!socket_send($sock, $_message, strlen($_message), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [{$errorcode}] {$errormsg} \n");
$log_update = mysql_query("INSERT INTO `log`(`message`) VALUES ('" . $errormsg . "')");
}
echo "Message send successfully \n";
//$log_update = mysql_query("INSERT INTO `log`(`message`) VALUES ('".$_remote_ip."')");
}
示例3: processPacket
function processPacket($packetData)
{
$packet = new dhcpPacket();
$packet->parse($packetData);
$processor = new dhcpRequestProcessor($this, $this->packetProcessor, $this->storage, $packet);
if ($responsePacket = $processor->getResponse()) {
$responseData = $responsePacket->build();
$this->verbosity && (print "Sending response" . "\n");
$ciaddr = $packet->getClientAddress();
if ($ciaddr == '0.0.0.0') {
$this->verbosity && (print "Switching to broadcast address...\n");
$ciaddr = '255.255.255.255';
}
$this->verbosity && (print "Attempting to send response packet to " . $ciaddr . "\n");
$numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, $ciaddr, 68);
if ($numBytesSent === FALSE) {
$this->verbosity && (print "send failed for specific address, broadcast.\n");
$numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, "255.255.255.255", 68);
$numBytesSent === FALSE && $this->verbosity && printf('socket send error: %s\\n', socket_strerror(socket_last_error($this->socket)));
}
$numBytesSent && $this->verbosity && (print "Response packet sent.\n");
} else {
$this->verbosity && (print "Packet ignored\n");
}
}
示例4: getsock
function getsock($port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) {
$error = socket_strerror(socket_last_error());
$msg = "socket create({$port}) failed";
echo "ERR: {$msg} '{$error}'\n";
return NULL;
}
socket_set_nonblock($socket);
$res = socket_connect($socket, API_HOST, $port);
$timeout = 50;
while ($res === false && $timeout > 0) {
$err = socket_last_error($socket);
echo ".";
if ($timeout > 1 && ($err == 115 || $err == 114)) {
$timeout--;
usleep(50);
$res = socket_connect($socket, API_HOST, $port);
continue;
}
$error = socket_strerror($err);
$msg = "socket connect({$port}) failed";
echo "ERR: {$msg} '{$error}'\n";
socket_close($socket);
return NULL;
}
socket_set_block($socket);
return $socket;
}
示例5: sendsock
function sendsock($in)
{
global $config;
$service_port = $config['sockport'];
$address = $config['sockhost'];
if (!function_exists("socket_create")) {
error_log(date("y-m-d H:i:s") . " 未启用socket模块\n", 3, "error.log");
return null;
}
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
error_log(date("y-m-d H:i:s") . "socket_create() failed, reason: " . socket_strerror(socket_last_error()) . "\n", 3, "error.log");
return null;
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
error_log(date("y-m-d H:i:s") . "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n", 3, "error.log");
return null;
}
socket_write($socket, $in, strlen($in));
$result = socket_read($socket, 8192);
$arr = unpack("C*", $result);
socket_close($socket);
return $arr;
}
示例6: socketClose
public function socketClose()
{
$this->sock_error = socket_strerror(socket_last_error($this->fsock));
socket_clear_error();
@socket_close($this->fsock);
return $this;
}
示例7: WakeOnLan
function WakeOnLan($addr, $mac_address, $socket_number)
{
$addr_byte = explode(':', $mac_address);
$hw_addr = '';
for ($a = 0; $a < 6; $a++) {
$hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255);
for ($a = 1; $a <= 16; $a++) {
$msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error code is '" . socket_last_error($s) . "' - " . socket_strerror(socket_last_error($s));
return FALSE;
} else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if ($opt_ret < 0) {
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return FALSE;
}
if (socket_sendto($s, $msg, strlen($msg), 0, $ip_address, $socket_number)) {
echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
} else {
echo "Magic packet failed!";
return FALSE;
}
}
}
}
}
示例8: sendcmd
function sendcmd($in, $address, $service_port)
{
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
return "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
return "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_write($socket, $in, strlen($in));
$mess = "";
/*$next = "";*/
sleep(1);
/*$next = socket_read($socket, 4096);*/
while (0 != socket_recv($socket, $out, 4096, MSG_DONTWAIT)) {
if ($out != null) {
$mess .= $out;
}
}
/*$mess .= $next;*/
socket_close($socket);
return $mess;
}
示例9: mainLoop
private function mainLoop()
{
$changed_sockets = $this->allsockets;
@socket_select($changed_sockets, $write = null, $exceptions = null, 0);
foreach ($this->apps as $app) {
$app->onTick();
}
foreach ($changed_sockets as $socket) {
if ($socket == $this->master) {
if (($rec = socket_accept($this->master)) < 0) {
$this->log("Error: Could not connect\n" . socket_strerror(socket_last_error($rec)));
continue;
} else {
$this->log("New client connecting...");
$client = new Connection($this, $rec);
$this->clients[$rec] = $client;
$this->allsockets[] = $rec;
}
} else {
$client = $this->clients[$socket];
$bytes = @socket_recv($socket, $data, 4096, 0);
if (!$bytes) {
$client->onDisconnect();
unset($this->clients[$socket]);
$index = array_search($socket, $this->allsockets);
unset($this->allsockets[$index]);
unset($client);
} else {
$client->onData($data);
}
}
}
}
示例10: queryAPI
function queryAPI($jsonQuery)
{
$service_port = 12345;
$address = "localhost";
$msg = "{\n \"array\": [\n";
echo $msg;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die('{"type" : "error", "msg" : "connection error: ' . socket_strerror(socket_last_error()) . '"}]}');
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
die('{"type" : "error", "msg" : "connection error: ' . socket_strerror(socket_last_error($socket)) . '"}]}');
}
$in = stripslashes($jsonQuery);
socket_write($socket, $in, strlen($in));
socket_write($socket, ';', 1);
$buffer = "";
while ($out = socket_read($socket, 2048)) {
echo $out;
ob_flush();
flush();
}
#JSON Array end
$msg = "]\n }";
echo $msg;
echo PHP_EOL;
ob_flush();
flush();
socket_close($socket);
}
示例11: create_connection
function create_connection($host, $port)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($socket)) {
echo 'Unable to create socket: ' . socket_strerror(socket_last_error()) . PHP_EOL;
} else {
echo "Socket created.\n";
}
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: ' . socket_strerror(socket_last_error()) . PHP_EOL;
} else {
echo "Set options on socket.\n";
}
if (!socket_bind($socket, $host, $port)) {
echo 'Unable to bind socket: ' . socket_strerror(socket_last_error()) . PHP_EOL;
} else {
echo "Socket bound to port {$port}.\n";
}
if (!socket_listen($socket, SOMAXCONN)) {
echo 'Unable to listen on socket: ' . socket_strerror(socket_last_error());
} else {
echo "Listening on the socket.\n";
}
while (true) {
$connection = @socket_accept($socket);
if ($connection) {
echo "Client {$connection} connected!\n";
send_data($connection);
} else {
echo "Bad connection.";
}
}
}
示例12: execute_script
function execute_script($scriptid, $hostid)
{
$res = 1;
$command = script_make_command($scriptid, $hostid);
$nodeid = id2nodeid($hostid);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
$res = 0;
}
if ($res) {
global $ZBX_SERVER, $ZBX_SERVER_PORT;
$res = socket_connect($socket, $ZBX_SERVER, $ZBX_SERVER_PORT);
}
if ($res) {
$send = "Command{$nodeid}{$hostid}{$command}\n";
socket_write($socket, $send);
}
if ($res) {
$res = socket_read($socket, 65535);
}
if ($res) {
list($flag, $msg) = split("", $res);
$message["flag"] = $flag;
$message["message"] = $msg;
} else {
$message["flag"] = -1;
$message["message"] = S_CONNECT_TO_SERVER_ERROR . ' [' . $ZBX_SERVER . ':' . $ZBX_SERVER_PORT . '] [' . socket_strerror(socket_last_error()) . ']';
}
if ($socket) {
socket_close($socket);
}
return $message;
}
示例13: connect
/**
* Initiates a connection on the socket
*
* @param string $host
* @param integer $port
* @return void
* @throws SocketException
*/
public function connect($host, $port)
{
if (!@socket_connect($this->socket, $host, $port)) {
$error = socket_strerror(socket_last_error());
throw new SocketException($error);
}
}
示例14: loop
/**
* the main loop for the client we handle I/O operations
*/
public function loop()
{
$tmp = TRUE;
// read from socket
$input = @socket_read($this->socket, 1024000);
if ($input !== FALSE) {
if (!empty($input)) {
// if there are multiple messages in one "packet"
if (substr_count($input, '\\final\\') > 1) {
$ex = explode('\\final\\', $input);
foreach ($ex as $item) {
if (trim($item) !== '') {
if ($this->commands($item . '\\final\\') === FALSE) {
$tmp = FALSE;
}
}
}
} else {
$tmp = $this->commands($input);
}
return $tmp;
}
}
if (socket_last_error($this->socket) > 0) {
return FALSE;
}
return TRUE;
}
示例15: _tcp_send_request
private static function _tcp_send_request($socket_write_json = null)
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (false === $sock) {
echo "sock create error!\n";
}
$address = Config::get("kgi.kgi_mid_server_ip");
$port = Config::get("kgi.kgi_mid_server_port");
try {
$result = socket_connect($sock, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($sock)) . "\n";
exit;
die;
}
socket_write($sock, $socket_write_json, strlen($socket_write_json));
while ($out_str = socket_read($sock, 2048)) {
// echo "revice result\n";
// echo $out_str . "\n";
$json_data = json_decode($out_str);
break;
}
socket_close($sock);
} catch (\ErrorException $e) {
//echo $e->getMessage() . "\n";
}
}