发送socket协议函数,取得返回值
时间:2010-04-02 来源:xhq6632
<?php
/*
* Author:xhq
* Email:[email protected]
* Oicq:119948717
*/ /*
*发送socket协议函数,取得返回值
*参数:发送的字符串,发送主机,发送端口
*返回值:取得返回字符串
*/
function sendto($msg,$host,$port,$debug=false){
$msg=trim($msg);
$host=trim($host);
$port=trim($port);
if(empty($host) || empty($port)){
if($debug){
die("<br>\nhost or port is empty!<br>\nhost={$host} port={$port}");
}else{
die(0);
}
}
$str="";
$msg.="\n";
$socket = socket_create(AF_INET,SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,$host,$port);
if(!socket_write($socket,$msg)){
$str=-1;
if($debug){
echo "<br>\nsocket fail!";
}
}else{//操作成功
while($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)){
if($debug){
echo("<br>\ncontent=>".$buffer);
}
}
$str=1;
}
return $str;
} ?>
/*
* Author:xhq
* Email:[email protected]
* Oicq:119948717
*/ /*
*发送socket协议函数,取得返回值
*参数:发送的字符串,发送主机,发送端口
*返回值:取得返回字符串
*/
function sendto($msg,$host,$port,$debug=false){
$msg=trim($msg);
$host=trim($host);
$port=trim($port);
if(empty($host) || empty($port)){
if($debug){
die("<br>\nhost or port is empty!<br>\nhost={$host} port={$port}");
}else{
die(0);
}
}
$str="";
$msg.="\n";
$socket = socket_create(AF_INET,SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,$host,$port);
if(!socket_write($socket,$msg)){
$str=-1;
if($debug){
echo "<br>\nsocket fail!";
}
}else{//操作成功
while($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)){
if($debug){
echo("<br>\ncontent=>".$buffer);
}
}
$str=1;
}
return $str;
} ?>
相关阅读 更多 +