PHP to Go using Unix domain sockets -
i'm trying use unix socket have php send ipc messages go. seems work, except php seems keep reading response socket, , won't let go. (the browser load-spinner keeps going, , there's no page render.) i'm using flag php_normal_read in php:socket_read() function, , explicitly sending "\n" go.
watching go process in terminal, appears work correctly on end.
edit: think browser caching issue. if send different data php echo, works expected. switched fmt.fprintln() make sure wasn't getting newline wrong.
go:
package main import ( "net" "fmt" "log" "os" ) const socket_addr = "/tmp/odc_ws.sock" func echoserver(c net.conn){ buf := make([]byte, 512) size, err := c.read(buf) if err != nil { log.fatal("read error: ", err) } data := buf[0:size] fmt.println("server received: ", string(data)) // new code t := time.now() retmsg := fmt.sprintf("ok+ @ %s", t) size, err = fmt.fprintln(c, retmsg) //size, err = c.write(ok) if err == nil{ fmt.println("wrote many bytes: ", size) } else { log.fatal("write error: ", err) } } func main(){ l, err := net.listen("unix", socket_addr) if err != nil{ fmt.println("on noes: %s", err) return } defer l.close() for{ fd, err := l.accept() if err != nil{ log.fatal("accept error", err) } go echoserver(fd) } }
php:
ob_implicit_flush(); $socket_file = "/tmp/odc_ws.sock"; if (($socket = socket_create(af_unix, sock_stream, 0)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>"; } if (socket_connect($socket, $socket_file) === false) { echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error($sock)) . "<br>"; } $msg = 'php sent go message @ ' . date('h:i:s'); $write_res = socket_write($socket, $msg, $msg_len = strlen($msg)); if($write_res != $msg_len){ echo '<div>socket write error: ' . socket_strerror( socket_last_error($socket) ) . '</div>'; } while($read = socket_read($socket, 512, php_normal_read)){ echo "<div>server says: $read</div>"; } socket_close($socket);
it not php issue; not closing server connection, php continues wait more data. add defer c.close()
:
func echoserver(c net.conn){ defer c.close() buf := make([]byte, 512) size, err := c.read(buf) if err != nil {
php_normal_read
force socket_read
read lines of text rather binary streams.
from php docs on socket_read (emphasis mine)
socket_read() returns data string on success, or false on error (including if remote host has closed connection)
Comments
Post a Comment