0

I am trying to communicate from php to java using sockets. I have the following java code:

private static ServerSocket socket;

private static Socket connection;
private static String command       = new String();
private static String responseStr   = new String();

private static int port = 2500;

public static void main(String args[])  {
    System.out.println("Server is running.");

    try  {
        socket = new ServerSocket(port);

        while (true)  {
            connection = socket.accept();

            InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
            DataOutputStream response = new DataOutputStream(connection.getOutputStream());
            BufferedReader input = new BufferedReader(inputStream);

            command = input.readLine();
            response.writeBytes(responseStr);
            response.flush();
        }
    } catch (IOException e)  {
        System.out.println("Fail!: " + e.toString());
    }
}

I have the following PHP code:

<?php
$socket = stream_socket_server("tcp://192.168.0.10:2500", $errno, $errstr);
if (!$socket) {
    echo "$errstr ($errno)<br />\n";
} else {
    while ($conn = stream_socket_accept($socket)) {
        fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
        fclose($conn);
    }
    fclose($socket);
}

I start the java app, which starts fine, When I run the php, I get the following error:

An attempt was made to access a socket in a way forbidden by its access permissions. (0)

I have searched Google and have tried all the solutions I could find although nothing has worked. I have restarted both machines and disabled the firewall, neither worked.

I am not sure where to go from here.


[update from comment:]

192.168.0.10 is the machine with the java app and web server on it. I am connecting from another machine 192.168.0.7

3
  • 192.168.0.10 is the IP address of which machine? Commented Sep 21, 2014 at 17:26
  • 192.168.0.10 is the machine with the java app and web server on it. I am connecting from another machine 192.168.0.7 Commented Sep 21, 2014 at 17:42
  • This will not solve your current issue, but have you considered using the AMQP messaging protocol to accomplish this task? Commented Sep 21, 2014 at 19:15

1 Answer 1

1

You can only can create a socket on the machine were the code is running on.

So if the PHP code is run on 192.168.0.7, then do:

$socket = stream_socket_server("tcp://192.168.0.7:2500", $errno, $errstr);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.