Member Avatar
KerRoy

Hello,
I try to send a string of numbers (It has to be string) from one pc to the other pc throught tcp and com (serial). I use xampp as my server.
I always get the same errors:
'Warning: fsockopen() [function.fsockopen]: unable to connect to 192.168.50.20:12344 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond...'
or
'Unable to bind ...'

I use this code to sent:

$fp = fsockopen($host, $port, $errno, $errstr, 30);
//$host, $port -> received from the page. host: 192.168.50.20. port: tried many, all failed
	if (!$fp){
      echo "$errstr ($errno)<br />\n";
	}else{
		fwrite($fp, $out);
		echo "success";
	}
</code>
to receive I tried to king: 1)
<code>
	$fp = fsockopen("192.168.50.20", "12123", $errno, $errstr, 30);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
		$stng = fread($fp, 10);
	}
echo "string: ".$stng; //just to display the string and make sure it`s working.
</code>
or (it was taken for example and mudified)
<code>
$host = "192.168.50.20"; / set the variables
$port = 1235;

set_time_limit(0); // don't timeout!

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // create socket

$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // bind socket to port

$result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // start listening for connections

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

// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");

$input = trim($input); // clean up input string
echo "input: ".$input;

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

the port itself is not important, I tried many and the same errors.

Help anyone!

the port itself is not important, I tried many and the same errors.

The port itself is important. The receiving computer must be listening on the port you're trying to send.