1

I'm attempting to set up a web-socket connection between a java script client and a php server. However, whenever I try to connect I get the error "connection refused".

I am already able to create a web-socket connection between two php scripts which makes me think the server side code is correct.

Please explain how I can fix this problem.

Server (PHP):

$host = "108.167.140.91";
$port = 5353;
set_time_limit(30);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 20);

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

socket_write($spawn, "connected", strlen ("connected"));

// close sockets
socket_close($spawn);
socket_close($socket);

Client (HTML):

<html>
<script>
var socket = new WebSocket("ws://108.167.140.91:5353");

// Open the socket
socket.onopen = function(event) {
    console.log("connected");

    // To close the socket....
    socket.close()

};
</script>
</html>

You can test it for yourselves here:

Server : http://chrislanggames.site/SocketTest/server3.php

Client : http://chrislanggames.site/SocketTest/client2.html

Working PHP client : http://chrislanggames.site/SocketTest/client.php

C. Lang
  • 463
  • 4
  • 12
  • Does your PHP server allow connections via port 5353 from outside the server. – Nigel Ren Oct 19 '19 at 14:25
  • I'm not sure. I have tried multiple different ports. How can I check if connections are allowed? – C. Lang Oct 19 '19 at 14:28
  • 1
    Try using telnet to see if it will connect `telnet 108.167.140.91 5353` – Nigel Ren Oct 19 '19 at 14:33
  • See here: https://stackoverflow.com/questions/2333400/what-can-be-the-reasons-of-connection-refused-errors – Darren Smith Oct 19 '19 at 14:46
  • Telnet could not open the connection. Could this have something to do with the firewall on my server? If so how can I white list the port? – C. Lang Oct 19 '19 at 15:59
  • I found the issue! The problem was that my server was blocking the port. However, as I am on shared hosting I was unable to open the port :(. – C. Lang Nov 17 '19 at 20:14

1 Answers1

0

Try changing the host to "0.0.0.0" .... that means it can accept socket connections requests from any host.

Darren Smith
  • 2,261
  • 16
  • 16
  • 1
    I this just changing the host variable in the server to "0.0.0.0"? Because if it is, it still doesn't work. Just to clarify, the client code should remain the same. Right? – C. Lang Oct 19 '19 at 14:23
  • Client code should remain the same. Sometimes if the server is listening on a specific IP, it will restrict the socket connections it can accept. Hence switching to the 0.0.0.0 .... which mean accept from any host. But is that is not fixing your problem, can be another problem. – Darren Smith Oct 19 '19 at 14:37