Help getting a socket to work?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am a PHP newbie (just got my "Hello World" page working this
morning). I'm doing some R&D work to see if PHP is viable for a
situation I have. To accomplish what I want to do, I have to have the
PHP page communicate directly with another process.

I want the PHP script to establish a socket connection to the other
process, send a message and receive some data back which would then
used for calculations and/or display on the resulting page. The PHP
page will be the client - the other process will be the server. The
server process is a program that I've written using Visual Basic 6 and
the socket there is the standard Winsock that's part of VB.

Shown below is the entire page that I'm using to test this concept;
I've adapted it from an example provided in the "Socket Functions"
section of the manual on the website. Amazingly enough, this
actually works - up to a point. I'm hoping someone here can help get
it working all the way through.

This script works up to the point of reading the response. It creates
the connection (my server process accepts the connection and receives
the "Hello world" message). The server sends its message. But then the
script seems to just sit there - it doesn't complete - it doesn't
generate any output.

Can anyone suggest what I might need to do to get this to work?

If it matters, I'm using PHP 5.0.1, IIS 5.1 running on Windows XP with
service pack 2.



echo "<h2>TCP/IP Connection</h2>\n";

$service_port = 1001;
$address = "";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
   echo "socket_create() failed: reason: " . socket_strerror($socket)
.. "<br>";
} else {
   echo "OK.<br>";

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
   echo "socket_connect() failed.\nReason: ($result) " .
socket_strerror($result) . "<br>";
} else {
   echo "OK.<br>";

$in = "Hello World";
$out = "";

echo "Sending message...";
socket_write($socket, $in, strlen($in));
echo "OK.<br>";

echo "Reading response: <br><br>";
while ($out = socket_read($socket, 999, PHP_NORMAL_READ)) {
   echo $out;

echo "<br>Closing socket...";
echo "OK.<br><br>";



Re: Help getting a socket to work?

Quoted text here. Click to load it

Are you sure it stops at the read? You are not checking if the write
succeeds. Have you used a packetsniffer to find out whats really going

Quoted text here. Click to load it

Mayeb you found another program that is broken by SP2 :)

Quoted text here. Click to load it

This blocks untill:
- 999 bytes are read.
- a \n, \r or is received

Are these conditions met?

BTW why socket_* and not fsockopen?


  Daniel Tryba

Re: Help getting a socket to work?

Andy / Daniel -


Quoted text here. Click to load it

yeah, very well could be.

Quoted text here. Click to load it

No, they weren't. I didn't know I needed to do that. When I added \n
to the message from the server, the script started working all the way
through. However, I'm still getting an error on the socket_read. It
says: "unable to read from socket. operation completed successfully."

I tried taking the socket_read out of the "while" construct but it
didn't make any difference.

I found a comment on the site that said that socket_read
doesn't work in Windows (the comment is nearly a year old). The writer
says to use: socket_recv instead. I tried it thus:

  echo $rcd;
  echo "<br>" . $buffer;

It worked!

Howeer, this appears to NOT wait on any terminating character. Since
it's possible for tcp/ip to break up the messages, I'm concerned that
I'll always receive complete messages. (I've had problems with that in
the past).

Quoted text here. Click to load it

Because I don't know any better. Should I be using it?

Any other advice and guidance will be greatly appreciated.

Thanks again.

Re: Help getting a socket to work?

Quoted text here. Click to load it

It did remove atleast some socket support (raw IIRC).

Quoted text here. Click to load it

Huh? I guess you mean incomplete messages? You'll have to play with
timeouts and blocking mode...

Quoted text here. Click to load it says:
This extension is EXPERIMENTAL. The behaviour of this extension --
including the names of its functions and anything else documented about
this extension -- may change without notice in a future release of PHP.
Use this extension at your own risk.

And since you are using a plain tcp socket, you might as wel use the
proven methods, which are fsockopen(), feof(), fread/fwrite,
fgets/fputs. Your program would roughly be (no error handling):

$s=fsockopen($address, $service_port);

fwrite($s,"Hello World");
 echo fgets($s,999);


  Daniel Tryba

Re: Help getting a socket to work?

On Fri, 3 Sep 2004 01:42:36 +0000 (UTC), Daniel Tryba

Quoted text here. Click to load it

Yes, I meant *incomplete* messages. In my work with socket
communications, I've always used delimiters at both ends of the
messages. On the receiving end, I then build a string of received data
until I see the termination character at which point I do whatever
processing that needs to be done.


Quoted text here. Click to load it

Thanks for the example.

Do you know if this works in Windows? I have not been able to get it
to work. gives a very similar example but I simply cannot get
it to receive any data. I've added a bunch of error handling - I know
the socket is opening ok and it's sending the request message to my
other process. The other process is sending its response (I've tried
both terminated and non-terminated). No luck.

Any suggestions?


Re: Help getting a socket to work?

Quoted text here. Click to load it

 What does the server do after it's sent "Hello world"?

 Does it follow this with a \n (since you're using the PHP_NORMAL_READ option
which claims to stop reading when it hits \r or \n), or close the connection?

 If it doesn't do either of those, your script is probably just waiting for the
server to send some more data (you asked for 999 bytes).

< Space: disk usage analysis tool

Re: Help getting a socket to work?

Martin wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it
By the way, you can use this construction as well for testing and stuff, as
many of the PHP functions return FALSE when failed.
Very common, easier to maintain too.

$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die
echo "OK <br>";

Re: Help getting a socket to work?

Quoted text here. Click to load it

While this example might (accidentally) work for socket_create. Consider
the following example when trying to handle false.

$hay="foo bar";

    die("Fatal error: '$hay' doens't contain '$needle'");


    die("Fatal error: '$hay' doens't contain '$needle'");

Moral: if you know what type the return value should be, check for type
as well.


  Daniel Tryba

Site Timeline