socket client
Posted: Sun Mar 22, 2009 3:20 pm
hello, im trying to make a simple php socket client that will send a small string of data to a server
heres what i have
i was able to get things running locally, but im trying to make this work with the php client be run on a remote server, but i keep getting the same issue of getting an instant disconnected message from my server and client times out. i have port 4321 on the router open to the socket server program. my webhost's php info says they allow sockets
heres what i have
Code: Select all
$host = "server ip";
$port = 4321; // nat opened port
$timeout = 6000;
$sk = fsockopen($host,$port,$errnum,$errstr,$timeout);
if (!is_resource($sk) || $errnum) {
exit("connection fail: ".$errnum." ".$errstr);
} else {
stream_set_blocking($sk, false);
fwrite($sk, "hello world");
$dati = "";
while (!feof($sk)) {
$chunk = fgets ($sk, 1024);
if ($chunk !== False) {
$dati.= $chunk;
break;
}
}
echo $dati;
}
fclose($sk);