3

I have a chatdemo.php which includes socket.php socket file. Inside the socket.php file there is a line

socket_bind($master, $address, $port);

where $address = "localhost" and $port=1300.

However, when I put onto the browser (and the apache from XAMPP is up) http://localhost/demo/chatdemo.php it says:

Warning: socket_bind() [function.socket-bind]: unable to bind address [48]:
Address already in use in /Applications/XAMPP/xamppfiles/htdocs/demo/socket.class.php on line 23
socket_bind() failed

So, instead, I goto the command line and did a

php -q /demo/chatbot.demo.php
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /demo/socket.php on line 21
Server Started : 2012-03-27 17:24:24
Listening on   : localhost port 13000
Master socket  : Resource id #5

So the question is: Why can't I run that chatdemo.php on my browser on localhost(using XAMPP), whereas I could executed that on the command line using php(non-XAMPP)?

2
  • Why would you run a chat daemon through a web server? Commented Mar 28, 2012 at 0:38
  • Chatdemo.php is Not a daemon, it's an app but it needs the daemon socket.php running though. Commented Mar 28, 2012 at 0:54

1 Answer 1

1

You shouldn't even try to run a server (that listens to a port and accepts connection) by a web server. A web server, processes a request and invokes your PHP script as necessary. Your script has 30 seconds time to finish it. This is the expected behavior. But when you run a server. The story is different. It runs 24/7. Think about web server, it runs 24/7. Of course your server can have much less run time than this. But if you run it under a web server 1 of the thread will be blocked for long time for every request.

Servers should be run stand alone. In your case it should run from command line.

Farther reading.

  1. Client-server architecture
  2. Socket states and the client-server model

For the warning at the command line just change the date.timezone settings in php.ini according to your location. I use

date.timezone = "Asia/Dhaka"  
Sign up to request clarification or add additional context in comments.

7 Comments

how do you run the chatdemo.php app on the command line? I don't see any graphical output. If i did php -q chatdemo.php it just says it's up and running but I don't see any output.
Its a daemon/server. it should be running. There must be client available to communicate with it. Client should be communication with the server over 13000 port.
@lilzz if you're on a linux box, try netstat -nplnt. That would list all the listening connections, you should see one showing the 13000 port.
OK, I understand the Daemon Socket.php should be running on command line. But my question is the client php and Javascript should be running on webbrowser right?
And if I run the client php/javascript on webbrowser then it has error because it has the include statement of the Daemon socket.php.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.