Developing a server in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Developing a server in PHP

Post 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. :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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? :?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

evilmonkey wrote:Is what I'm doing wrong? :?
Yes! :wink:
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);
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Don't worry evilmonkey, I Just tried to do sockets as well and had 'tcp' as my third parameter. :P
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Haha, good to hear I'm not alone. Thanks. :) The manual is a little misleading there, no?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Check out how others have done it: http://nanoweb.si.kz/
Post Reply