Title: How to specify the port that the client side of socket in PHP?
Tags: php;sockets
Question: I want to bind a specified port to the CLIENT side of socket in PHP. Can I do this? How to do this? 
Using socket_bind()? but I dont know what should be set for the address

I have googled the solution about this question but no example/solution can be found.

Thanks for helping.

Comment: @lanzz He doesn't want to listen. He said 'client socket'. 267-633-4682 therefore doesn't apply either.

Here is the accepted answer: Look here: http://php.net/manual/en/function.socket-bind.php

```socket_bind ( resource $socket , string $address [, int $port = 0 ] )
```

So something like:

```socket_bind($socket, 'localhost', 5555); 
```

Of course, I don't know what the address of the host is, it might not be localhost.

Comment for this answer: I have read the php manual.
socket_bind is usually for SERVER side.
For CLIENT side, is the address the IP of client? I just want to make sure what should the address is.
Comment for this answer: I am a beginner in socket programming. Maybe I have some concept about socket not clear. As described as this picture http://pic.pimg.tw/kezeodsnx/4a83b89243c39.png
The server side bind to a ip and port so that it can send and receive data to and from client respectively. The ip of server is likely be fixed because all its client needs to connect only one server(in normal cases). But for client, if I want to specify a port in client side, I guess I should use socket_bind(), which requires 2 parameters ip and port. What is the address of ip in the this socket_bind()?Is the ip address of client?
Comment for this answer: But if I have hundreds of clients, then I needs to know the ip of hundreds of clients(I know this can done by some php functions). I am not sure what I thought is true or not so I just come here to understand more about the socket_bind() for client side
(I will send the php client code to the clients so the code should be finished before sending the code and should not need to be changed for each clients)
Comment for this answer: It can (and should) be used by both SERVER and CLIENT side. How do you create the socket, what is the thing you call "server". How do you want to communicate with that socket. Elaborate if you need better answer.
Comment for this answer: On both the server and client you should bind the socket to same ip/port. If the server and client are on the same machine - it would be "267-633-4682", if it's different, you maybe should bind it to the public ip of the server. This is called TCP socket, there are also UNIX sockets. Sockets allow duplex communication between two processes - if you need multiple clients bound to the same socket, you actually need a socket server that will handle every new connection separately.

Here is another answer: You can do it if you create the stream yourself with ```stream_context_create```

You will need to use bindto:

```
  Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port. Setting the IP or the port to 0 will let the system choose the IP and/or port.
```

So:

```$ctx = stream_context_create(array('socket'=&gt; array('bindto' =&gt; "0:1234")));
```

Then use the context as you see fit.

Comment for this answer: Can I use this with socket created by php socket functions i.e. socket_create()?
Comment for this answer: Not socket_create, but functions such as `stream_socket_*` or `fopen`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:09/18/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><sockets>', 'Title': 'How to specify the port that the client side of socket in PHP?', 'ViewCount': 1078, 'id': 12479452, 'user_AboutMe': '', 'user_AccountId': 1848437.0, 'user_DisplayName': 'Timespace', 'user_Reputation': 4481.0, 'user_Views': 99.0, 'user_id': 1675581.0}}