Java client PHP server UDP Hole Punching sample code

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.

enter image description here

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!

+1
source share
1 answer

I had a similar problem. And he tried to solve it in a similar way.

Some parts of your code do not look right. Sockets in Java are created for TCP , but the header says UDP . Therefore u should use DatagramSockets. But then we came to where I was stuck too. HTTP-Requests also uses tcp, so opening a port with HTTP can lead to a damaged port after closing a tcp session. (Just guess)


 public class Main { public static void main(String[] args) { try { String httpRequest = "GET /index.php HTTP/1.1\n" + "Host: <PHP SERVER NAME HERE>"; InetAddress IPAddress = InetAddress.getByName(<PHP SERVER IP HERE>); DatagramSocket clientSocket = new DatagramSocket(); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = httpRequest; sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 80); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }catch(Exception e){e.printStackTrace();} } } 

In the code above, an HTTP request is theoretically transmitted over UDP. So the port displayed will be UDP. In my case, I did not get any response from PHP Server and got stuck in clientSocket.recieve (..). I think because the firewall of my web server blocks udp packets. If the code is working by someone, I would do the following:

  • save all access to ips and ports in the database and transfer them to another client.
  • Write ur Data to DatagramPackets, as shown above, to another client.

Hope this helps. If anyone can get it in full, I will also be interested in it :)

+1
source

Source: https://habr.com/ru/post/1200985/


All Articles