I am working on a project that will require an ea p2p server, but I have not found the java-client php-server example code. I understand the concept of working the udp hole hole, but I cannot get anything to work in the code.

What I tried:
TheSocket.java
public class TheSocket { public static String response = "hello"; public static String request; public static String webServerAddress; public static ServerSocket s; protected static ServerSocket getServerSocket(int port)throws Exception{ return new ServerSocket(port); } public static void handleRequest(Socket s){ BufferedReader is; PrintWriter os; try{ webServerAddress = s.getInetAddress().toString(); is = new BufferedReader(new InputStreamReader(s.getInputStream())); request = is.readLine(); System.out.println(request); os = new PrintWriter(s.getOutputStream(), true); os.println("HTTP/1.0 200"); os.println("Content-type: text/html"); os.println("Server-name: TheSocket"); os.println("Content-length: " + response.length()); os.println(""); os.println(response); os.flush(); os.close(); s.close(); }catch(Exception e){ System.out.println("Failed to send response to client: " + e.getMessage()); }finally{ if(s != null){ try{ s.close(); }catch(Exception e){ e.printStackTrace(); } } } return; } }
Main.java
public class Main { public static void main(String[] args)throws Exception{ TheSocket.s = TheSocket.getServerSocket(6789); while(true){ Socket serverSocket = TheSocket.s.accept(); TheSocket.handleRequest(serverSocket); } }
PHP-CONNECT.php - to get the port of other users, I manually connect and use the port shown on the web page.
<?php echo $_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT']; ?>
The problem with the above code is that it cannot get into the socket unless I transfer it.
Comment if you have questions!
user7887655
source share