0

i have made a video conferencing application in java. It is based on UDP as it should be. It works well on LAN. Now I am switching it to WAN. After a lot of reading about problems in UDP transmission behind NAT (as there will be users behind different routers). I tried to implement hole punching as read. But its not working. I am using a client-server-client architecture. More precisely, every client sends its images(or frames) to server, which then forwards them to other clients. For this i am using the client's IP and port assigned to them by the router. The server then sends the packets to clients. But the packets are still blocked by the router. Can anyone tell me what should be a reliable solution to this? Or there is any library to achieve hole punching efficiently?

================================================================================== 
client code:
//some code
final Socket client = new Socket(IPaddr, port); // tcp connection 
...
// code for some gui components...
...
String message2="video";
clientSocketReceiver = new DatagramSocket();
String ip = "x.x.x.x";
InetAddress IPAddress = InetAddress.getByName(ip);
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();    
baos1.write(message2.getBytes());
baos1.write('\n');
baos1.flush();
byte[] message2InByte = baos1.toByteArray();
baos1.close();
clientSocketReceiver.send(new DatagramPacket(message2InByte, message2InByte.length,    IPAddress, 8876));
...
//some code....
...

//thread for receiving images..
while (true) {
      byte[] receiveData = new byte[50000];
      DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
      clientSocketReceiver.receive(receivePacket);
      InputStream in = new ByteArrayInputStream(receivePacket.getData());
      buffImage = ImageIO.read(in);
      ...
      //code for displaying images on GUI
      ...
}
//==========================================================================


=========================================================================== =
server code:
//code for storing client's ip and port. runs only when a new client is accepted by tcp              socket....

serverSocket=new DatagramSocket(8876);       //declared at starting of code
while (checked==false ) {
       DatagramPacket receivePacket = new  DatagramPacket(receiveData,receiveData.length);
       serverSocket.receive(receivePacket);
       String data = new String(receivePacket.getData());
       data = data.trim();
       if (data.equals("video")) {
             vx.add(receivePacket.getAddress().getHostAddress()); // storing ip
             String tmp = "" + receivePacket.getPort();
             vv.add(tmp);                //storing port. vv and vx r vectors
             checked=true;
       } 
   }

======================================  
//thread for receiving/sending images from/to clients
DatagramSocket serverSocket2 = new DatagramSocket(9876);
byte[] receiveData = new byte[50000];
while (true) {
 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
 serverSocket2.receive(receivePacket);
 InputStream in = new ByteArrayInputStream(receivePacket.getData());
 bImage = ImageIO.read(in);
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ImageIO.write(bImaget, "jpg", baos);
 baos.flush();
 byte[] imageInByte = baos.toByteArray();
 baos.close();
 for (int i = 0; i < vx.size() && i < vv.size(); i++) {
     serverSocket2.send(new DatagramPacket(imageInByte, imageInByte.length, InetAddress.getByName(vx.elementAt(i).toString()), Integer.parseInt(vv.elementAt(i).toString())));
  }
}
=============================================
Kunjan Thadani
  • 1,660
  • 3
  • 18
  • 26
  • You described what you want to do, but gave no info about what you have done. Your Client and Server code will be useful, NOT all of it - just the relevant connect ad send/receive methods – Germann Arlington Nov 24 '13 at 12:42
  • Initially the clients send a packet to the server. The server accepts the requests or logins on a port say X. Another port say Y is used by the server to receive the images. Two different ports are used, one for accepting client (to store their ip and port). And the second port only for receiving/sending images. The server is saving the ip and respective port of the clients. After that, Whenever any image is received by the server, it sends those images to the stored ip(s) on their respective ports. – Kunjan Thadani Nov 24 '13 at 12:54
  • This sounds like TCP, **NOT** UDP connection. That is the reason why I ask for your `code`, **NOT** your explanaton – Germann Arlington Nov 24 '13 at 12:59
  • Yes you r thinking right. There is TCP included. But the code is too big and complex now to put it up here. Still making it clear, the clients connect to the server through tcp. The server accepts it and sends a message back to it. After that a udp packet is sent from client. After this the above procedure mentioned occurs. – Kunjan Thadani Nov 24 '13 at 13:17
  • I don't need **ALL** code, just the part that sends/receives UDP packets. I doubt that there will be many people who will want to steal your code, if your concern **IS** the size of the code than you can share it on github/googlecode or any of other open repositories. Better still provide **SHORT** parts of code relevant to your problem – Germann Arlington Nov 24 '13 at 13:50
  • Is there anything to be done with bind or connect methods? I've started studying java just about 5 months ago, so i may not be knowing many of the things right now. Please do correct wherever required. :) – Kunjan Thadani Nov 25 '13 at 03:47

0 Answers0