PHP是一种通用的开源脚本语言。PHP的独特语法是C,Java,Perl和PHP自己的语法的混合。它可以比CGI或Perl更快地执行动态网页。与其他编程语言相比,用PHP创建的动态页面是通过将程序嵌入HTML文档中来执行的,其执行效率比完全生成HTML标签的CGI更高。它可以嵌入HTML中,特别适合于Web开发。那么php socket如何设置超时?php字符串太长怎么办?中培专家为您详解。
php socket如何设置超时?
//如果$waitAckSec=0,则返回成功发送的字节
//如果$waitAckSec大于0,则返回发送后接收到得内容
//任何情况下,失败都返回FALSE
function sendUdp($host, $port, $buff,$waitAckSec=0) {
$socket = ($result = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP));
//发送超时1秒
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) );
//接收超时6秒
socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>6, "usec"=>0 ) );
if($socket){
$result = @socket_sendto($socket,$buff,strlen($buff),0,$host,$port);
if($waitAckSec>0){
$result = FALSE;
$read = array($socket);
$write = NULL;
$except = NULL;
if(@socket_select($read,$write,$except,$waitAckSec)>0){
$fromHost = "";
$fromPort = 0;
@socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);
$result = phpext_unpack($result);
if($result["needAck"] == 1){
$this->sendUdp($host, $port, $result["ackdata"]);
if(isset ($result['data']['list']) && isset ($result['data']['totalCount'])){
$list = $result['data']['list'];
$count = $result['data']['totalCount'];
while($count> count($list)){
@socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
$result_temp = phpext_unpack($result_temp);
$this->sendUdp($host, $port, $result_temp["ackdata"]);
$list = array_merge($list,$result_temp['data']['list']);
}
$result['data']['list'] = $list;
}
}else{
@socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);
$result = phpext_unpack($result);
if($result["needAck"] == 1){
$this->sendUdp($host, $port, $result["ackdata"]);
if(isset ($result['data']['result']) && isset ($result['data']['userID'])){
$list = $result['data']['list'];
$count = $result['data']['totalCount'];
while($count> count($list)){
@socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
$result_temp = phpext_unpack($result_temp);
$this->sendUdp($host, $port, $result_temp["ackdata"]);
$list = array_merge($list,$result_temp['data']['list']);
}
$result['data']['list'] = $list;
}
}
}
}else{
$result = SEND_UDP_ERROR;
}
}
@socket_close($socket);
}
return $result;
}
php字符串太长怎么办?
php字符串太长的解决办法:首先截取长度等于0或大于等于本字符串的长度,则返回字符串本身;然后如果截取长度为负数,那么截取长度就等于字符串长度减去截取长度;最后如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度。
if (! function_exists('mbSubStr')){
function mbSubStr($str, $length = 0, $append = true)
{
$str = trim($str);
$strlength = strlen($str);
if ($length == 0 || $length >= $strlength) {
return $str; //截取长度等于0或大于等于本字符串的长度,返回字符串本身
}elseif ($length < 0){ //如果截取长度为负数
$length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度
if ($length < 0) {
$length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度
}
}
if (function_exists('mb_substr')){
$newstr = mb_substr($str, 0, $length, 'utf-8');
}elseif (function_exists('iconv_substr')){
$newstr = iconv_substr($str, 0, $length, 'utf-8');
}else{
//$newstr = trim_right(substr($str, 0, $length));
$newstr = substr($str, 0, $length);
}
if ($append && $str != $newstr){
$newstr .= '...';
}
return $newstr;
}
}
以上就是关于php socket如何设置超时,以及php字符串太长怎么办的全部内容,想了解更多关于php的信息,请继续关注中培教育。