UDP applet

O

oliviergir

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);
 
K

Knute Johnson

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);

Do you have a firewall?
 
O

oliviergir

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..
 
K

Knute Johnson

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.
 
O

oliviergir

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.

XP.
 
K

Knute Johnson


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.
 
O

oliviergir

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>";
}

}
}
 
O

oliviergir

LOL

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

--
LTP

:)- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

no
 
L

Luc The Perverse

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!
 
O

oliviergir

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();


}
 
K

Knute Johnson

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();


}

Let's see the complete code of your applet.
 
O

oliviergir

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;
}
}
}
 
K

Knute Johnson

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;
}
}
}

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

oliviergir

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 ?
 
K

Knute Johnson

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 ?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

receive udp packets on windows xp 3
udp multithreading 4
Sending message across network 0
Pb. receiving UDP datagrams 2
nio read timeout ? 1
UDP comm 6
NIO UDP and TCP 5
Java version of tcpdump 6

Members online

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top