本文整理汇总了PHP中socket_send函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_send函数的具体用法?PHP socket_send怎么用?PHP socket_send使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_send函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: send
function send()
{
if (!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
echo "Socket created";
if (!socket_connect($sock, '192.168.0.17', 9000)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \n");
}
echo "Connection established \n";
$msg = "Hello";
//Send the message to the server
if (!socket_send($sock, $msg, strlen($msg), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [{$errorcode}] {$errormsg} \n");
}
echo "Message send successfully \n";
//Now receive reply from server
if (socket_recv($sock, $buf, 2045, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [{$errorcode}] {$errormsg} \n");
}
//print the received message
$visual = $buf;
socket_close($sock);
}
示例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: sendaprs
function sendaprs($call, $lat, $lon, $desc, $ts)
{
global $aprs_server;
global $msg;
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($s, $aprs_server, 14580);
$N = 'N';
if ($lat < 0) {
$lat = -$lat;
$N = 'S';
}
$E = 'E';
if ($lon < 0) {
$lon = -$lon;
$E = 'W';
}
$msg = $call . ">WEB2NT:!";
$msg = $msg . sprintf("%02d%05.2f%s%s", floor($lat), ($lat - floor($lat)) * 60, $N, substr($ts, 0, 1));
$msg = $msg . sprintf("%03d%05.2f%s%s", floor($lon), ($lon - floor($lon)) * 60, $E, substr($ts, 1, 1));
$msg = $msg . sprintf("%s%s", $desc, "\r\n");
// echo $msg;
// echo "<p>";
socket_send($s, $msg, strlen($msg), 0);
$msg = date("Y-m-d H:i:s ") . $msg;
}
示例4: coop_send
function coop_send($message)
{
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");
}
//Connect socket to remote server
if (!socket_connect($sock, COOPSERVER, COOPPORT)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \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");
}
$buf = 'my buffer';
if (false !== ($bytes = socket_recv($sock, $buf, 1024, 0))) {
echo "Read {$bytes} bytes of socket_recv(). close socket ...";
} else {
echo "socket_recv() error; Reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($sock);
return $buf;
}
示例5: send
/**
* 发送数据
* @param string $data
* @return bool | int
*/
function send($data)
{
$length = strlen($data);
$written = 0;
$t1 = microtime(true);
//总超时,for循环中计时
while ($written < $length) {
$n = socket_send($this->sock, substr($data, $written), $length - $written, null);
//超过总时间
if (microtime(true) > $this->timeout_send + $t1) {
return false;
}
if ($n === false) {
$errno = socket_last_error($this->sock);
//判断错误信息,EAGAIN EINTR,重写一次
if ($errno == 11 or $errno == 4) {
continue;
} else {
return false;
}
}
$written += $n;
}
return $written;
}
示例6: sendCommandToRPi
function sendCommandToRPi($command)
{
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");
}
//Connect socket to remote server
try {
if (!socket_connect($sock, '172.17.10.71', 1111)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo $errormsg;
return False;
}
} catch (Exception $e) {
}
//Send the message to the server
if (!socket_send($sock, $command, strlen($command), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
return False;
}
return true;
}
示例7: negotiate
function negotiate($socket)
{
socket_recv($socket, $buffer, 1024, 0);
for ($chr = 0; $chr < strlen($buffer); $chr++) {
if ($buffer[$chr] == chr(255)) {
$send = isset($send) ? $send . $buffer[$chr] : $buffer[$chr];
$chr++;
if (in_array($buffer[$chr], array(chr(251), chr(252)))) {
$send .= chr(254);
}
if (in_array($buffer[$chr], array(chr(253), chr(254)))) {
$send .= chr(252);
}
$chr++;
$send .= $buffer[$chr];
} else {
break;
}
}
if (isset($send)) {
socket_send($socket, $send, strlen($send), 0);
}
if ($chr - 1 < strlen($buffer)) {
return substr($buffer, $chr);
}
}
示例8: init
public function init()
{
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");
}
echo "Socket created \n";
//Connect socket to remote server
if (!socket_connect($sock, self::ADDRESS, self::PORT)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \n");
}
echo "Connection established \n";
$message = json_encode(['message' => 'Alright!']);
//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");
}
echo "Message send successfully \n";
//Now receive reply from server
if (socket_recv($sock, $buf, 2045, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [{$errorcode}] {$errormsg} \n");
}
//print the received message
socket_shutdown($sock);
return $buf;
}
示例9: write
/**
* Writes data to the socket.
*
* @param string $string String to send.
* @param integer $flags Send flags - php.net/socket_send
*
* @return integer|boolean Number of bytes written, False on error
*/
public function write($string, $flags = null)
{
if ($flags !== null) {
return socket_send($this->get_resource(), $string, strlen($string), $flags);
}
return socket_write($this->get_resource(), $string);
}
示例10: write
/**
* Writes to this peer.
*
* @param string $what What to write.
*
* @return integer
*/
public function write($what)
{
$ret = null;
if ($this->isConnected()) {
$ret = @socket_send($this->socket, $what, strlen($what), 0);
}
return $ret;
}
示例11: dataIs
protected final function dataIs(data\data $data)
{
$bytesWritten = socket_send($this->resource, $data, strlen($data), 0);
if ($bytesWritten === false) {
throw $this->exception();
}
return $this->numberOfBytesConsumedIs(new data\data\numberOfBytes($bytesWritten));
}
示例12: send_all
function send_all(&$socket, $snd)
{
$to_send = strlen($snd) . ":" . $snd;
if (@socket_send($socket, $to_send, strlen($to_send), 0)) {
return true;
}
return false;
}
示例13: send
public function send(ServerMessage\Crypt $message)
{
$message->prepend(ByteConverter::IntSixteenBits($message->getLength()));
if (!socket_send($this->socket, $message, strlen($message), 0)) {
$this->error();
}
return $this;
}
示例14: write
public function write($buffer, $length)
{
$bytesWritten = socket_send($this->socket, $buffer, $length, 0);
if ($bytesWritten === false) {
$bytesWritten = -1;
}
return $bytesWritten;
}
示例15: send
function send($message)
{
global $sock;
if (!socket_send($sock, $message, strlen($message), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "Could not send data: [{$errorcode}] {$errormsg} \n";
}
}