Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 1.88 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Simple PHP NAT Punch Through Server Script
  2. <?php
  3. error_reporting(E_ALL);
  4. set_time_limit(40); // Allow script to execute for at most 40 seconds.
  5. $myFile = "output.txt";
  6. $fh = fopen($myFile, 'w');
  7.  
  8. if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))
  9. {
  10.  
  11. if(socket_bind($socket,0, 2005))
  12. {
  13.     $clientAddress = 0;
  14.     $clientPort = 0;
  15.     fwrite($fh, "Start at: ".time());
  16.     fwrite($fh, "Waiting for socket at ".time());
  17.     if(socket_recvfrom($socket, &$udp_buff, 23, MSG_WAITALL, &$clientAddress, &$clientPort)) // BLOCKING METHOD
  18.     {
  19.         fwrite($fh, print_r($udp_buff, true));
  20.     }
  21.     else
  22.     {
  23.         echo(socket_strerror(socket_last_error()));
  24.         die();
  25.     }
  26. }
  27. else
  28. {
  29.     echo(socket_strerror(socket_last_error()));
  30.     die();
  31. }
  32. }
  33. else
  34. {
  35. echo(socket_strerror(socket_last_error()));
  36. die();
  37. }
  38.  
  39. fwrite($fh, "End at: ".time());
  40. fclose($fh);
  41. ?>
  42.        
  43. package comm;
  44.  
  45. import java.io.IOException;
  46. import java.net.DatagramPacket;
  47. import java.net.DatagramSocket;
  48. import java.net.InetAddress;
  49. import java.net.SocketException;
  50.  
  51. public class UDPSocket
  52. {
  53. public static void main (String[] asdf)
  54. {
  55.  
  56.     try
  57.     {
  58.  
  59.         String host = <SERVER ADDRESS>;
  60.         int port = 2005;
  61.  
  62.         byte[] message = "Java Source and Support".getBytes();
  63.  
  64.         // Get the internet address of the specified host
  65.         InetAddress address = InetAddress.getByName(host);
  66.  
  67.         // Initialize a datagram packet with data and address
  68.         DatagramPacket packet = new DatagramPacket(message, message.length,
  69.                 address, port);
  70.  
  71.         // Create a datagram socket, send the packet through it, close it.
  72.         DatagramSocket dsocket = new DatagramSocket();
  73.         dsocket.send(packet);
  74.         dsocket.close();
  75.  
  76.     }
  77.     catch (SocketException e)
  78.     {
  79.         e.printStackTrace();
  80.     } catch (IOException e) {
  81.         // TODO Auto-generated catch block
  82.         e.printStackTrace();
  83.     }
  84.  
  85. }
  86. }