jQuery in Action, 2nd edition
The moose likes Sockets and Internet Protocols and the fly likes can DatagramSocket work in applets Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "can DatagramSocket work in applets" Watch "can DatagramSocket work in applets" New topic
Author

can DatagramSocket work in applets

Ramneek Handa
Ranch Hand

Joined: Nov 10, 2000
Posts: 44
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

Joined: Jun 08, 2001
Posts: 4
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

Joined: Nov 10, 2000
Posts: 44
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

Joined: May 15, 2001
Posts: 35
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


Laurent LEONARD
Ramneek Handa
Ranch Hand

Joined: Nov 10, 2000
Posts: 44
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: can DatagramSocket work in applets
 
Similar Threads
This Weeks Giveaway
DatagramSocket.setSoTimeout( )
java udp socket programming problem
J2ME for socket
Simple problem in running a Server class