简体   繁体   English

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

[英]PHP socket Connection refused (Java server)

I have a working Java socket, but I need some help connecting to it with PHP. 我有一个工作的Java套接字,但我需要一些帮助用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. 我的问题:我可以从Java客户端连接到Java套接字并发送/接收消息,但是当我尝试使用PHP连接到同一套接字时,它将无法连接。

This is what I have for the socket in the while loop: (keep in mind this part works) 这就是我在while循环中对套接字所拥有的:(请记住这部分有效)

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: 这就是我尝试与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? 有谁知道我如何诊断为什么PHP无法连接,但Java客户端可以? They are both accessing the socket externally, and since the Java client can connect it's not blocked. 它们都在外部访问套接字,并且由于Java客户端可以连接它,因此它不会被阻止。 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. 我看了几十个有同样问题的人,但没有人提供答案。

Did you look in the php.ini if fsockopen is allowed ? 如果允许使用fsockopen,你看过php.ini吗?

1、php.ini, look for line: disable_functions = fsockopen 2、php.ini, see allow_url_fopen = On or allow_url_fopen = Off 1,php.ini,查找行:disable_functions = fsockopen 2,php.ini,参见allow_url_fopen = On或allow_url_fopen = Off

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM