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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题