Page 1 of 2
Developing a server in PHP
Posted: Wed Jul 14, 2004 7:38 pm
by evilmonkey
Hello. I have decided to make a small chat server in php. My only problem is, I have no idea where to start. I know I need to open a socket, bind it to the local computer, have it listen for incoming messages, and send those messages back to the client. I got the first two down:
Code: Select all
<?php
//this will be a chat server
$hostname = "localhost";
$port = 8080;
$socket = socket_create(AF_INET, SOCK_STREAM, tcp); //create a socket
socket_bind($socket, $hostname, $port); //connect it to a computer
?>
My problem comes when I start doing the last two, because I have no idea what I'm doing.
I'm not asking anyone to write this code for me, I want to learn for myself, but if someone could give me a pointer, an explanation, a function, or a link to a tutorial, I'll really appreciate it.
Another thing: I try running the above code on my windows box inside Zend, and it tells me that the functions are undefined. I'm running php 4.3.4. Help?
Thanks.

Posted: Wed Jul 14, 2004 8:01 pm
by redmonkey
Code: Select all
socket_listen($socket); // listen for any incoming connections
while($connection = socket_accept($socket)) // accept any incoming connection and write to the socket.
{
socket_write($connection,'You''re connected!!\r\n');
}
You would need to enable the sockets module through php.ini in order to use them.
I would not recommend developing this for a Windows platform as PHP for the win32 environment does not support process forking, therefore this kind of thing will always be faster running on a *nix box.
Posted: Wed Jul 14, 2004 8:38 pm
by evilmonkey
How can it recieve messages though? Wouldn't what's coming through the socket need to be placed in a variable? or is that $connection?
Thanks.

Posted: Wed Jul 14, 2004 8:43 pm
by redmonkey
Posted: Wed Jul 14, 2004 9:04 pm
by evilmonkey
Code: Select all
<?php
//this will be a chat server
$hostname = "localhost";
$port = 8080;
$socket = socket_create(AF_INET, SOL_TCP, 'tcp'); //create a socket
socket_bind($socket, $hostname, $port); //connect it to a computer
socket_listen($socket); // listen for any incoming connections
while($connection = socket_accept($socket)) // accept any incoming connection and write to the socket.
{
socket_write($connection,'You''re connected!!\r\n');
}
?>
this code produces these errors:
Warning: socket_create() expects parameter 3 to be long, string given in PHPDocument1 on line 5
Warning: socket_bind() expects parameter 1 to be resource, null given in PHPDocument1 on line 6
Warning: socket_listen() expects parameter 1 to be resource, null given in PHPDocument1 on line 7
Warning: socket_accept() expects parameter 1 to be resource, null given in PHPDocument1 on line 8
Is what I'm doing wrong?

Posted: Wed Jul 14, 2004 9:10 pm
by redmonkey
evilmonkey wrote:Is what I'm doing wrong?

Yes!
evilmonkey wrote:Code: Select all
$socket = socket_create(AF_INET, SOL_TCP, 'tcp');
Try....
Code: Select all
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
Posted: Wed Jul 14, 2004 9:16 pm
by evilmonkey
I just changed that as you relied, looked it up in the manual...it's pretty unclear there, but there was a user made example that helped out. Thanks.

Posted: Wed Jul 14, 2004 9:29 pm
by evilmonkey
Another question, would I need to set the socket timeout to some unreasonable value because I want this server to run until the user exits it. Here's my server code so far, would this read a message and write the message back?
Code: Select all
<?php
$hostname = "localhost";
$port = 8080;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //create a socket
print $socket;
socket_bind($socket, $hostname, $port); //connect it to a computer
socket_listen($socket); // listen for any incoming connections
while($connection = socket_accept($socket)) // accept any incoming connection and write to the socket.
{
while ($message = socket_read($socket, PHP_NORMAL_READ)){ //read any incoming messages
socket_write($socket, $message); //write them back to the client
}
}
?>
Thanks.

Posted: Wed Jul 14, 2004 9:31 pm
by d3ad1ysp0rk
Don't worry evilmonkey, I
Just tried to do sockets as well and had 'tcp' as my third parameter.

Posted: Wed Jul 14, 2004 9:32 pm
by evilmonkey
Haha, good to hear I'm not alone. Thanks.

The manual is a little misleading there, no?
Posted: Wed Jul 14, 2004 9:40 pm
by redmonkey
The server should run indefinately and should not terminate when the client exits so run your server from the PHP-CLI interface.
Essentially your code should work but it gets a bit more complicated than what you have.
How do you intend to implement the client side?
Posted: Wed Jul 14, 2004 9:43 pm
by evilmonkey
For the client side, I plan to try something similar: connect to the server (socket_connect() I guess), and do the same thing: read in a loop, and the write in a loop. I want to make GUI for it too with php-gtk, ust for the client though.
Posted: Wed Jul 14, 2004 10:13 pm
by redmonkey
I had a play with php-gtk and wrote a few small apps but I just don't like gtk so I never really went any further with it.
Have you considered how you will handle multiple simultaneous requests?
Posted: Thu Jul 15, 2004 8:34 am
by evilmonkey
Doesn't the loop already take care of that? All this does is recieve a message, send a message. It's nt to one person, it's to everyone, like a chat room. Because this is TCP that lines up the packages, shouldn't mmultiple requests not be a problem?
Posted: Thu Jul 15, 2004 9:16 am
by kettle_drum
Check out how others have done it:
http://nanoweb.si.kz/