Hey guys,
I need a TCP connection between my PHP Website and my Java plugin.
I think nobody asked this before, so here is an example:
A player changes a setting in his control panel (website). Then PHP will send for example a JSON array / object to localhost:8123.
My plugin should listen on this port. As soon as it gets the JSON data, it should print it (console).
So I never did something like that before, I just looked around for solutions, but unfortunately I could not find a good example / solution.
Here is my Java code:
import java.io.*;
import java.net.*;
/**
* Created by pucks on 04.12.15.
*/
public class tcp {
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(8123);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
}
}
}
So I created an object onServerStart → tcp tcp = new tcp().
When I send a TCP request, it seems like there is no service waiting for it … → no effect
My questions:
How do I have to modify my code to get it working?
How can I start the TCP listener then and print the received (JSON) data?
Calling new tcp() is different from calling tcp.main(). main() is only used when you are creating a standalone program. Change the line public static void main(String[] args) { to public tcp() { to have the code run when you say new tcp().
Sorry for not responding …
I found a solution, but there is a problem!
public class tcp {
String clientSentence;
public tcp () throws IOException {
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(25533);
System.out.println("lel wurde eingerichtet");
String line;
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
while((line = inFromClient.readLine()) != null) {
clientSentence += line;
}
System.out.println(clientSentence);
}
}
Btw: thanks to @JBYoshi for advise, I did this in the past, but forgot to throw an IOException But adding the code to the constructor of tcp works fine ^^.
So the tcp service works, but when I start my listener by writing tcp tcp = new tcp() in onServerStart(), it seems like my server freezes. I can print received data, but as I said, my forge server is “freezed”. So I think, I have to start my TCP listener in background or something similar…
Please can you guys say me, how I can do that?
Thanks
I added your sample -> error: socket is not bound -> I added ss.bind(new InetSocketAddress("127.0.0.1", 0)); in main -> error does not occur, but my tcp threads do not start…
Main:
final ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", 0));
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
while(true) {
tcp tcp = new tcp(ss.accept(), logger);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t1.start();