2

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.

KumarA
  • 1,368
  • 3
  • 18
  • 41
  • What is your $address? Is the $service_port allowed by the firewall in the $address machine? – srain Jul 17 '13 at 10:54
  • yes. My $service port is allowed to $address. If I am checking with Visual Studio 2010 - Windows application, my C based target is communicating with the client, but not with PHP socket connection. – KumarA Jul 17 '13 at 12:41
  • @Kumar Have you tried reinstalling WAMP? –  Jul 17 '13 at 12:44
  • check this. [link](http://stackoverflow.com/questions/9695224/no-connection-could-be-made-because-the-target-machine-actively-refused-it-127-0) – srain Jul 17 '13 at 13:10

3 Answers3

1

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);
?>
Ramin Rabii
  • 359
  • 4
  • 14
0

hope the below link helps. checkout the solution. http://www.codeproject.com/Questions/426966/System-Net-Sockets-SocketExcep

p1nkrock
  • 1,786
  • 1
  • 10
  • 7
  • Thanks for reply, but this link is not working for me.(I am trying to make TCP connection) – KumarA Jul 17 '13 at 12:53
  • Please check your proxy settings if you are accessing the link from your office. The link on the answer posted is working for me. – p1nkrock Jan 23 '14 at 09:12
0

I realize this is an old post, however, I recently had this problem using php running under IIS 7 and was unable to find a solution after many google searches.

Many sites suggested enabling sockets by changing the php.ini file (then stopping and restarting IIS).

I found I had to enable sockets within IIS 7 Manager to get sockets to work.

I found simply changing the php.ini file manually did not enable sockets within php (along with IIS 7). My sample code below displays phpinfo(); if sockets are enabled there will be a specific section named Sockets and it will show itself enabled.

IIS Manager 7 --> Scroll down to PHP Manager and select --> scroll down to PHP Extensions and select the option to enable/disable an extension --> enable sockets if not already enabled --> restart IIS.

I was running a desktop socket server (that I wrote - using port 9999) and was able to connect to my server using a desktop client I wrote (for testing, running on same Windows 7 PC) but was unable to connect using php running on the same PC.

My php code to test the connection looks like this:

<?php
echo "hello<br><br>";
echo phpinfo();
$fp = stream_socket_client("tcp://127.0.0.1:9999", $errno, $errstr, 30);
if (!$fp) {
   echo "error<br><br>";
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "GET / HTTP/1.0\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n"); //just returns an acknowledgement
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}
?>

I hope this helps someone.

Ken

Ken_sf
  • 89
  • 1
  • 9