Questions about Socket Programming in PHP


#1

Hi everyone,

I have a fairly basic question, but it’s been driving me crazy for about a week now. (Please be nice even if it is a newbie question.)

Before I go into the issue, I want to give the following specifications about my system:

  1. AMD Athlon
  2. Redhat 7.1
  3. I think all ports are closed, except for the HTTP port (80, I think). If I remember correctly, that is the firewall specification for which I opted when I installed the operating system.

Now, for the problem itself. I want to learn socket programming in PHP. I have read about five tutorials so far, and I am very sure that I understand all the basics–what is a socket, how to open a live connection between two computers, etc.

But when I copy the source code in a tutorial, place it in a directory on the remote server that hosts my web site, and try to access the document by typing the URL of the document in the address bar, the document never loads.

Take this tutorial, for example. I copy out the first tutorial, save it as socket_testing.php, upload it to my web host, and then try to access it with my browser. No go, folks! Zilch! Not even when I relax the firewall settings.

I understand that the tutorial talks about saving the file on the local hard drive, but should it not work when the document is saved in any computer in which a server is running?

I know my question is very ill-formed, but I am hoping one of the gurus here can read my mind and help me out before I tear out my hair.

Thanks in advance for your help.

Sincerely,

Reisswolf.


#2

Usually, a socket-based application won’t be started from a web request. Also, if all ports are blocked, you simply won’t be able to connect to the server. If you try to use port 80, it will fail as your webserver is already bound to it.

Socket extension is not enabled by default. Make sure you compile PHP with it.

If you want more details, try running the script in a shell and post the error messages with the code used…


#3

Thanks Bane, for the response.

The thing is, as far as I know, I haven’t enabled the Apache server on my computer. I suppose I could read up the literature and figure out how to start Apache on my computer, but I just figured it would be easier to upload the file to my web host and access it from there.

You say that these socket files cannot be accessed by an HTTP request. Could you please tell me how I can connect from the shell? Should I telnet? Use ssh perhaps? If the file in in a directory called http://www.mydomain.net/Testing, what is the ebst way to connect to it?

Thanks a lot for your help.


#4

For test purpose, use a shell to start the scriipt:

php /path/to/script.php

Telnet is perfect to connect to it until you have a real client application.