php利用stream_socket_server与stream_socket_client |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
-
- while(true)
- {
-
- receive_message('127.0.0.1','85',5);
- }
-
- function receive_message($ips教程erver,$portnumber,$nbsecondsidle)
- {
-
- $socket=stream_socket_server('tcp://'.$ipserver.':'.$portnumber, $errno, $errstr);
- if(!$socket)
- {
-
- echo "$errstr ($errno)<br />n";
- }
- else
- {
-
- while($conn=@stream_socket_accept($socket,$nbsecondsidle))
- {
- $message=read($conn,1024);
- echo 'i have received that : '.$message;
- fputs ($conn, "okn");
- fclose ($conn);
- }
- fclose($socket);
- }
- }
-
- ?>
- <?php
-
- send_message('127.0.0.1','85','message to send...');
-
- function send_message($ipserver,$portserver,$message)
- {
- $fp=stream_socket_client("tcp://$ipserver:$portserver", $errno, $errstr);
- if(!$fp)
- {
- echo "erreur : $errno - $errstr<br />n";
- }
- else
- {
- fwrite($fp,"$messagen");
- $response = fread($fp, 4);
- if($response != "okn")
- {
- echo 'the command couldn't be executed...ncause :'.$response;
- }
- else
- {
- echo 'execution successfull...';
- }
- fclose($fp);
- }
- }
|
|
|
|