Greetings all,
i am trying to listen on a udp port and save everything recieved into a text file using php.
so far i have this:

#!/usr/local/bin/php –q

<?php

// Server IP address
$address = "192.168.1.6";

// Port to listen
$port = 3658;

$mysock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

socket_bind($mysock,$address, $port) or die('Could not bind to address'); 
socket_listen($mysock, 5);
$client = socket_accept($mysock);

// read 1024 bytes from client
$input = socket_read($client, 1024);

// write received data to the file
writeToFile('abc.txt', $input);

socket_close($client);
socket_close($mysock);
?> 

<? 
  /**
   * write string to file
   */
  function writeToFile($strFilename, $strText) 
{ 
      if($fp = @fopen($strFilename,"w ")) 
     { 
          $contents = fwrite($fp, $strText); 
          fclose($fp); 
          return true; 
      }else{ 
          return false; 
      } 

  } 
?>

and it doesnt look great because i have all these errors:

Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: The attempted operation is not supported for the type of object referenced. in C:\xampp\htdocs\new.php on line 14

Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: The attempted operation is not supported for the type of object referenced. in C:\xampp\htdocs\new.php on line 15

Warning: socket_read() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\new.php on line 18

Warning: socket_close() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\new.php on line 23

could some one please enlighten me as to what i am doing wronge? it would be most appreciated.

Astro

    From the manual.

    socket_listen() is applicable only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.

    I think you need to remove
    socket_listen($mysock, 5);
    $client = socket_accept($mysock);

    and use $mysock in the socket_read()

      actually i found this on the web, an thoguht i should give it a shot

      <?php
      
      
      
      // example of "daemon" in UDP
      while(TRUE) { // never stop the daemon
         $socketD = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create an UDP socket
      
         echo 'Creating socket... '; // YES, the socket is creating EACH TIME the while loops
      
         if($socketD === FALSE) { //omg, something went wrong
             echo 'socket_create failed: '.socket_strerror(socket_last_error())."\n";
             exit(1);
         }
      
         echo "OK\nBinding the socket on 192.168.1.2:3658 ... ";
         // let the daemon listen on the port 3658 on all interfaces
         if(!socket_bind($socketD, "192.168.1.2", 3658)) {
             socket_close($socketD);
             echo 'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
             exit(1);
         }
      
         echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";
         // so far, our socket is open and bound to a port: it's listening for ONE incoming connection
      
         // this is a special way of socket_read()'ing what's on the socket once someone establishes a connection
         socket_recvfrom($socketD, $buf, 65535, 0, $clientIP, $clientPort);
         if($buf === FALSE) { // something went wrong
             echo 'socket_read() returned false : '.socket_strerror(socket_last_error())."\n";
             continue;
         } elseif(strlen($buf) === 0) { // this should mean "client closed the connection"
             echo 'socket_read() returned an empty string : '.socket_strerror(socket_last_error())."\n";
             continue;
         }
      
         echo 'Incoming connection from '.$clientIP.':'.$clientPort.' ... ';
      
         // ok now thats the tricky part
         // if you want to reply to the client on the UDP socket it opened, you need to connect your listening socket on them
         // this won't work unless the client puts their IP in the IP  header of the packets it's sending to your socket
         // UDP sessions don't require them to do so, and if they don't, you have no way to tell who's talking to you on the socket, and therefore no way to reply
         // so if you want to interact with the client, you have to connect your socket on them, but by doing so, you're no longer listening to other incoming connections
         // one way to fixe that would be to fork the script when an incoming connection arrives and then stop the forked script once the client exits
         if(!socket_connect($socketD, $clientIP, $clientPort)) {
             echo 'socket_create failed to connect the clientsock on '.$clientIP.':'.$clientPort.' : '.socket_strerror(socket_last_error())."\n";
             socket_close($socketD);
             continue;
         }
         echo 'connected'."\n"; // cool, now we can reply to the client
      
         // now you can interact with the client using socket_write() and socket_read()
      
         // once you're finished, you need to socket_close() your sockets and let the while loop and re-create the listenning socket (because you're no longer listenning for new incoming connections)
      } // end of the infinite while
      
      ?>

      however it says:

      socket_bind failed: Only one usage of each socket address (protocol/network address/port) is normally permitted. 

      is it uses port 3658 to listen to port 3658?

        Did it receive some data?

        If you are not connecting to the remote IP, you should just close the socket after receiving the data.

          Write a Reply...