3

I've seen this question asked at least a dozen times. None of the responses helped me.

The code:

$host = "127.0.0.1";
$port = 80;

//no timeout
set_time_limit(0);

//create socket
$socket=socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");


if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($socket));
    exit;
}

$result = socket_bind($socket, $host, $port) or die("Could not bind socket\n");

The 'if' statement was added because one response suggested putting it in there. Didn't seem to do anything for me. I plan to take it out. Anyway, my error is:

Warning: socket_bind(): unable to bind address [10013]: An attempt was made to access a socket in a way forbidden by its access permissions.

And, yes, I get that its supposed to mean the port is in use by another process. I changed the port number about 30 times. I temporarily turned off my (Windows 8) firewall. I ran netstat and I see that these ports don't close so I have like 30 sockets with the same PID and name.

So, my question is: what am I doing wrong?

Steven Detweiler
  • 151
  • 4
  • 17

3 Answers3

2

Thanks to those that responded, but I figured out the problem on my own. The tutorial I used said to navigate to the server.php file. Don't laugh, but I navigated there via the browser. So, that message was popping up on my browser.

I ran the same file in the command prompt and I was either getting that error or it looked like it was hanging there depending on the port I chose. It wasn't crashing... it just had nothing to output. I gave it something to output (yay echo statements!!).

Anyway, thanks again.

Steven Detweiler
  • 151
  • 4
  • 17
1

To bind to a port below 1024 the process needs privileged rights. Typical only root could do this.

For a more detailed discussion in this topic please read here: Is there a way for non-root processes to bind to "privileged" ports on Linux?

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
  • The port number isn't important. Like I said, I tried like 30 ports. I'm pretty sure 80 and 443 are the only two I used under 1024. All the others, I just kinda picked a random number under 65000. I've been keeping my numbers above 1024, but I'm still getting the same error – Steven Detweiler Jun 14 '14 at 14:09
1

Most frequently the reason that you'll get this error is that the socket you're trying to use is already in use by another program. You're trying to open a socket to listen on port 80, what port is apache listening on? If it's also port 80, there's your problem.

Beachhouse
  • 4,972
  • 3
  • 25
  • 39