本文整理汇总了PHP中getprotobyname函数的典型用法代码示例。如果您正苦于以下问题:PHP getprotobyname函数的具体用法?PHP getprotobyname怎么用?PHP getprotobyname使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getprotobyname函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Constructor.
*
* @param array $config Socket configuration, which will be merged with the base configuration
* @see CakeSocket::$_baseConfig
*/
public function __construct($config = array())
{
$this->config = array_merge($this->_baseConfig, $config);
if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']);
}
}
示例2: _connect
private function _connect()
{
if ($this->connection = socket_create($this->socket_domain, SOCK_STREAM, $this->socket_domain > 1 ? getprotobyname('tcp') : 0)) {
if (socket_connect($this->connection, $this->socket_address, $this->socket_port)) {
$this->setNonBlock();
// $this->connection); // устанавливаем неблокирующий режим работы сокета
return $this->connected = true;
} else {
$error = socket_last_error($this->connection);
socket_clear_error($this->connection);
switch ($error) {
case SOCKET_ECONNREFUSED:
case SOCKET_ENOENT:
// если отсутствует файл сокета, либо соединиться со слушающим сокетом не удалось - возвращаем false
$this->disconnect();
return false;
default:
// в иных случаях кидаем необрабатываемое исключение
throw new \Exception(socket_strerror($error));
}
}
}
// @TODO delete next line...
trigger_error('Client connect failed', E_USER_ERROR);
return false;
}
示例3: testConstruct
/**
* testConstruct method
*
* @return void
*/
public function testConstruct() {
$this->Socket = new CakeSocket();
$config = $this->Socket->config;
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => getprotobyname('tcp'),
'port' => 80,
'timeout' => 30
));
$this->Socket->reset();
$this->Socket->__construct(array('host' => 'foo-bar'));
$config['host'] = 'foo-bar';
$this->assertSame($this->Socket->config, $config);
$this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
$config = $this->Socket->config;
$config['host'] = 'www.cakephp.org';
$config['port'] = 23;
$config['protocol'] = 17;
$this->assertSame($this->Socket->config, $config);
}
示例4: connect
/**
* Gets the OrientSocket, and establishes the connection if required.
*
* @return \PhpOrient\Protocols\Binary\OrientSocket
*
* @throws SocketException
* @throws PhpOrientWrongProtocolVersionException
*/
public function connect()
{
if (!$this->connected) {
if (empty($this->hostname) && empty($this->port)) {
throw new SocketException('Can not initialize a connection ' . 'without connection parameters');
}
if (!extension_loaded("sockets")) {
throw new SocketException('Can not initialize a connection ' . 'without socket extension enabled. Please check you php.ini');
}
$this->_socket = @socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_set_block($this->_socket);
socket_set_option($this->_socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => self::READ_TIMEOUT, 'usec' => 0));
socket_set_option($this->_socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => self::WRITE_TIMEOUT, 'usec' => 0));
$x = @socket_connect($this->_socket, $this->hostname, $this->port);
if (!is_resource($this->_socket) || $x === false) {
throw new SocketException($this->getErr() . PHP_EOL);
}
$protocol = Reader::unpackShort($this->read(2));
if ($protocol > $this->protocolVersion) {
throw new PhpOrientWrongProtocolVersionException('Protocol version ' . $protocol . ' is not supported.');
}
//protocol handshake
$this->protocolVersion = $protocol;
$this->connected = true;
}
return $this;
}
示例5: send
/**
* Method send
*
* @param string $raw_text
*
* @return string $return_text
*/
public function send($raw_text)
{
if (!empty($raw_text)) {
$this->raw_text = $raw_text;
$xw = new xmlWriter();
$xw->openMemory();
$xw->startDocument('1.0');
$xw->startElement('wordsegmentation');
$xw->writeAttribute('version', '0.1');
$xw->startElement('option');
$xw->writeAttribute('showcategory', '1');
$xw->endElement();
$xw->startElement('authentication');
$xw->writeAttribute('username', $this->username);
$xw->writeAttribute('password', $this->password);
$xw->endElement();
$xw->startElement('text');
$xw->writeRaw($this->raw_text);
$xw->endElement();
$xw->endElement();
$message = iconv("utf-8", "big5", $xw->outputMemory(true));
//send message to CKIP server
set_time_limit(60);
$protocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $protocol);
socket_connect($socket, $this->server_ip, $this->server_port);
socket_write($socket, $message);
$this->return_text = iconv("big5", "utf-8", socket_read($socket, strlen($message) * 3));
socket_shutdown($socket);
socket_close($socket);
}
return $this->return_text;
}
示例6: connectSocket
protected function connectSocket()
{
if (filter_var($this->host, FILTER_VALIDATE_IP)) {
$ip = $this->host;
} else {
$ip = gethostbyname($this->host);
if ($ip == $this->host) {
throw new MongoConnectionException(sprintf('couldn\'t get host info for %s', $this->host));
}
}
// For some reason, PHP doesn't currently allow resources opened with
// fsockopen/pfsockopen to be used with socket_* functions, but HHVM does.
if (defined('HHVM_VERSION')) {
$this->socket = pfsockopen($ip, $this->port, $errno, $errstr, $this->connectTimeoutMS / 1000);
if ($this->socket === false) {
$this->socket = null;
throw new MongoConnectionException(sprintf("unable to connect to {$ip}:{$this->port} because: %s", "{$errstr} ({$errno})"));
}
} else {
$this->socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
if (!$this->socket) {
throw new MongoConnectionException(sprintf('error creating socket: %s', socket_strerror(socket_last_error())));
}
$connected = socket_connect($this->socket, $ip, $this->port);
if (!$connected) {
throw new MongoConnectionException(sprintf("unable to connect to {$ip}:{$this->port} because: %s", socket_strerror(socket_last_error())));
}
}
}
示例7: listen
/**
* Começa a escutar conexões e processar as demais funcionalidades
* @param int $port número da porta
* @throws EndPointException
*/
public function listen($port)
{
$this->port = $port;
/* @var resource cria o socket que escutará conexões */
$this->serverSocket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('TCP'));
if (!socket_set_option($this->serverSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($this->serverSocket));
exit;
}
/* seta o host e a porta */
if (!@socket_bind($this->serverSocket, 0, $this->port)) {
throw new EndPointException("socket_bind() falhou: resposta: " . socket_strerror(socket_last_error($this->serverSocket)));
}
/* inicia a escuta de conexões */
if (!@socket_listen($this->serverSocket)) {
throw new EndPointException("socket_listen() falhou: resposta: " . socket_strerror(socket_last_error($this->serverSocket)));
}
/* cria um array de sockets e adiciona o serverSocket no array */
$this->sockets = array($this->serverSocket);
$this->loopPrincipal();
/* Fecha todos os sockets e encerra a aplicação */
foreach ($this->sockets as $socket) {
if (get_resource_type($socket) == "Socket") {
socket_close($socket);
}
}
}
示例8: mServer
public function mServer()
{
$sock = socket_create(AF_INET, SOCK_DGRAM, getprotobyname('udp'));
$mIP = '239.255.255.250';
if (!socket_bind($sock, $mIP, 1900)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [{$errorcode}] {$errormsg} \n");
}
socket_set_option($sock, IPPROTO_IP, MCAST_JOIN_GROUP, array("group" => '239.255.255.250', "interface" => 0));
while (1) {
echo "Waiting for data ... \n";
socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "{$remote_ip} : {$remote_port} -- " . $buf;
$query = $this->parseHeaders($buf);
if (!isset($query["m-search"])) {
continue;
}
$response = "HTTP/1.1 200 OK\r\n";
$response .= "CACHE-CONTROL: max-age=1810\r\n";
$response .= "DATE: " . date("r") . "\r\n";
$response .= "EXT:\r\n";
$response .= "LOCATION: http://192.168.7.123:9000/TMSDeviceDescription.xml\r\n";
$response .= "SERVER: Linux/3.x, UPnP/1.1, fheME/0.6\r\n";
$response .= "ST: urn:fheme.de:service:X_FIT_HomeAutomation:1\r\n";
$response .= "USN: uuid:f6da16ab-0d1b-fe1c-abca-82aacf4afcac::urn:fheme.de:service:X_FIT_HomeAutomation:1\r\n";
$response .= "Content-Length: 0\r\n";
$response .= "\r\n";
//Send back the data to the client
socket_sendto($sock, $response, strlen($response), 0, $mIP, $remote_port);
}
socket_close($sock);
}
示例9: create
/**
* 創建SOCKET
* @author wave
*/
protected static function create()
{
self::$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if (!self::$sock) {
exit('create socket error');
}
}
示例10: initIcecastComm
function initIcecastComm($serv = '127.0.0.1', $port = 8000, $mount = "/setme", $pass = "hackmake")
{
global $iceGenre, $iceName, $iceDesc, $iceUrl, $icePublic, $icePrivate, $iceAudioInfoSamplerate, $iceAudioInfoBitrate, $iceAudioInfoChannels;
// We're going to create a tcp socket.
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_set_block($sock) or die("Blocking error: " . socket_strerror(socket_last_error($sock)) . " \n");
if (!$sock) {
die("Unable to create socket! " . socket_strerror(socket_last_error($sock)) . " \n");
} else {
// Connect to the server.
echo "Connecting to {$serv} on port {$port}\n";
if (socket_connect($sock, $serv, $port)) {
echo "Connection established! Sending login....\n";
$this->sendData($sock, "SOURCE {$mount} HTTP/1.0\r\n");
$this->sendLogin($sock, $pass);
$this->sendData($sock, "User-Agent: icecastwebm-php\r\n");
$this->sendData($sock, "Content-Type: video/webm\r\n");
$this->sendData($sock, "ice-name: {$iceName}\r\n");
$this->sendData($sock, "ice-url: {$iceUrl}\r\n");
$this->sendData($sock, "ice-genre: {$iceGenre}\r\n");
$this->sendData($sock, "ice-public: {$icePublic}\r\n");
$this->sendData($sock, "ice-private: {$icePrivate}\r\n");
$this->sendData($sock, "ice-description: {$iceDesc}\r\n");
$this->sendData($sock, "ice-audio-info: ice-samplerate={$iceAudioInfoSamplerate};ice-bitrate={$iceAudioInfoBitrate};ice-channels={$iceAudioInfoChannels}\r\n");
$this->sendData($sock, "\r\n");
// Go into loop mode.
$this->parseData($sock);
$this->initStream($sock);
// Start the stream.
//die;
} else {
die("Unable to connect to server! " . socket_strerror(socket_last_error($sock)) . " \n");
}
}
}
示例11: __construct
public function __construct($ip, $port)
{
$this->IP = $ip;
$this->Port = $port;
$this->Socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
$this->Connected = false;
SocketManager::AddSocket($this);
}
示例12: SMTP_Server_Socket
/**
* Constructor with an optional argument of a socket resource
*
* @param resource $socket A socket resource as returned by socket_create() or socket_accept()
*/
function SMTP_Server_Socket($socket = null)
{
$this->log = new SMTP_Server_Log();
$this->socket = $socket;
$this->remote_address = '';
if (!$this->socket) {
$this->socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
}
}
示例13: getConnection
/**
* @return resource
* @throws \Exception
*/
public function getConnection()
{
if (!empty($this->socket)) {
return $this->socket;
}
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($this->socket, getprotobyname('TCP'), TCP_NODELAY, 1);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ["sec" => self::STREAM_TIMEOUT, "usec" => 0]);
if (!socket_connect($this->socket, $this->host, $this->port)) {
throw new ConnectionException("Unable to connect to Cassandra node: {$this->host}:{$this->port}");
}
return $this->socket;
}
示例14: getConnection
/**
*
* @throws Exception
* @return resource
*/
public function getConnection()
{
if (!empty($this->socket)) {
return $this->socket;
}
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($this->socket, getprotobyname('TCP'), TCP_NODELAY, 1);
foreach ($this->_options['socket'] as $optname => $optval) {
socket_set_option($this->socket, SOL_SOCKET, $optname, $optval);
}
if (!socket_connect($this->socket, $this->_options['host'], $this->_options['port'])) {
throw new Exception("Unable to connect to Cassandra node: {$this->_options['host']}:{$this->_options['port']}");
}
return $this->socket;
}
示例15: Startup
public function Startup()
{
$Host = "10.10.43.138";
$Proto = getprotobyname("tcp");
set_time_limit(0);
$Socket = socket_create(AF_INET, SOCK_STREAM, $Proto) or dir("Create socket failed!!\n");
socket_bind($Socket, $Host, 5678) or die("Can not bind socket!!\n");
$Result = socket_listen($Socket);
while (true) {
$Client = socket_accept($Socket) or die("Can not read input!!\n");
$this->ResBind($Client);
$this->ResUserInfo($Client);
}
socket_close($Socket);
// close server
}