fritz
fritz

Reputation: 45

What is a socket bound to in php?

I was just reading the documentation on socket_bind on php.net. It seems unclear to me why you should specify the address to bind the socket to on the server side. Shouldn't this always be the address of the server you use to return the php file itself? What happens if you specify an address that isn't your server's?

It also states that windows may throw an error if the address isn't your machines. Why isn't the default the server address?

In java you just have to specify the port number to bind the socket to on the server side. Why is php different?

Upvotes: 1

Views: 92

Answers (1)

Evert
Evert

Reputation: 99505

While I can't tell you what's going on with Java, I can tell you how it works in PHP and most other platforms.

You specify the address because your machine might have multiple addresses. Each can be bound to specifically, and you can have multiple open sockets on the same port, but different ips. The 0.0.0.0 ip means "everything", and 127.0.0.1 causes your server to only be accessible on your local machine.

Edit: Looks like even java has the means to also specify a specific ip to bind to. http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html#ServerSocket%28int,%20int,%20java.net.InetAddress%29

Upvotes: 1

Related Questions