<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2745.2800" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>
<DIV><FONT face=Arial size=2><FONT size=2>
<P>Hello,</P>
<P>I am attempting to create a simple sockets program in PHP. I have created a
server and a client routine - see below. The server starts and then waits for a
message when the client sends a message the server confirms receipt and then
tries to send a reply. At this point I get a failure. If I run through a browser
I get a "socket[32] broken pipe" error but when run in a terminal window I get
"File size limit exceeded". The output from both programs is shown below.</P>
<P>I have tried umpteen variations without success. My best deduction is that
the client is not waiting for a message when the server sends the reply.</P>
<P>Any suggestions welcome.</P>
<P>Thanks, Brendan</P>
<P>----------------------------------</P>
<P>Server:</P>
<P><?php</P>
<P>set_time_limit(0);</P>
<P>ob_implicit_flush();</P>
<P>$web_address = 'xxx.xxx.40.30'; </P>
<P>$web_port = 9013;</P>
<P>print "$web_address<>$web_port<br>\n";</P>
<P>print "Define socket<br>\n";</P>
<P>if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)</P>
<P>{ print "Socket creation failed: socket_strerror($sock)<br>\n"; }</P>
<P>print "Bind socket<br>\n";</P>
<P>if (( $return = socket_bind($sock, $web_address, $web_port)) < 0)</P>
<P>{ print "Socket bind failed: socket_strerror($return)<br>\n"; }</P>
<P>print "Listen<br>\n";</P>
<P>if (( $client = socket_listen($sock)) < 0)</P>
<P>{ print "Socket listen failed: socket_strerror($client)<br>\n"; }</P>
<P>print "Accept messages<br>\n";</P>
<P>if (($message_in = socket_accept($sock)) < 0)</P>
<P>{ print "Accept failed: socket_strerror($message_in)<br>\n"; }</P>
<P>else</P>
<P>{</P>
<P>$received = socket_read($message_in, 4096);</P>
<P>print "Message received was >$received<br> - send a
reply<br>\n";</P>
<P>$message_out = "Thanks";</P>
<P>print "Message to be returned is >$message_out<<br>\n";</P>
<P>if (( $return = socket_write($sock, $message_out, strlen($message_out))) <
0)</P>
<P>{</P>
<P>print "Unable to write: socket_strerror($return)<br>\n";</P>
<P>}</P>
<P>else</P>
<P>{</P>
<P>print "Return message of >$message_out< sent<br>\n";</P>
<P>}</P>
<P>}</P>
<P>print "Finished<br>\n";</P>
<P>?></P>
<P>----------------------------------------------------</P>
<P>Client:</P>
<P><?php</P>
<P>error_reporting(E_ALL);</P>
<P>ob_implicit_flush();</P>
<P>$web_address = 'xxx.xxx.40.30'; </P>
<P>$web_port = 9013;</P>
<P>print "$web_address<>$web_port<br>\n";</P>
<P>if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)</P>
<P>{ print "Socket creation failed: socket_strerror($sock)<br>\n"; }</P>
<P>else</P>
<P>{ print "Socket created<br>\n"; }</P>
<P>print "Attempt to connect to $web_address on port $web_port<br>\n";</P>
<P>if (($result = socket_connect($sock, $web_address, $web_port)) < 0)</P>
<P>{ print "Socket connection failed: socket_strerror($result)<br>\n";
}</P>
<P>else</P>
<P>{ print "Socket connected - sending a message<br>\n"; </P>
<P>$message = "Hello world";</P>
<P>socket_write($sock, $message, strlen($message));</P>
<P>print "Message sent - awaiting a reply<br>\n";</P>
<P>if (($in = socket_read($sock, 4096)) < 0)</P>
<P>{</P>
<P>print "Receive failed: socket_strerror($in)";</P>
<P>}</P>
<P></P>
<P>print "Received: $in<br>\n";</P>
<P>socket_close($sock);</P>
<P>print "Finished<br>\n";</P>
<P>}</P>
<P>print "End of program<br>\n";</P>
<P>?> </P>
<P>-----------------------------------------------------</P>
<P>Running client program gives:</P>
<P>php test_send_message.php</P>
<P>Content-type: text/html; charset=iso-8859-1</P>
<P>X-Powered-By: PHP/4.3.9</P>
<P>xxx.xxx.40.30<>9013<br></P>
<P>Socket created<br></P>
<P>Attempt to connect to xxx.xxx.40.30 on port 9013<br></P>
<P>Socket connected - sending a message<br></P>
<P>Message sent - awaiting a reply<br></P>
<P>Received: <br></P>
<P>Finished<br></P>
<P>End of program<br></P>
<P>------------------------------------------------</P>
<P>Running server program gives:</P>
<P>php test_socket.php</P>
<P>Content-type: text/html; charset=iso-8859-1</P>
<P>X-Powered-By: PHP/4.3.9</P>
<P>xxx.xxx.40.30<>9013<br></P>
<P>Define socket<br></P>
<P>Bind socket<br></P>
<P>Listen<br></P>
<P>Accept messages<br></P>
<P>Message received was >Hello world<br> - send a reply<br></P>
<P>Message to be returned is >Thanks<<br></P>
<P>File size limit exceeded</P>
<P>Note: If run through a browser the error is:</P>
<P>Warning: socket_write() unable to write to socket [32]: Broken pipe in
/home/db/support/support_application/test_socket.php on line
32</P></FONT></FONT></DIV></FONT></DIV></BODY></HTML>