PHP Socket Programming : Server not accessible from outside network -
i working on php socket programming project. in project going create service in php socket. socket listen on 1 particular port. , client outside network able communicate on port.
till able create server , client in php socket programming. pc connected lan have use port forward connecting pc outside client. forward port 2000 , communication on port transfer pc ip address. have netgear router n150 wireless adsl .i add configuration on router. test port forwarding online @ site http://www.yougetsignal.com/tools/open-ports/ says port open.
i test code on locally (intranet), working fine. when trying run server on pc , client web server ipage hosting server. throws me error "server not connect server".
server.php
<?php // set variables // lan ip $host = "192.168.0.5"; $port = 2000; // don't timeout! set_time_limit(0); // create socket $socket = socket_create(af_inet, sock_stream, 0) or die("could not create socket\n"); // bind socket port $result = socket_bind($socket, $host, $port) or die("could not bind socket\n"); // start listening connections $result = socket_listen($socket, 3) or die("could not set socket listener\n"); $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"); // clean input string $input = trim($input); echo "client message : " . $input; // reverse client input , send $output = strrev($input) . "\n"; socket_write($spawn, $output, strlen($output)) or die("could not write output\n"); // close sockets socket_close($spawn); socket_close($socket); ?>
client.php
<?php //my public ip $host = "117.223.90.191"; // port on port forword $port = 2000; $message = "hello server"; echo "message server :" . $message; // create socket $socket = socket_create(af_inet, sock_stream, 0) or die("could not create socket\n"); // connect server $result = socket_connect($socket, $host, $port) or die("could not connect server\n"); // send string server socket_write($socket, $message, strlen($message)) or die("could not send data server\n"); // server response $result = socket_read($socket, 1024) or die("could not read server response\n"); echo "reply server :" . $result; // close socket socket_close($socket); ?>
any suggestion problem. think many have same problem me.
even if think problem in lan forwarding, try testing :
telnet 117.223.90.191 2000
another thing try make server listen on interfaces
$host = "0.0.0.0";
and take @ http://reactphp.org/
Comments
Post a Comment