[Sussex] Setting up a socket - PHP/Fedora

Brendan Whelan b_whelan at mistral.co.uk
Mon Sep 12 19:14:13 UTC 2005


Hello,

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.

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.

Any suggestions welcome.

Thanks, Brendan

----------------------------------

Server:

<?php

set_time_limit(0);

ob_implicit_flush();

$web_address = 'xxx.xxx.40.30'; 

$web_port = 9013;

print "$web_address<>$web_port<br>\n";

print "Define socket<br>\n";

if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)

{ print "Socket creation failed: socket_strerror($sock)<br>\n"; }

print "Bind socket<br>\n";

if (( $return = socket_bind($sock, $web_address, $web_port)) < 0)

{ print "Socket bind failed: socket_strerror($return)<br>\n"; }

print "Listen<br>\n";

if (( $client = socket_listen($sock)) < 0)

{ print "Socket listen failed: socket_strerror($client)<br>\n"; }

print "Accept messages<br>\n";

if (($message_in = socket_accept($sock)) < 0)

{ print "Accept failed: socket_strerror($message_in)<br>\n"; }

else

{

$received = socket_read($message_in, 4096);

print "Message received was >$received<br> - send a reply<br>\n";

$message_out = "Thanks";

print "Message to be returned is >$message_out<<br>\n";

if (( $return = socket_write($sock, $message_out, strlen($message_out))) < 0)

{

print "Unable to write: socket_strerror($return)<br>\n";

}

else

{

print "Return message of >$message_out< sent<br>\n";

}

}

print "Finished<br>\n";

?>

----------------------------------------------------

Client:

<?php

error_reporting(E_ALL);

ob_implicit_flush();

$web_address = 'xxx.xxx.40.30'; 

$web_port = 9013;

print "$web_address<>$web_port<br>\n";

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)

{ print "Socket creation failed: socket_strerror($sock)<br>\n"; }

else

{ print "Socket created<br>\n"; }

print "Attempt to connect to $web_address on port $web_port<br>\n";

if (($result = socket_connect($sock, $web_address, $web_port)) < 0)

{ print "Socket connection failed: socket_strerror($result)<br>\n"; }

else

{ print "Socket connected - sending a message<br>\n"; 

$message = "Hello world";

socket_write($sock, $message, strlen($message));

print "Message sent - awaiting a reply<br>\n";

if (($in = socket_read($sock, 4096)) < 0)

{

print "Receive failed: socket_strerror($in)";

}


print "Received: $in<br>\n";

socket_close($sock);

print "Finished<br>\n";

}

print "End of program<br>\n";

?> 

-----------------------------------------------------

Running client program gives:

php test_send_message.php

Content-type: text/html; charset=iso-8859-1

X-Powered-By: PHP/4.3.9

xxx.xxx.40.30<>9013<br>

Socket created<br>

Attempt to connect to xxx.xxx.40.30 on port 9013<br>

Socket connected - sending a message<br>

Message sent - awaiting a reply<br>

Received: <br>

Finished<br>

End of program<br>

------------------------------------------------

Running server program gives:

php test_socket.php

Content-type: text/html; charset=iso-8859-1

X-Powered-By: PHP/4.3.9

xxx.xxx.40.30<>9013<br>

Define socket<br>

Bind socket<br>

Listen<br>

Accept messages<br>

Message received was >Hello world<br> - send a reply<br>

Message to be returned is >Thanks<<br>

File size limit exceeded

Note: If run through a browser the error is:

Warning: socket_write() unable to write to socket [32]: Broken pipe in /home/db/support/support_application/test_socket.php on line 32

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.lug.org.uk/pipermail/sussex/attachments/20050912/6c9c1d8c/attachment.htm 


More information about the Sussex mailing list