3

I have found quite a lot of other posts on this topic but none seem to have the answer I need.
I have written a Bukkit plugin for Minecraft that can send post data to a PHP page and get a return from the page.

Now the one thing I can't figure out. I would like to have a button on the page, and when the button is clicked, send data to the Java plugin and have the plugin print the message.

I have seen something about sockets. But after reading about them I can't figure out how to set them up.
Pretty much at any time you should be able to click the button and it sends data to the Java plugin and I can use that data however I like.

Does anyone know how I can have the Java plugin constantly waiting for data from the page?

My current code:
(This sends the players name to the website.)

String re = "";  
URL url = new URL("address here");  
URLConnection con = url.openConnection();  
con.setDoOutput(true);  
PrintStream ps = new PrintStream(con.getOutputStream());  
ps.print("player=" + player.getName());  
con.getInputStream();  
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));  
String line;  
while ((line = rd.readLine()) != null) {  
   re += line + "\n";  
}  
rd.close();  
ps.close();

And my php just returns any post data it gets.
It works fine, but I would like to listen in my java plugin for data from the php page.

2 Answers 2

1

There are many ways to make communication between two servers. I'd use one of them:

  • Sockets
  • JMS - Java Message Service such as ActiveMQ

Both of them have tutorials available, just google.

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

1 Comment

Yes, I heard about sockets. And I am trying to figure out how to use them.
1

You could use a database, or setup a json/xml api on the PHP end, and access the database, or access the json/xml from Java with this example code to open the url.

URL url = new URL("site.com/api/foo.json");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

You can look at this tutorial to parse JSON with Java.

2 Comments

So you say I should write to a file with Java and read it with PHP and write a file with PHP and read it with Java? I think that would only work one way because the PHP can't access files where the Java is running.
@ParkerJones Generate the JSON from a database in PHP, and read it from Java. Write from Java using an api in PHP. It's pretty complicated, but doable. The simplest way is to use a database.

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.