Unsupported library that attempts to punch holes through NAT
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SimpleDemo.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.nat.test;
  6. import com.md87.nat.CommsAgent;
  7. import com.md87.nat.NatTraverser;
  8. import com.md87.nat.NatType;
  9. import de.javawi.jstun.attribute.MessageAttributeParsingException;
  10. import de.javawi.jstun.header.MessageHeaderParsingException;
  11. import de.javawi.jstun.util.UtilityException;
  12. import java.io.Console;
  13. import java.io.IOException;
  14. import java.net.DatagramPacket;
  15. import java.net.DatagramSocket;
  16. import java.net.InetAddress;
  17. import java.net.SocketException;
  18. import java.net.UnknownHostException;
  19. /**
  20. *
  21. * @author chris
  22. */
  23. public class SimpleDemo implements CommsAgent {
  24. static final Console console = System.console();
  25. public void sendNATType(NatType type) {
  26. console.printf("Nat type: %s\n", type.name());
  27. }
  28. public NatType readNATType() {
  29. console.printf("Enter NAT type: ");
  30. return NatType.valueOf(console.readLine().trim());
  31. }
  32. public void sendAddress(InetAddress address) {
  33. console.printf("Address: %s\n", address.getHostAddress());
  34. }
  35. public void sendPort(int port) {
  36. console.printf("Port: %s\n", port);
  37. }
  38. public InetAddress readAddress() {
  39. console.printf("Enter address: ");
  40. try {
  41. return InetAddress.getByName(console.readLine().trim());
  42. } catch (UnknownHostException ex) {
  43. return null;
  44. }
  45. }
  46. public int readPort() {
  47. console.printf("Enter port: ");
  48. return Integer.parseInt(console.readLine().trim());
  49. }
  50. public static void main(final String ... args) throws
  51. UnsupportedOperationException, IOException, SocketException,
  52. UtilityException, MessageHeaderParsingException,
  53. MessageAttributeParsingException, InterruptedException {
  54. final NatTraverser traverser = new NatTraverser(new SimpleDemo());
  55. traverser.findBestInetAddress();
  56. final DatagramSocket sock = traverser.traverse();
  57. System.out.println("Traversal complete. Say hello!");
  58. new Thread(new Runnable() {
  59. public void run() {
  60. while (true) {
  61. DatagramPacket p = new DatagramPacket(new byte[255], 255);
  62. try {
  63. sock.receive(p);
  64. System.out.printf("<< %s\n", new String(p.getData()));
  65. } catch (IOException ex) {
  66. ex.printStackTrace();
  67. }
  68. }
  69. }
  70. }).start();
  71. new Thread(new Runnable() {
  72. public void run() {
  73. while (true) {try {
  74. DatagramPacket p = new DatagramPacket(new byte[255], 255);
  75. p.setData((console.readLine() + "\n").getBytes());
  76. sock.send(p);
  77. System.out.printf(">> %s\n", new String(p.getData()));
  78. } catch (IOException ex) {
  79. ex.printStackTrace();
  80. }}
  81. }
  82. }).start();
  83. }
  84. }