简体   繁体   中英

PHP socket Connection refused (Java server)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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