0

I want to implement a java server and a php client. Every time a click is done on the website (php client) a action should be executed on the server.

Actually my server looks like:

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();

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

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

My HTML site looks like:

<?php
if (isset($_POST["Btn1"])){
    $address = "localhost";
    $port = 4343;
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $message = 'blablabla';

        socket_connect($socket, $address, $port);
        socket_sendto($socket, $message, strlen($message), 0, $address, $port);
    };
?>

<html>
<head>
    <title>Test</title>
</head>
<body>

    <form id="form1" method="POST">
        <button type="submit" form="form1" id="Btn1" name="Btn1" value="Btn1" title="Btn1">
        Btn1</button>
    </form>
</body>
</html>

My problem is, whats the best way to delegate the actions to the server. A little example, I have a method on my java server which posts "Hello" to the console. Now I click on a button on my website and this method should be executed. What is the best way to do?

Can I use my approach or is there a better one?

11
  • correct me if I'm wrong but PHP is server side. Things like HTML and JavaScript are client side. Maybe you want to change that Commented Sep 1, 2014 at 8:44
  • at the end of the day, even PHP is running on server, but yes it's considered a CLIENT in perspective to Java server. @Kevin this code should connect on page load (php page), and send the message to the Java server, so is it success to this point? if yes we can go th the Button click thing. Commented Sep 1, 2014 at 8:53
  • yes, on page load there is a message sent to the java server. My idea was, every button gets a specific message which is send to the server. On the server I have a switch case and then the specified method is executed. I don't know whether this is a good approach? Commented Sep 1, 2014 at 9:39
  • ok, now you can place buttons inside html <form> and use php if(isset($_POST['btn1'])){} based on that you can run the socket code and send appropriate message. Commented Sep 2, 2014 at 13:20
  • @Yazan I updated my question with what I have done. Commented Sep 3, 2014 at 9:46

1 Answer 1

2

PHP runs server-side, so you cannot execute PHP code on the client's web page...

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

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
With less then 50 reputation (at the time of posting) I do not see a chance to leave a comment. Plus, I don't see how clarifying that PHP is a server-side technology and NOT a technology to be executed in a client's browser is "critique".

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.