dsdukbc60905239 2011-02-08 21:01
浏览 47

too long

First of all, thanks for taking the time to read this. I have a strange problem with PHP sockets. I working on a php socket daemon which works via localhost, but when I try to connect from outside the LAN or another PC, it doesn't work. I've simplified my daemon to a very basic socket connection to replicate the issue for you to see.

Basically, here's the senario. I start the socket daemon on my server on port 6667. I can connect to the daemon via telnet and from the browser on the local machine running the daemon, but I cannot from any other machine - the daemon doesn't even see a connection attempt being made.

To further complicate the issue (which is why I think it's a port forwarding issue), my ISP blocks port 80, so I've setup dyndns and my router to use port 8000. I've also setup my router to forward port 6667 to my server.

To access my daemon from a browser, I enter the following (seudo) url:

http://mydomain.com:8000/client.php

This works from the local machine and will connect, but from any other machine, the daemon doesn't even see a connection attempt being made. However, if I specify the port like this:

http://mydomain.com:6667

my daemon does see a connection being made, but of course then the browser doesn't have a client page loaded that the user can use to interact with the daemon.

My client uses a flash file to create the socket connection (jsocket), but I know it isn't the cross-domain policy file because the policy is correct, and when connecting via localhost, it serves the policy file correctly.

Here's the simplified daemon code:

 <? 

// set some variables 
$host = '0.0.0.0'; 
$port = 6667; 

// don't timeout! 
set_time_limit(0); 

// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
"); 

// bind socket to port 
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
"); 

// start listening for connections 
$result = socket_listen($socket, 3) or die("Could not set up socket listener
"); 

// accept incoming connections 
// spawn another socket to handle communication 
$spawn = socket_accept($socket) or die("Could not accept incoming connection
"); 

// read client input 
$input = socket_read($spawn, 1024) or die("Could not read input
"); 

// clean up input string 
$input = trim($input); 

// echo input back 
$output = $input . "
"; 
socket_write($spawn, $output, strlen ($output)) or die("Could not write output
"); 

// close sockets 
socket_close($spawn); 
socket_close($socket); 
?>

Summary:

I CAN connect from localhost via telnet and browser... I CAN connect from other machines via telnet, but I CAN NOT connection from the browser from other machines using the ip or domain name when port 8000 is specified. The daemon doesn't see any connection attempt. If I specify port 6667, then the daemon see's a connection attempt, but that is useless to the user. :(

Any help in this matter would be greatly appreciated! Thanks!

  • 写回答

2条回答 默认 最新

  • dongtun1872 2011-02-08 21:16
    关注

    You're binding the socket (using socket_bind) onto localhost. Supplying localhost there will have PHP bind the socket to the 127.0.0.1.

    socket_bind is used to bind a socket to a specific interface. Per example:

    socket_bind($socket, '127.0.0.1', 80);
    

    This allows you to connect to 127.0.0.1:80, but not 192.168.1.100:80, even if they are the same machine. The socket is bound to the 127.0.0.1 interface only:

    $ telnet localhost 80
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    $ telnet 192.168.1.100 80
    Trying 192.168.1.100...
    telnet: Unable to connect to remote host: Connection refused
    

    If you want to bind the socket on all available interfaces, use:

    socket_bind($socket, '0.0.0.0', 80);
    

    Then this works:

    $ telnet localhost 80
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    $ telnet 192.168.1.100 80
    Trying 192.168.1.100...
    Connected to 192.168.1.100.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题