Search code examples
c#phpsocketstcp

Website->Server PHP Socket Refused, but .NET Client Connects


The program that is running on the server is listening for a connection. It uses TcpListener from .NET. Using netstat, the port is listening.

When using a tester data sender, the connection is accepted and the data is processed. To do the same with PHP sockets does not work properly. It returns errno 111.

Looking for an answer amidst some of the PHP-related socket questions, it was said that it could be one of three things: no http server, TCP queue full, or the firewall.

Web server

$port = 6000;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "remote.ip", $port);
socket_send($socket, $data, strlen($data), 1);
socket_close($socket);

Server

tcp = new TcpListener(IPAddress.Any, port);
tcp.Start(backlog);
sock = tcp.AcceptSocket();

Warning: socket_connect(): unable to connect [111]: Connection refused in page.php on line 10

Warning: socket_send(): unable to write to socket [32]: Broken pipe in page.php on line 11


Solution

  • Most of the time the web host was not using appropriate PHP versions, or did not compile with enabling php sockets. I eventually hosted my own website and had access to the console. Compiling with this option, and enabling it in the appropriate php.ini helped.

    This was shown in my comment on the question:

    "Local attempts are working. The main attempts for this question are from a web host, and locally was done using IIS."