- Simple PHP NAT Punch Through Server Script
- <?php
- error_reporting(E_ALL);
- set_time_limit(40); // Allow script to execute for at most 40 seconds.
- $myFile = "output.txt";
- $fh = fopen($myFile, 'w');
- if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))
- {
- if(socket_bind($socket,0, 2005))
- {
- $clientAddress = 0;
- $clientPort = 0;
- fwrite($fh, "Start at: ".time());
- fwrite($fh, "Waiting for socket at ".time());
- if(socket_recvfrom($socket, &$udp_buff, 23, MSG_WAITALL, &$clientAddress, &$clientPort)) // BLOCKING METHOD
- {
- fwrite($fh, print_r($udp_buff, true));
- }
- else
- {
- echo(socket_strerror(socket_last_error()));
- die();
- }
- }
- else
- {
- echo(socket_strerror(socket_last_error()));
- die();
- }
- }
- else
- {
- echo(socket_strerror(socket_last_error()));
- die();
- }
- fwrite($fh, "End at: ".time());
- fclose($fh);
- ?>
- package comm;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- public class UDPSocket
- {
- public static void main (String[] asdf)
- {
- try
- {
- String host = <SERVER ADDRESS>;
- int port = 2005;
- byte[] message = "Java Source and Support".getBytes();
- // Get the internet address of the specified host
- InetAddress address = InetAddress.getByName(host);
- // Initialize a datagram packet with data and address
- DatagramPacket packet = new DatagramPacket(message, message.length,
- address, port);
- // Create a datagram socket, send the packet through it, close it.
- DatagramSocket dsocket = new DatagramSocket();
- dsocket.send(packet);
- dsocket.close();
- }
- catch (SocketException e)
- {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }