dongqiangse6623 2016-07-08 06:23
浏览 110

PHP套接字连接被拒绝(Java服务器)

I have a working Java socket, but I need some help connecting to it with PHP.

My problem: I can connect to the Java socket from a Java client and send/receive messages, but when I try to connect to the same socket with PHP, it won't connect.

This is what I have for the socket in the while loop: (keep in mind this part works)

Socket socket = serverSocket.accept();
System.out.println("Got connection");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String cmd = in.readLine();
System.out.println("Received: " + cmd);
String response = "It worked. Received: " + cmd;
out.println(response);
...

And just to show the other half that works, this is the client:

Socket socket = new Socket("<ip>", port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("test msg");
out.flush();
System.out.println("Sent message");
String r = in.readLine();
System.out.println("Response: " + r);

Now for the part that doesn't work.
This is what I am doing to try and connect with PHP:

$s = fsockopen('<ip>', $port, $errno, $errstr, 25);
if (!$s) {
    echo 'Error: '.$errstr;
    die;
}

Running that outputs: "Error: Connection refused"

Does anyone know how I can diagnose why the PHP can't connect but the Java client can? They are both accessing the socket externally, and since the Java client can connect it's not blocked. Is there some protocol I forgot to set?

I've looked at dozens of other people with the same question but nobody has provided an answer.

  • 写回答

1条回答 默认 最新

  • dongxie559554 2016-07-08 07:19
    关注

    Did you look in the php.ini if fsockopen is allowed ?

    1、php.ini, look for line: disable_functions = fsockopen 2、php.ini, see allow_url_fopen = On or allow_url_fopen = Off

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本