php - 为什么 php stream_socket_client 没有连接到端口 80 以外的端口?
问题描述
我有一个服务器托管在ipaddress:8728
. 我可以使用我的 Java 客户端程序完美地访问它的服务。现在我正在尝试从托管在 Web 上的 codeigniter Web 应用程序访问此服务,使用stream_socket_client()
. 但它返回错误 111(拒绝连接)。然后我将我的服务器移动到端口 80( ipaddress:80
) 并且stream_socket_client
程序运行正常。为什么流套接字客户端无法从端口 80 以外的端口上的服务器获取数据,并且当我的 Java 套接字客户端工作正常时。
贝娄是我的 PHP 代码。
public function connect($ip, $login, $password)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$PROTOCOL = ($this->ssl ? 'ssl://' : '' );
$context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
$this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);//this line has exception
if ($this->socket) {
echo "\nSocket is not null";
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=password=' . $password);
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0])) {
if ($RESPONSE[0] == '!done') {
if (!isset($RESPONSE[1])) {
// Login method post-v6.43
$this->connected = true;
break;
} else {
// Login method pre-v6.43
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$this->connected = true;
break;
}
}
}
}
}
}
fclose($this->socket);
}else{
echo "||\nError no = ".$this->error_no."||";
echo "||\n Error Message = ".$this->error_str."||";
echo "||\nsocket is null||";
}
sleep($this->delay);
}
if ($this->connected) {
$this->debug('Connected...');
} else {
$this->debug('Error...');
}
return $this->connected;
}
是因为我的代码中的错误而发生的吗?或托管 Web 应用程序的服务器上的某些保护功能?
解决方案
推荐阅读
- java - 如何为枚举对象编写自定义提供程序?
- reactjs - React + Laravel - 显示 HTML 的 Response.data
- swift - 什么可能导致 SwiftUI 和 NavigationView 出现此动画错误?
- ios - XCDYouTubeClient 不在 Swift 4 中播放视频
- java - In Java elegant way to get each section of path up to Root
- mysql - 如何更改 mysql DB 以将保存的文本转换为 UTF-8 的可读版本
- python - 检查字典字典中是否存在值并获取键?
- java - JSP应用程序中集成vaadin视图的问题
- zapier - 如何在 zapier 的测试用例中检查响应的状态码
- regex - 如何使用正则表达式从 URL 获取 PATH?