2

I attempted to create a PHP script which determines if the server (the computer which hosts the java program listed below) is running or not, If it is, the php funciton should return true, if not it returns false.

Here is the server:

package darestium.minecraft.server;

import java.io.BufferedReader;
import java.net.*;
import java.io.*;

public class Server {
    private static ServerSocket socket;

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

    private static int port = 4343;

    public static void main(String args[])  {
        System.out.println("Signal 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();
                //response.close();

                System.out.println("Running");
            }
        } catch (IOException e)  {
            System.out.println("Fail!: " + e.toString());
        }

        System.out.println("Closing...");
    }
}

And here is the client:

<?
    function isRunning()  {
        $address = 'darestium.dyndns-free.com';
        $port = 4343;
        $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
        $message = 'loolololol';

        try {
            socket_connect($socket, $address, $port);

            $status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);

            if ($status != false)  {
                return true;
            }

            return false;
        } catch (Exception $e)  {
            return false;
        }
    }
?>

The following are the error messages that show up on the php page that echos out the result of the function like so:

include('server.php');
echo isRunning();

Then the error messages:

Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 9

Notice: Use of undefined constant MSG_EOF - assumed 'MSG_EOF' in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 11

Warning: socket_sendto() expects parameter 4 to be long, string given in C:\Users\darestium\Documents\Portables\xampp\htdocs\darestium\minecraftserver.php on line 11

I was wondering how I could fix this issue. Also, I would like to be able to send messages to the server, any ideas how I could do this? I am basing this off Simple Java TCP Server and PHP Client Problems

Note: that I am very new to Sockets, and Server/Client communication.

Edit:

@VideanuAdrian OK, just port forwarded the port 4343 and it no longer shows up with the first error, but the function always seems to return false, and the last two errors still show.

3
  • do you have any firewall on the machine that runs the java part? or if the java part is hosted behind a router, did you have port forwarding setup ? Commented Apr 25, 2012 at 5:34
  • @VideanuAdrian OK, just port forwarded the port 4343 and it no longer shows up with the first error, but the function always seems to return false, and the last two errors still show. Commented Apr 25, 2012 at 5:49
  • oh, also, the error (the first one) shows up again when the server application is off, and the function still returns false. Commented Apr 25, 2012 at 6:06

2 Answers 2

1

You should not close the DataOutputStream object in your server.

Just comment the line response.close(); in your server and the program should work.

Sign up to request clarification or add additional context in comments.

Comments

0

When running a Client/Server you need to know if the user that runs the app has access to the port. Ports to 1024 are reserved by the system know apps/services. That´s point 1.

Point 2: One of the best ways is to run the server inside a connection-thread approach. Running in this way, when a new client request arrives, the server can delegate the request handle to the connection thread.

Point 3: The message is related to the protocol. If you are using a built protocol like HTTP or FTP, you must use the protocol rules. If not, you could built your own rules for the message request/response.

I recomend you to read the Java Networking Tutorial before continue. Run all Java examples. Later, you could mix with PHP. The more information about sockets and protocols you have, the better your programm will be.

http://docs.oracle.com/javase/tutorial/networking/

Hope this can help you.

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.