PHP socket 網路程式設計例項

2022-03-07 07:47:29 字數 3163 閱讀 2510

如果你對socket理解太模糊,可以做做定位請求的專案,以下是我的經驗的結晶,希望對大家有幫助

php**

//確保在連線客戶端時不會超時

set_time_limit(0);

//設定ip和埠號

$address='127.0.0.1';

$port=2009; //除錯的時候,可以多換埠來測試程式!

//建立乙個socket

if(($sock=socket_create(af_inet,sock_stream,sol_tcp))<0)

//繫結到socket埠

if(($ret=socket_bind($sock,$address,$port))<0)

//開始監聽

if(($ret=socket_listen($sock,4))<0)

do //發到客戶端

$msg ="歡迎進入伺服器!

";socket_write($msgsock, $msg, strlen($msg));

echo "讀取客戶端發來的資訊

";$buf = socket_read($msgsock,8192);

$talkback = "收到的資訊:$buf

";echo $talkback;

/* if(socket_write($msgsock, $talkback, strlen($talkback))<0)

else

*///echo $buf;

socket_close($msgsock);

} while (true);

socket_close($sock);

?>

[php] view plaincopyprint?

//確保在連線客戶端時不會超時

set_time_limit(0);

//設定ip和埠號

$address='127.0.0.1';

$port=2009; //除錯的時候,可以多換埠來測試程式!

//建立乙個socket

if(($sock=socket_create(af_inet,sock_stream,sol_tcp))<0)

//繫結到socket埠

if(($ret=socket_bind($sock,$address,$port))<0)

//開始監聽

if(($ret=socket_listen($sock,4))<0)

do //發到客戶端

$msg ="歡迎進入伺服器!

";socket_write($msgsock, $msg, strlen($msg));

echo "讀取客戶端發來的資訊

";$buf = socket_read($msgsock,8192);

$talkback = "收到的資訊:$buf

";echo $talkback;

/* if(socket_write($msgsock, $talkback, strlen($talkback))<0)

else

*///echo $buf;

socket_close($msgsock);

} while (true);

socket_close($sock);

?>

client端

php**

error_reporting(e_all);

set_time_limit(0);

echo "

tcp/ip connection

/n";

$service_port = 2009;

$address = "127.0.0.1";

$socket = socket_create(af_inet, sock_stream, sol_tcp);

if ($socket < 0)

else

echo "試圖連線 '$address' 埠 '$service_port'...

";$result = socket_connect($socket, $address, $service_port);

if ($result < 0)

else

$in = "hello/r/n";

$in .= "我是chester/r/n";

$out = '';

$out1='';

if(!socket_write($socket, $in, strlen($in)))

else

while($out = socket_read($socket, 8192))

echo "關閉socket...

";socket_close($socket);

echo "關閉ok

";?>

[php] view plaincopyprint?

error_reporting(e_all);

set_time_limit(0);

echo "

tcp/ip connection

/n";

$service_port = 2009;

$address = "127.0.0.1";

$socket = socket_create(af_inet, sock_stream, sol_tcp);

if ($socket < 0)

else

echo "試圖連線 '$address' 埠 '$service_port'...

";$result = socket_connect($socket, $address, $service_port);

if ($result < 0)

else

$in = "hello/r/n";

$in .= "我是chester/r/n";

$out = '';

$out1='';

if(!socket_write($socket, $in, strlen($in)))

else

while($out = socket_read($socket, 8192))

echo "關閉socket...

";socket_close($socket);

echo "關閉ok

";?>

先執行server.php

再執行client.php

PHP socket 網路程式設計例項

一開始對socket不太理解,做起東西很模糊,後來做過定位請求的專案,對socket的理解更清楚了。貼一下示例 server端 php 確保在連線客戶端時不會超時 set time limit 0 設定ip和埠號 address 127.0.0.1 port 2009 除錯的時候,可以多換埠來測試程...

PHP socket 網路程式設計例項

一開始對socket不太理解,做起東西很模糊,後來做過定位請求的專案,對socket的理解更清楚了。貼一下示例 server端 確保在連線客戶端時不會超時 set time limit 0 設定ip和埠號 address 127.0.0.1 port 2009 除錯的時候,可以多換埠來測試程式!建立...

PHP SOCKET程式設計!

server.php php 確保在連線客戶端時不會超時 set time limit 0 設定ip和埠號 address 伺服器自己的ip port 埠號 建立乙個socket if sock socket create af inet sock stream sol tcp 0 繫結到socke...