Guest User

Untitled

a guest
Jul 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. ## in init() method
  2. <?php
  3. // Initialise remote listener
  4. if(!$this->socket_server)
  5. {
  6. $socket = stream_socket_server("udp://0.0.0.0:1337", $errno, $errstr, STREAM_SERVER_BIND);
  7. if(!$socket)
  8. {
  9. $this->send_msg('failed to open socket!');
  10. }
  11. else
  12. {
  13. $this->socket_server = $socket;
  14. }
  15. }
  16.  
  17.  
  18. ## in poll() method (called on each loop of the irc bot)
  19. <?php
  20. // Process incoming stream requests
  21. if($this->socket_server)
  22. {
  23. // Setup arrays for stream_select
  24. $read = array($this->socket_server);
  25. $write = NULL;
  26. $except = NULL;
  27.  
  28. // Check if we have data to read
  29. while(stream_select($read, $write, $except, 0, 0))
  30. {
  31. // Read the data from the UDP socket
  32. $line = fgets($this->socket_server, 4096);
  33.  
  34. // Forward the message to the channel
  35. $this->send_msg($line);
  36. }
  37. }
  38.  
  39.  
  40.  
  41. ## Client webserver script
  42. <?php
  43. $fp = stream_socket_client("udp://:1337", $errno, $errstr);
  44. if (!$fp) {
  45. echo "ERROR: $errno - $errstr<br />\n";
  46. } else {
  47. fwrite($fp, "Hello, World!\r\n");
  48. fclose($fp);
  49. }
  50. ?>
Add Comment
Please, Sign In to add comment