0

I have written some code in PHP to create a socket and listen for incoming connections. I am trying to do this using a specific port I'm interested in.

Everything is going well, except it opens the listening TCP socket on some random port, instead of the one chosen by me.

I'm running this code on Google Compute Engine (GCE).

Here is the exact php code that I tried:

    set_time_limit(0);
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
    $address = '35.190.179.17';
    //bind socket to specified host
    socket_bind($socket, $address,54468);
    //listen to port
    socket_listen($socket);
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
Chandrapal Yadav
  • 277
  • 1
  • 3
  • 9
  • When you open a listening socket, that listening socket is on a specific port. When someone connects, their connection is shifted/delegated to some other port so that the listening socket can continue to listen for new connections. Think of the listening socket as an operator -- it has to keep the line open for more connections. – S. Imp Jun 21 '17 at 01:32
  • I am not even able to connect through telnet using the specified port, I actually find the open port using netstat -l and connect to that port using telnet. – Chandrapal Yadav Jun 21 '17 at 05:13
  • You should try adding error checks to your code like the examples at php.net. You may find that any one of your socket_x commands fails. http://php.net/socket_set_option – S. Imp Jun 21 '17 at 16:32

1 Answers1

0

I got the issue resolved by using the internal IP instead of external IP address and opening the port for external connections.

Compute engine was not allowing socket bind using external IP address.

Chandrapal Yadav
  • 277
  • 1
  • 3
  • 9