socket_connect(): unable to connect [10061] - target machine actively refused it
13,217
if you are running the client and server codes on the same machin then you have to mix them up like this example below:
<?php
//client
$host = "localhost";
$port = 80;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result1 = socket_connect($socket1, $host, $port) or die("Could not connect to server\n");
//server
// don't timeout!
set_time_limit(100);
// create socket
$socket2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result2 = socket_bind($socket2, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result2 = socket_listen($socket2, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket2) or die("Could not accept incoming connection\n");
// send string to server
socket_write($socket1, $message, strlen($message)) or die("Could not send data to server\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
$result1 = socket_read ($socket1, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result1;
socket_close($spawn);
socket_close($socket2);
socket_close($socket1);
?>
Comments
-
KumarA almost 2 years
I am trying to connect the target written in C using PHP socket_connect() function, but I am getting the following error:
Warning: socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it.
This is the code what I am running:
$service_port = 1234; $address = "xx.xxx.xx.xxx"; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "OK.\n<br>"; } echo "Attempting to connect to '$address' on port '$service_port'...\n"; $result = socket_connect($socket, $address, $service_port); if ($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else { echo "OK.\n<br>"; }
I am using the xampp server with PHP 5.4 in Windows 7 and I have disabled the firewall settings. Still I am getting the same refused error.
Please help me to solve this issue. [I am fighting with this issue for the last 3 days]
Thanks in advance.