Pro.Luv Posted September 11, 2009 Share Posted September 11, 2009 Hi, I read online documentation but I stil can't get this right I need to create a PHP page that will send data to another PHP page using sockets so far I got this: client.php function ping($host) { $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67"; /* create the socket, the last '1' denotes ICMP */ $socket = socket_create(AF_INET, SOCK_RAW, 1); /* set socket receive timeout to 1 second */ socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0)); /* connect to socket */ socket_connect($socket, $host, null); /* record start time */ list($start_usec, $start_sec) = explode(" ", microtime()); $start_time = ((float) $start_usec + (float) $start_sec); socket_send($socket, $package, strlen($package), 0); if(@socket_read($socket, 255)) { list($end_usec, $end_sec) = explode(" ", microtime()); $end_time = ((float) $end_usec + (float) $end_sec); $total_time = $end_time - $start_time; $great = "Working !!"; return $total_time." - ".$great; } else { return false; } socket_close($socket); } ping("127.0.0.1"); when I use the function it passes back to the same page I need it to send to this page server.php error_reporting(E_ALL | E_STRICT); $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind($socket, '127.0.0.1', 1223); $from = "127.0.0.1"; $port = 80; socket_recvfrom($socket, $buf, 12, 0, $from, $port); echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; I really need help with this. Thanks Quote Link to comment Share on other sites More sharing options...
Zyx Posted September 11, 2009 Share Posted September 11, 2009 Your server code is completely wrong. The socket you have created there is not used to communicate with the client, but to listen for the incoming connections. You need two more functions: socket_listen() and socket_accept(). Moreover, the server must be running all the time. When socket_accept() accepts a connection, it creates a new socket to talk to the connected client. As you finish the connection processing, you close it and get back to listening on the main socket. This will be something like that: $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); socket_bind($socket,'127.0.0.1',$port); socket_listen($socket); while(true) { if(is_resource($newSocket = socket_accept($socket))) { // do something with $newSocket socket_close($newSocket); } } socket_close($socket); Quote Link to comment Share on other sites More sharing options...
Pro.Luv Posted September 11, 2009 Author Share Posted September 11, 2009 Thanks alot for the reply, the client.php is right ? is there anything I need to add in there so that I can send data to the server.php ? Really appreciate your help. My server.php now is: $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); socket_bind($socket,'127.0.0.1', $port); socket_listen($socket); while(true) { if(is_resource($newSocket = socket_accept($socket))) { // do something with $newSocket echo $newSocket; socket_close($newSocket); } } socket_close($socket); Quote Link to comment Share on other sites More sharing options...
Zyx Posted September 11, 2009 Share Posted September 11, 2009 The client seems to be all right. In the server code, you should still use socket_recvfrom to receive the data from the client. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.