PHP fsockopen()失败不打开端口,但telnet工作
在过去的两天里,我一直在努力解决一个奇怪的网络问题。我无法在php网页上打开端口25003。代码似乎不是问题,但是,它如下所示PHP fsockopen()失败不打开端口,但telnet工作,php,sockets,serial-port,Php,Sockets,Serial Port,在过去的两天里,我一直在努力解决一个奇怪的网络问题。我无法在php网页上打开端口25003。代码似乎不是问题,但是,它如下所示 $host='xx.xx.xx.xx';//我的isp分配的statis公共ip地址 $ports=阵列(8025003); foreach($port作为$port) { $connection=fsockopen($host,$port); 如果(是_资源($connection)) { 回显“.$host.”:“.$port.”(“.getservbyport(
$host='xx.xx.xx.xx';//我的isp分配的statis公共ip地址
$ports=阵列(8025003);
foreach($port作为$port)
{
$connection=fsockopen($host,$port);
如果(是_资源($connection))
{
回显“.$host.”:“.$port.”(“.getservbyport($port,'tcp'))已打开。“。”\n”;
fclose($连接);
}
其他的
{
回显“.$host.”:“.$port.”未响应。“。\n”;
}
}
就像我之前提到的-之前我尝试连接所有尝试都失败了,但下面两种方法现在都可以工作了,所以我假设问题已经解决。我可能建议重新启动Web服务器和浏览器
似乎对我有效(使用上一个问题中的ip)@RamRaider有任何提示,问题可能在哪里吗?没有-我以前尝试过,所有连接尝试都被拒绝(无法连接等),但现在使用上述方法和以前的尝试(socket\u create、socket\u connect等)似乎ok@RamRaider我是否应该假设问题在于Bluehost?因为如果它和你一起工作,它也应该和我一起工作,不是吗?谢谢,这意味着问题在于bluehost。当联系bluehost时,他们拒绝重新启动Web服务器,因为我在共享主机中。还有什么特别的问题我必须问他们,因为sockets不仅仅在buehost中工作。请尝试在您的本地/Dev Web服务器上运行上述代码(使用正确的ip)…它对您有效吗?使用上述fsockopen代码,我现在可以成功地获得数据,但我想知道我是否可以在我的网页上实时获得重量闪烁,因为它在称重指示器中发生变化。可能吗?有什么提示吗,我该怎么做?顺便说一句,非常感谢你的回答,这让我相信问题出在Bluehost上,尽管直到现在还没有解决,因为他们说他们正在努力解决这个问题。它在我的本地服务器和另一个A2托管站点上运行得非常好。
<?php
set_time_limit( 0 );
error_reporting( E_ALL );
$address = 'xxx.xxx.xxx.xxx';
$port = 25003;
/* Method #1 */
$fp = fsockopen( "tcp://{$address}", 25003, $errno, $errstr );
if( !$fp ) echo "$errstr ($errno)<br />\n";
else {
if( !feof( $fp ) ) {
$weight = trim(fgets($fp, 64)," ");
}
}
printf('<h1>Method #1</h1>Weight: %s<br /><br />',$weight);
fclose($fp);
/* Method #2 */
function geterror(){
$obj=new stdClass;
$obj->code=socket_last_error();
$obj->error=socket_strerror( $obj->code );
return $obj;
}
function negotiate( $socket ) {
socket_recv( $socket, $buffer, 1024, 0 );
for( $chr = 0; $chr < strlen( $buffer ); $chr++ ) {
if( $buffer[ $chr ] == chr( 255 ) ) {
$send = ( isset( $send ) ? $send . $buffer[$chr] : $buffer[$chr] );
$chr++;
if( in_array($buffer[$chr], array(chr(251), chr(252))) ) $send .= chr(254);
if( in_array($buffer[$chr], array(chr(253), chr(254))) ) $send .= chr(252);
$chr++;
$send .= $buffer[$chr];
} else {
break;
}
}
if( isset($send)) socket_send($socket, $send, strlen($send), 0);
if( $chr - 1 < strlen($buffer)) return substr($buffer, $chr);
}
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
try{
if( $socket && is_resource( $socket ) ){
if( !socket_connect( $socket, $address, $port ) ){
$err=geterror();
throw new Exception( sprintf( 'socket_connect: %s', $err->error ), $err->code );
} else {
while( true ){
$e=null;
$w=null;
$r = array( $socket );
$c = socket_select( $r, $w, $e, 5 );
foreach( $r as $read_socket ) {
if( $r = negotiate( $read_socket ) ) {
exit( sprintf( '<h1>Method #2</h1>Reading: %s', print_r( $r, true ) ) );
}
}
}
}
} else {
$err=geterror();
throw new Exception( sprintf( 'Failed to create socket: %s',$err->error ), $err->code );
}
}catch( Exception $e ){
printf( "
<pre>
<h1>Error: %d</h1>\nMessage: %s\nTrace: %s\nLine: %d
</pre>",$e->getCode(),$e->getMessage(),$e->getTraceAsString(),$e->getLine() );
}
socket_close( $socket );
?>