Helpful Information
 
 
Category: Development Articles
Socket Programming With PHP

It seems that this article is missing a page, the second page in particular. Please let us know when/if this is corrected, I'd love to read this. Thanks

You have 'putting it all together' as your first chapter... surely supposed to come at the end. Bit of an edit run needed here, but great article ;)

In order to try out the examples in the article, I downloaded the latest source (v4.1.1) and tried to compile it using the -enable-php-sockets option. THe compilation failed.

s that the correct option to enable? According to the help for the configure script, it should only be used for testing.

Has anybody managed to run the examples yet?

Oops!!

Correct option is --enable-sockets! The option I was wrongly trying to use was actually --enable-php-streams. I must have misread that through lack of sleep, honest!

Recompiled with --enable-sockets and all is well. <g>

i just tested this with a fresh php4.1.1 install on win2000,
but, alas,
it doesn't work the way its suppost to be.

all it does is accept a single char and then returns it imediatly (with for example the inversed string example).

i also tried the example scripts on php.net:
http://www.php.net/manual/en/html/ref.sockets.html

but with the same result :(

i guess it still needs some work for the win32 platform.

i haven't tried it on linux yet,
because i didnt have the guts yet to upgrade my 4.0.6 install to 4.1.1 which is ofcourse to bad since i had the idea of creating a chat server/client with this socket stuff.

that would be possible, right?

anyway,
still a nice article!

Very nice article, thanks for giving us the insides on PHP socket programming! I never knew you could write server apps in PHP, but I did have a bit of experience with using PHP sockets as a client.

Again, thanks for the info.
I look forward to writing some useful applications for myself.

tried it on my linux box,
exactly the same problem as on win2000.
but with a msg this time:

PHP Warning: UåSè: Unable to initialize module
Module compiled with debug=140, thread-safety=180 module API=1075564500
PHP compiled with debug=0, thread-safety=0 module API=20010901
These options need to match
in Unknown on line 0

any ideas?

Your article is very interesting for a php beginner like me.
Other thing: please, could you to send me the php code for the fonctionnality "Printer Friendly Version" of each page?
Thank you in advance.
Regards
Bernard

Hello GiLuX (it seams that this name looks like a famous french TV showman, isnt'it ?).
I had the same trouble, but I realize that it cames from my windows telnet client and not from the PHP server.
Using another client over which I had a total control (programmed through PHP of course :) everything is going allright.
Hope it helps.
And thanks for PHP Sockets article.

i get the above error message when trying the script. Am using PHP 4.0.6 and Apache 1.3.20 for windows 98. Any help??

Make sure you compile PHP with --enable-sockets, I'm not sure how you'd do this in Win98 w/o Cygwin, but if you are, make sure you do that.

This function is enabled for PHP 4.1.*. So please download and install this version of PHP.

This function is enabled for PHP 4.1.*. So please download and install this version of PHP.

I have been looking for a way to broadcast a message sent by one client to all the other attached clients on seperate spawned sockets. Such as a chat. I'm having a problem detecting and ordering the spawned sockets in a way which will work. I was hopeing the the Socket_Select and the Socket_FD functions would help me to do this - but their is little to no documentation so I'm having some difficulty.

I think the best way to explain would be this code piece. This will broadcast the results to 2 Clients - But it would be ridiculous to do it this way and their's got to be a better method:

<?php
set_time_limit (0);
ob_implicit_flush ();

$address = 'localhost';
$port = 1030;

$sock = socket_create (AF_INET, SOCK_STREAM, 0);
$bind = socket_bind ($sock, $address, $port);
$list = socket_listen ($sock, 5);

$New = socket_accept($sock);
$New2 = socket_accept($sock);

if ($New && $New2) {
broadcast($New,$New2);
}
function broadcast($S1,$S2) {
do {
$buf1 = socket_read($S1, 512);
socket_write ($S1, $buf1, 70);
socket_write ($S2, $buf1, 70);

$buf2 = socket_read($S2, 512);
socket_write ($S1, $buf2, 70);
socket_write ($S2, $buf2, 70);
} while (true);
}
?>

------
Here's a peice of Perl Code that I would like to be able to Mimic:

sub broadcast {
for $eachsocket ($select->handles) {
if ($eachsocket==$listen) {
next;
} else {
$eachsocket->send($_[0]) or do {
&disconnection;
}
}

-----------
Any idea's or any additional reference links for this type of stuff would be great. I wish their where more examples on PHP.net on this stuff.

Thanks.
-Jeff (Jeff@Flash-db.com)
www.flash-db.com

I have the problem that if I want to use socket_create() I get the error message Call to undefined function: socket_create()

strange thing is that I have the 4.0.6 redhat 7.2 rpm of php and sockets are definitely enabled (I checked this with phpinfo())

amy idea?

tnx for the reply,
i tried a few other telnet programs and whatever i do, the problem persists :(

i'm even tried php4.2 rc3 and no difference.

i'm confused.

well, i learnd some more about this problem here:

http://bugs.php.net/bug.php?id=16680










privacy (GDPR)