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?

Kevin
  • 23
  • 7
  • 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 – SuperDJ Sep 01 '14 at 08: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. – Yazan Sep 01 '14 at 08: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? – Kevin Sep 01 '14 at 09:39
  • ok, now you can place buttons inside html
    and use php if(isset($_POST['btn1'])){} based on that you can run the socket code and send appropriate message.
    – Yazan Sep 02 '14 at 13:20
  • @Yazan I updated my question with what I have done. – Kevin Sep 03 '14 at 09:46
  • ok so now it works as you need right? on page load it will not send message until you click on that button – Yazan Sep 03 '14 at 11:19
  • @Yazan yeah perfect, thank you. There is one question left. How can I connect on page load, hold the connection the whole time until I leave the page. So on click I only have to send the message and not connect every time I click the button. – Kevin Sep 03 '14 at 14:00
  • as far as i know the socket will be closed after php script completes execution, unless you use pfsockopen http://php.net/manual/en/function.pfsockopen.php but its not reliable, read the comments at the link – Yazan Sep 03 '14 at 14:17
  • thank you very much. Now I can go on with the other stuff :) – Kevin Sep 04 '14 at 08:17
  • @Yazan As I examined my code again more carefully, I noticed that a message is sent while loading the page. Do you know where the problem is? – Kevin Sep 05 '14 at 20:30
  • this could be if you hit F5 (refresh) after pressing Btn1, so it will submit form again, which makes it a click, to reload the page safely as 1st view, copy the URl and past it in new tab – Yazan Sep 07 '14 at 07:08

1 Answers1

2

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

Xenonite
  • 1,823
  • 4
  • 26
  • 39
  • 1
    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](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Reporter Sep 01 '14 at 09:32
  • 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". – Xenonite Mar 13 '15 at 10:53