dt97868 2012-07-06 22:09
浏览 1153
已采纳

php中的socket_connect上的“连接被拒绝”错误

I am trying to convert some code from perl to php.

Perl code looks like below:

my $handle = Connect($port, $host);

and I am trying to use socket to do the same thing in php. I have tried socket_create and socket_connect, socket_create and socket_bind, and fsocketopen.

As a result, I'm stuck with error messages saying "Connection refused" or "permission denied":

socket_connect() [function.socket-connect]: unable to connect [111]: Connection refused in

I am not sure if this is the problem I need to solve, or the problem of permission because the code in perl works fine (I did not write that code).

my php code looks like below:

$socket =  socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$socket){
        die('Error: socket_create()');
}
if(!socket_connect($socket,$host,$port)) {
        die('Error: socket_connect()');
}

I'm not the one who manages the server, so I will need to ask someone else for the access if it is a permission issue. What should I ask for specifically?

Or should I use some other function to connect to the server? I am new to perl, so I am not sure if socket_connect is the equivalent function to use or not.

Thanks.

  • 写回答

1条回答 默认 最新

  • dtd793353 2012-07-07 01:50
    关注

    If your perl code is able to establish the connection, no additional permissions should be needed to do the same in php. Connection refused means the remote host doesn't let you in (you probably connect to wrong address/port). Permission denied is more surprising, a lot of people have this kind of problem while running httpd scripts with SELinux enabled. If you're one of them, refer to the SELinux manpage:

    SELinux policy can be setup such that httpd scripts are not allowed to connect out to the network. This would prevent a hacker from breaking into you httpd server and attacking other machines. If you need scripts to be able to connect you can set the httpd_can_network_connect boolean on:

    setsebool -P httpd_can_network_connect 1

    I have a few concers to your examples though. Connect from your Perl snippet doesn't seem to be the standard socket connect; I don't know which module it belongs to, but are you sure there is no magic behind the call? As socket_connect takes address in dotted-quad notation (for IPv4), make sure you're not passing a hostname (you would need to make a DNS lookup first). At the very end check if it's really a TCP socket you need, not UDP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值