PHP下socket编程
下面是一些簡(jiǎn)單的例子,在命令行運(yùn)行php腳本就行
[命令行運(yùn)行PHP]PHP中有一個(gè)php.exe文件,可以用命令執(zhí)行PHP腳本。如:D:/php.exe -f F:/test.php ; 可以使用php.exe -h查看更多參數(shù)?:
服務(wù)器端:
<?php
/**
?* 服務(wù)器端代碼
?*
?*/
//確保在連接客戶端時(shí)不會(huì)超時(shí)
set_time_limit(0);
//設(shè)置IP和端口號(hào)
$address = "localhost";
$port = 1234; //調(diào)試的時(shí)候,可以多換端口來(lái)測(cè)試程序!
//創(chuàng)建一個(gè)SOCKET
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)
{
? ? echo "socket_create() 失敗的原因是:" . socket_strerror(socket_last_error()) . "/n";
? ? die;
}
//阻塞模式
if (socket_set_block($sock) == false)
{
? ? echo "socket_set_block() 失敗的原因是:" . socket_strerror(socket_last_error()) . "/n";
? ? die;
}
//綁定到socket端口
if (socket_bind($sock, $address, $port) == false)
{
? ? echo "socket_bind() 失敗的原因是:" . socket_strerror(socket_last_error()) . "/n";
? ? die;
}
//開始監(jiān)聽
if (socket_listen($sock, 4) == false)
{
? ? echo "socket_listen() 失敗的原因是:" . socket_strerror(socket_last_error()) . "/n";
? ? die;
}
do
{
? ? if (($msgsock = socket_accept($sock)) === false)
? ? {
? ? ? ? echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n";
? ? ? ? die;
? ? }
? ? //發(fā)到客戶端
? ? $msg = "welcome /n";
? ? if (socket_write($msgsock, $msg, strlen($msg)) === false)
? ? {
? ? ? ? echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n";
? ? ? ? die;
? ? }
? ? echo "讀取客戶端發(fā)來(lái)的信息/n";
? ? $buf = socket_read($msgsock, 8192);
? ? echo "收到的信息: $buf ? /n";
? ?
? ? socket_close($msgsock);
} while (true);
socket_close($sock);
?>
客服端
<?php
/**
?* 客戶端代碼
?*/
?
error_reporting(0);
set_time_limit(0);
echo " TCP/IP Connection /n";
$service_port = 10001;
$address = '127.0.0.1';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
{
? ? die;
}
else
{
? ? echo "OK";
}
echo "試圖連接 ";
if (socket_connect($socket, $address, $service_port) == false)
{
? ? $error = socket_strerror(socket_last_error());
? ? echo "socket_connect() failed./n","Reason: {$error} /n";
? ? die;
}
else
{
? ? echo "連接OK/n";
}
$in ? = "Hello World/r/n";
if (socket_write($socket, $in, strlen($in)) === false)
{
? ? echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n";
? ? die;
}
else
{
? ? echo "發(fā)送到服務(wù)器信息成功!/n","發(fā)送的內(nèi)容為: $in ?/n";
}
$out ?= "";
while ($out = socket_read($socket, 8192))
{
? ? echo "接受的內(nèi)容為: ".$out;
}
echo "關(guān)閉SOCKET…/n";
socket_close($socket);
echo "關(guān)閉OK/n";
?>
?
?
?
下面是我自己的代碼:這是一個(gè)PHP客戶端測(cè)試程序,目的是與服務(wù)器連接,將一個(gè)包體中的內(nèi)容發(fā)給服務(wù)器,服務(wù)器查詢數(shù)據(jù)庫(kù),然后將結(jié)果返還給客戶端
<?????????????????????????????????????????????????????????????????????????
$clientfd = socket_create(AF_INET, SOCK_STREAM, 0);
if($clientfd == -1)
{
??? printf("failed to create socket!\n");
}
$server_port = 63214;
$server_addr = "10.1.1.65";
if((socket_connect($clientfd, $server_addr, $server_port)) === false)
{
??? echo "failed to connect!\n";
??? die;
}
else
{
??? echo "succeed to connect!\n";
}
$pkg_len = 18;
$cmd_id? = 1;
$user_id =100;
$buf = pack("LLSLL", $pkg_len, 0, $cmd_id, 0, $user_id);
if(socket_write($clientfd, $buf, strlen($buf)) === false)
{
??? echo "failed to send!\n";
}
else
{
??? echo "succeed to send message to the server!\n";
??? if(($result = socket_read($clientfd, 63214)))
??? {
??????? $res = unpack($result);
??????? echo "succeed to receive data from the server:$server_addr\n";--
??????? echo "the result is :$res[1]\n";
??? }
??? else
??? {
??????? echo "failed to receive data from the server:$server_addr\n";
??? }
}
?
總結(jié)
以上是生活随笔為你收集整理的PHP下socket编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下c调试
- 下一篇: linux下makefile