Пример #1
0
 /**
  * Send the syslog to target host using the UDP protocol. Note that the UDP protocol
  * is stateless, which means we can't confirm that the message was received by the
  * other end
  * @param  MessageInterface $message
  * @param  string           $target  Host:port, if port not specified uses default 514
  * @return void
  * @throws \RuntimeException If there's an error creating the socket
  */
 public function send(MessageInterface $message, $target)
 {
     $msg = $message->getMessageString();
     if (strpos($target, ':')) {
         list($host, $port) = explode(':', $target);
     } else {
         $host = $target;
         $port = self::DEFAULT_UDP_PORT;
     }
     $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
     if ($sock === false) {
         $errorCode = socket_last_error();
         $errorMsg = socket_strerror($errorCode);
         throw new \RuntimeException("Error creating socket: [{$errorCode}] {$errorMsg}");
     }
     socket_sendto($sock, $msg, strlen($msg), 0, $host, $port);
     socket_close($sock);
 }
Пример #2
0
 /**
  * Send the syslog to target host using the TCP protocol.Automatically adds a \n character to end
  * of the message string
  * @param  MessageInterface $message
  * @param  string           $target  Host:port, if port not specified uses default 514
  * @return void
  * @throws \RuntimeException If there's an error creating the socket
  */
 public function send(MessageInterface $message, $target)
 {
     //Add EOL to message so the receiver knows it has ended
     $msg = $message->getMessageString() . "\n";
     if (strpos($target, ':')) {
         list($host, $port) = explode(':', $target);
     } else {
         $host = $target;
         $port = self::DEFAULT_TCP_PORT;
     }
     $sock = fsockopen($host, $port, $errorCode, $errorMsg, $this->timeout);
     if ($sock === false) {
         throw new \RuntimeException("Error connecting to {$server}: [{$errorCode}] {$errorMsg}");
     }
     $written = fwrite($sock, $msg);
     fclose($sock);
     if ($written != strlen($msg)) {
         throw new \RuntimeException("Error sending message to {$server} not all bytes sent.");
     }
     return $this;
 }