fsocket(<udp:host>,<port>) Not working, no errors reported.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #1

    fsocket(<udp:host>,<port>) Not working, no errors reported.

    Hello Hello!

    I'm trying to connect to a host that accepts UDP on a port I setup and tested from a command line too called udp_write. However a PHP fsocketopen() call doesn't work nor does it give an error.

    stripped down code + fake data:

    Code:
    $fp = fsockopen("udp://999.999.999.999",9999,$errNo,$errMsg) or die("$errNo : $errMsg");
    $test = fwrite($fp, "Logging Test"); 
    print($test);
    var_dump() of $fp says its a socket resource and print($test) gives me an integer < 100. (ie. 12)

    Do you see an issue with this code? I can connect from this host to the target host from the command line with this UDP port.

    Let me know if I'm missing something,

    Cheers,




    Dan
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    Hi Dan,

    This piece from PHP.net explains the lack of errors with UDP:

    Warning

    UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless " protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.
    fwrite returns the number of bytes which have been wrote so it should be working. Can you explain what data you are sending and what result you are expecting?

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Originally posted by hoopy
      Hi Dan,

      This piece from PHP.net explains the lack of errors with UDP:



      fwrite returns the number of bytes which have been wrote so it should be working. Can you explain what data you are sending and what result you are expecting?
      This code will be used to send log messages to a logger server. Each application in the environment (including mine) has been setup to send string text to that server. The logger software there adds a time-stamp and formats it into a .log file for troubleshooting and monitoring purposes.

      I did see that note (in fact I've read everything in the manual, as I always do so I don't come here and sound like an idiot :D )

      However, i thought I'd get some error when I tried to write, hence why I dumped the output of fwrite().

      That is the byte written your right. So I'm thinking the problem is not in the code and maybe elsewhere.

      I'll post back a solution if it's related to the code.


      Thank you,




      Dan

      Comment

      Working...