Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

UDP applet

Thread view: 
oliviergir@gmail.com - 16 Apr 2007 17:10 GMT
Hi,
I thought a regular applet could communicate with its server through
tcp or udp.
It seems to me that it can only send udp packet but not receive any
from its server.

Why is that ?
Why the receive method does not raise any security exception but just
falls in time out ??

dataSent =
            new DatagramPacket(buffer,length,server, port);
        socket = new DatagramSocket();
socket.setSoTimeout(800);
socket.send(dataSent);
        socket.receive(dataRecieved);
Knute Johnson - 16 Apr 2007 17:14 GMT
> Hi,
> I thought a regular applet could communicate with its server through
[quoted text clipped - 12 lines]
> socket.send(dataSent);
>         socket.receive(dataRecieved);

Do you have a firewall?

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 16 Apr 2007 18:07 GMT
yes(windows xp firewall).
I tried to shut it down but same results.

I tried signing the applet (myself with jarsigner) and then it works
fine (even with my firewall on). But that's does not answer my
question..
Knute Johnson - 16 Apr 2007 18:17 GMT
> yes(windows xp firewall).
> I tried to shut it down but same results.
>
> I tried signing the applet (myself with jarsigner) and then it works
> fine (even with my firewall on). But that's does not answer my
> question..

I don't know, if it works when you sign it and not when you don't, I
think that is a clue.  What OS are you using?  Vista is supposed to have
some more issues with applets.

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 16 Apr 2007 18:47 GMT
On 16 avr, 19:17, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> olivier...@gmail.com wrote:
> > yes(windows xp firewall).
[quoted text clipped - 12 lines]
> Knute Johnson
> email s/nospam/knute/

XP.
Knute Johnson - 16 Apr 2007 20:04 GMT
> On 16 avr, 19:17, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 14 lines]
>
> XP.

What are you using for a server, what does your network look like, what
version and supplier of Java are you using?  How about showing us some
actual code for both ends.

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 16 Apr 2007 21:07 GMT
Can some confirm that java applet is authorized to receive UDP packets
from its server ?

I already post java client code.

Server code is C# (IIS): (it just echoes pack the packet)

   protected void StartUDP()
   {
       try
       {
           isStarted = true;
           Boolean goon = true;
           while (goon)
           {
               UdpClient serveur = new UdpClient(port);
               IPEndPoint ip = null;
               log=log+"start waiting at "+DateTime.Now+"<br>";
               byte[] tmp = serveur.Receive(ref ip);

               serveur.Connect(ip);
               serveur.Send(tmp, tmp.Length);
               serveur.Close();
               log = log + "received packet of length=" + tmp.Length
+"<br>";
               log = log + "ip.port=" + ip.Port + "<br>";
               log = log + "Echoed it back to " + ip.Address +
"<br>";

           }
       }
       catch (Exception e)
       {
           isStarted = false;
           log=log+"Exception at "+DateTime.Now+"<br>";
           log=log+e.Message+"<br>";
       }

   }
}
Knute Johnson - 17 Apr 2007 01:12 GMT
> Can some confirm that java applet is authorized to receive UDP packets
> from its server ?

Yes you can.  And I would assume that you can make a regular socket
connection too.

Go to http://www.knutejohnson.com/echo.html to see a working example.
The complete source code is there too.

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 18 Apr 2007 16:16 GMT
thanks you seemed to have prooved that a regular applet can receive
udp datagram from its server though all that remains mysterious and my
server does not work:

Facts:
- There is a firewall on my server side and the windows xp firewall
here on my desktop
- your udp applet http://www.knutejohnson.com/echo.html works fine
from  my desktop
- your udp applet does not work with my c# echo udp (I plugged it to
my server)- server receives packets and echo them back but they nerver
reach back the applet
-my c# echo udp works fine with a standalone java app from my local
computer
-my applet (which does pretty much the same than knute's one) does not
work with my c# echo udp server - - server receive packets and echo
them back but they nerver reach back the applet
- if my applet is signed then it works fine with my c# echo udp server

I need this applet working without being signed !!
Any clue ??
thanks

here is new version of my c# echo udp server :

  protected void StartUDP()
   {
       Socket soUDP = new Socket(AddressFamily.InterNetwork,
              SocketType.Dgram, ProtocolType.Udp);
       try
       {

           //60 sec timeout
           soUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 3000);

           // On doit lier la Socket à un port d'écoute sur la
machine. On choisit également de se mettre en écoute sur toutes les
interfaces réseaux présentes (IPAddress.Any).
           soUDP.Bind(new IPEndPoint(IPAddress.Any,
(int)Application["port"]));
           Application["log"] = Application["log"] + "isBinded<br>";
           Application["isStarted"] = true;
           Application["serverStatus"] = "UDP server isStarted at " +
DateTime.Now + "<br>";
           Application["log"] = Application["log"] + "start waiting
at " + DateTime.Now + "<br>";

           while (!((Boolean)(Application["stopRequested"])))
           {
               //if datagram receveided is bigger than 12 exception
will be thrown on receive
               Byte[] received = new Byte[12];

               // Empty endpoint
               EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
               try
               {

                   //remoteEP will be instanciated by the method
                   // with remote sender info
                   int bytesReceived = soUDP.ReceiveFrom(received,
ref remoteEP);
                   //echoes back
                   soUDP.SendTo(received, remoteEP);

                   String dataReceived =
System.Text.Encoding.ASCII.GetString(received);
                   IPEndPoint ip = (IPEndPoint)remoteEP;
                   Application["log"] = Application["log"] +
"Datagram packet received from "+ ip.Address +":"+ip.Port+ "<br>";
                   Application["log"] = Application["log"] +
"Datagram packet length=" + bytesReceived + " dataReceived=" +
dataReceived + "<br>";
                   Application["log"] = Application["log"] + "Echoed
it back to " + ip.Address +":"+ip.Port+ "<br>";

               }
               catch (Exception t)
               {
                   // Here it is a timeout (but not a
sockettimeoutexception)
                  // Application["log"] = Application["log"] + "" +
t.Message;
               }

           }
       }
       catch (Exception e)
       {
           Application["serverStatus"] = "UDP server probably stoped
on exception at " + DateTime.Now + "<br>";
           Application["isStarted"] = false;
           Application["log"] = Application["log"] + "Exception at "
+ DateTime.Now + "<br>";
           Application["log"] = Application["log"] + e.Message +
"<br>";
           soUDP.Close();
       }

           Application["isStarted"] = false;
           Application["log"] = Application["log"] + "finally closing
server";
           Application["serverStatus"] = "UDP server stoped at " +
DateTime.Now + "<br>";
           soUDP.Close();

   }
Knute Johnson - 18 Apr 2007 17:45 GMT
> thanks you seemed to have prooved that a regular applet can receive
> udp datagram from its server though all that remains mysterious and my
[quoted text clipped - 104 lines]
>
>     }

Let's see the complete code of your applet.

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 18 Apr 2007 18:20 GMT
here is the class which is supposed to measure the ping time within
the applet

package  P;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.*;
import java.io.*;

public class Ping {
    static InetAddress serveur;
    static String payload;
    static DatagramSocket socket;

    static DatagramPacket dataSent;
    static DatagramPacket dataRecieved ;

    public static String getRemotePage(String urlPath)
     {
         try {
          //On crée l'URL
                 URL url = new URL(urlPath);

          //On crée une connection vers cet URL
          URLConnection connection = url.openConnection( );

          //On récupère la taille du fichier
          int length = connection.getContentLength();

          //Si le fichier est inexestant, on lance une exception
          if(length == -1){
           System.err.println("empty page !!");
                }

          //On récupère le flux du fichier
          InputStream is = new
BufferedInputStream(connection.getInputStream());

          //On prépare le tableau de bits pour les données du fichier
          byte[] data = new byte[length];
          is.read(data);
          String s=new String(data);
          System.out.println(" page content="+s);
          return s;
         }
         catch (Exception e)
         {
             e.printStackTrace();
             return "";
         }
     }

    public static void getReady(BandePassante monapplet) {
    try {
        String s=getRemotePage(monapplet.getCodeBase()+"UDPServer.Aspx?
start=Y");
        if (s.indexOf("Server started")<0)
            System.err.println("Could not start UDP Server!!");
        else
            System.out.println("page="+s);
        serveur = InetAddress.getByName(monapplet.host);
        payload="";
        int length = payload.length();
        byte buffer[] = payload.getBytes();
        dataSent =
            new DatagramPacket(buffer,length,serveur, 7777);
        socket = new DatagramSocket();
        socket.setSoTimeout(800);
        dataRecieved = new DatagramPacket(new byte[length],length);
        // let some time to the server start
        Thread.sleep(300);
        for(int i=0;i<2;i++)
            System.out.println("First drop few ping :"+Ping.go());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    }

    public static long go() {
        try {
        System.out.println("send packet");
        long t1=System.currentTimeMillis();
        socket.send(dataSent);
        socket.receive(dataRecieved);
        long t2=System.currentTimeMillis();
        return (t2-t1);
        }catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }
}
Knute Johnson - 18 Apr 2007 18:35 GMT
> here is the class which is supposed to measure the ping time within
> the applet
[quoted text clipped - 91 lines]
>     }
> }

Does it work if you set the data of the outbound packet to something
other than ""?

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 18 Apr 2007 19:02 GMT
with your applet the packet is chosen by user and it does not work
with any
oliviergir@gmail.com - 18 Apr 2007 19:29 GMT
A regular (not signed) applet is only allowed to receive UDP packets
from "its server" right ?
A signed applet can receive UDP packets from any host , right ?

maybe there is a network device somewhere that make impossible for my
applet to realise that the packet is actually coming from "its
server".
That would explain why it only works when my applet is signed ..
what do you think of that track ?
Knute Johnson - 18 Apr 2007 23:21 GMT
> A regular (not signed) applet is only allowed to receive UDP packets
> from "its server" right ?
[quoted text clipped - 5 lines]
> That would explain why it only works when my applet is signed ..
> what do you think of that track ?

I don't know what that would be but what does you network look like?
Where is the server located?

Signature

Knute Johnson
email s/nospam/knute/

oliviergir@gmail.com - 18 Apr 2007 18:34 GMT
more simple :

see how your applet is plugged to my server
http://66.51.207.91/testUDP.html

and the server really receives the packet see output here
http://66.51.207.91/UDPServer.aspx?action=start

but your applet never receives answer, at least on my desktop.
Does it on yours ?
Knute Johnson - 18 Apr 2007 23:20 GMT
> more simple :
>
[quoted text clipped - 6 lines]
> but your applet never receives answer, at least on my desktop.
> Does it on yours ?

No.  It doesn't work if run your applet here.  The server site gives an
error too.

Signature

Knute Johnson
email s/nospam/knute/

Luc The Perverse - 16 Apr 2007 18:11 GMT
>> Hi,
>> I thought a regular applet could communicate with its server through
[quoted text clipped - 14 lines]
>
> Do you have a firewall?

LOL

In the day of NATs the better question might be - do you have port
forwarded?

--
LTP

:)
oliviergir@gmail.com - 16 Apr 2007 21:08 GMT
On 16 avr, 19:11, "Luc The Perverse"
<sll_noSpamlicious_z_XX...@cc.usu.edu> wrote:

> > olivier...@gmail.com wrote:
> >> Hi,
[quoted text clipped - 27 lines]
>
> - Afficher le texte des messages précédents -

no
Luc The Perverse - 17 Apr 2007 00:05 GMT
On 16 avr, 19:11, "Luc The Perverse"
> LOL
>
> In the day of NATs the better question might be - do you have port
> forwarded?

Well there ya go ;)

Setup a static ip address and forward the port (preferable), setup a port
trigger, define your computer as the DMZ (possible security risk).  And your
problems should be solved!

--
LTP

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.