After forwarding the port I wish to open, I cannot seem to connect or get any data to this port. The PHP CLI software I have written is listening to the port actively on Ubuntu 14.04. Part of the code is shown below. The port is opened as AF_INET, script throws no errors for each line, and /etc/protocols has tcp defined as 'tcp 6 TCP'.

// I have tried $host=gethostbyname(gethostname());$port=8399 (host is
// as well as $host = ''; $port = 8399; and other addresses

$this->wsRead[0] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
socket_set_option($this->wsRead[0], SOL_SOCKET, SO_REUSEADDR, 1)
socket_bind($this->wsRead[0], $host, $port) 
socket_listen($this->wsRead[0], 10)
while (isset($this->wsRead[0])) { /* Handshake/Process data, etc. */ }

The ports are forwarded and my iptables are empty, although I have tried specifically adding an allow rule for the port. I've tried running the script as super user. When the server is listening, netstat -tuln lists this line (with my port 8399), and I notice apache also listed, which works fine and shows as an open port from external port checking websites (on port 8301):

Proto Recv-Q Send-Q Local Address      Foreign Address    State
tcp        0      0*          LISTEN
tcp6       0      0 :::8301            :::*               LISTEN
tcp6       0      0 :::80              :::*               LISTEN

Using nmap the port shows to be open also. I have tried different ports as well. My client side connection script is a JavaScript websocket script which works to connect when running the PHP CLI on Windows. The script is too long to include here, and it does not connect from external network. If I set both JS and PHP scripts to local addresses or 192.168.0.### then it does work but not from a device outside my LAN.

// The JS is basically this, plus some connection events to alert success/fail.
// This external connection request will always time out
Server = new WebSocket('ws://');

Am I missing some firewall in Linux besides iptables that I am not bypassing? Apache is getting the ports open correctly and other software on the PC but it appears my script is missing a necessary step to fully open the port for external use.

tl,dr; Connecting using external IP address using JavaScript client to PHP server working on Windows but not on Ubuntu. Port forwarded, listening, not blocked by iptables, what's left?

  • Can you ping and telnet to that ip/port respectively? – Fabby Nov 1 '15 at 23:46
  • I could not but I have discovered the problem, thank you. – Daryll Nov 2 '15 at 0:07
  • Don't forget to answer your own question then and let us know what the solution was! ;-) – Fabby Nov 2 '15 at 0:10

You are missing something conceptually, and in the code. You have the PHP code listening for connections, but you have to accept a connection before using it.

$ns = socket_accept($this->wsRead[0]);
$some_string = socket_read($ns, 128);

The call to socket_accept() blocks. It will not return until some client program connects to the now-waiting-for-connection program.

A call to socket_accept() returns the actual socket you use to communicate with the remote program. I don't think isset() is really what you want, you should look into socket_select() or something. When you're done communicating with the remote, you call socket_close($ns), and then loop back to call socket_accept($this->wsRead[0]) to wait for another remote program to connect to your program.

When your PHP program is done handling connections, it should call socket_close($this->wsRead[0]) to close the socket, which basically tells the OS kernel to quit listening to incoming TCP SYN packets for the port you specify.


It turns out the problem was multiple things, mostly stemming from my modem/router. I use a Motorola SB6580, which recently had a firmware update, and has not been reboot for 10 days, causing it to get quite slow, deterring me from wanting to make too many changes on it.

The firmware update changed the port forwarding page to include an external remote host address section (defaults to no issue here, but something else they did broke the existing port forwarding rules I had set.

I was using a range of ports to be forwarded which was not working, and by changing this range to 8399 - 8399, it began working again. The restart did not affect it besides increasing the modems speed, nor did changing protocol from Both to TCP.

IP Addr|Start Port|End Port|Remote Host Addr|Start Port|End Port|Protocol|Enabled|8340|    8399||      8340|    8399|    Both|Checked|8399|    8399||      8399|    8399|     TCP|Checked

Also, when using PHP's command gethostbyname(gethostname()) on Linux, the address returned is a local address of, the 2nd entry in /etc/hosts file, which will not work for binding a socket. $_SERVER['REMOTE_ADDR'] also returns a warning: cannot look up host. By manually changing this to the home (subnet?) address (mine is or the IPv4 wildcard, the socket listener is bound correctly and accepts external connections.

My experience with the router and inexperience with Linux made me lean towards the wrong path of troubleshooting Linux. After noticing things weren't working on Windows 7 either, I made the 2 changes above and all now works as it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.