Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can DatagramSocket work in applets  RSS feed

 
Ramneek Handa
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to use UDP in applet. below is the part code of the applet. It compiles fine and while running it throws NullPointerException at clientSocket.send(datagram); line.
DatagramSocketclientSocket;
DatagramPacketdatagram;
System.out.println( "Connecting to Server ..." );
destination=InetAddress.getByName("rhanda");
// msg here are byte array object
datagram=new DatagramPacket(msg,msg.length,destination , 5001);
clientSocket.send(datagram);
Can we use DatagramSocket in applets if can then what can be the reason for giving its NullPointerException.
sooner replies will be highly appriciated.
regards,
Ramneek Handa.
 
Srikanth Remani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is the Destination your Server url ie is the UDP connection to the Same server on which your applet is there. Because for security reasons Applets are not allowed to establish socket connetions with other Servers
if this is the Same server I believe UDP should work...

cheers
Srikanth
 
Ramneek Handa
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srikanth,
The servers are same for that. but as i mentioned when I run applet it gives nullpointerexception.
which should not come form connection problem.
rgds,
Ramneek
 
Laurent Leonard
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DatagramSocketclientSocket;
DatagramPacketdatagram;
System.out.println( "Connecting to Server ..." );
destination=InetAddress.getByName("rhanda");
// msg here are byte array object
datagram=new DatagramPacket(msg,msg.length,destination , 5001);
clientSocket.send(datagram);
When do you initialize clientSocket ?
Why not getting the host URL with "Applet.getCodeBase()" instead of hardcoding ?
What happen if destination is null ? (why not testing it, before using it)

------------------
Laurent Leonard
Laurent.Leonard@belgique.com
 
Ramneek Handa
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Laurent Leonard:
When do you initialize clientSocket ?
Why not getting the host URL with "Applet.getCodeBase()" instead of hardcoding ?
What happen if destination is null ? (why not testing it, before using it)

Thanks the problem was that I did not initialize clientSocket.
and I had done hardcoding for giving exp. only and not wrote the testing code here to keep it shorter.
but overall problem was not solved 'coz the motive was not make connection between server and the applet which is behind firewall.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!