您的位置:首页 > 编程语言 > PHP开发

PHP socket 连接 Socket server

2009-05-03 22:30 501 查看

socket_send

(PHP 4 >= 4.1.0, PHP 5)

socket_send -- Sends data to a connected socket

Description

int socket_send ( resource socket, string buf, int len, int flags)

警告
本扩展模块是实验性的。该模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。我们提醒您在使用本扩展模块的同时自担风险。

The function socket_send() sends len bytes to the socket socket from buf

The value of flags can be any ORed combination of the following:

表格 1. possible values for flags

0x1Process OOB (out-of-band) data
0x2Peek at incoming message
0x4Bypass routing, use direct interface
0x8Data completes record
0x100Data completes transaction
See also socket_sendmsg() and socket_sendto().

<?

// set some variables
$host = "10.1.0.3";
$port = 5038;

// don't timeout!
set_time_limit(0);

// create socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol) or die("Could not create
socket/n");
$connection=socket_connect($socket,$host,$port);
if (!$connection)
{
echo "Can't Connect to Asterisk Manager Port .";
}
else
{
echo "Connect to Asterisk Manager Successfully.";
}

$Socket_logintext="Action: login/r/nUsername: cron/r/nSecret: 1234/r/nEvents: ON/r/n/r/n";
socket_send($socket,$Socket_logintext,strlen($Socket_logintext),0x4);
//socket_write($socket,$Socket_logintext,strlen($Socket_logintext));
/*
while ($buffer=socket_read($socket,1024,PHP_NORMAL_READ))
{
if($buffer=="NO DATA");
{
echo ("<p>NO DATA</p>");
continue;
}
else
{
echo ("<p>Buffer Data: ".$buffer."</p>");
}
}
*/
socket_close($socket);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: