Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java UDP Applet Problem

  • 02-01-2007 09:16PM
    #1
    Registered Users, Registered Users 2 Posts: 590 ✭✭✭


    Hey,

    I'm not very good at programming in java so this problem may be very simple. I'm trying to get basic communications between a UDP server application and a UDP Client running in an applet. The client works fine when I run the code inside an application, but when I run it as an applet I get the JOptionPane showing "Probe1" but never gets to "Probe2". Has this got something to do with java security not allowing UDP/TCP from an applet? I had a similar project work using TCP last year as long as I was communicating with an application on the localhost so I thought it should work the same here. Any help would be very much appriecated.

    Edit: I just checked to see if there was any error raised in the applet. When I print it out it shows:
    java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:9876 connect, resolve)
    What does this mean??
    ______________________________________

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.net.*;
    import javax.swing.*;

    public class UDPApplet extends Applet
    {
    //public void paint( Graphics g )
    public void init()
    {
    try
    {
    DatagramSocket clientSocket = new DatagramSocket();

    InetAddress IPAddress = InetAddress.getByName("127.0.0.1");

    byte[] sendData = new byte[1024];

    byte[] receiveData = new byte[1024];

    String sentence = "Hello World";

    sendData = sentence.getBytes();

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

    JOptionPane.showMessageDialog(null, "Probe1");

    clientSocket.send(sendPacket);

    JOptionPane.showMessageDialog(null, "Probe2");

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

    clientSocket.receive(receivePacket);

    String modifiedSentence = new String(receivePacket.getData());

    JOptionPane.showMessageDialog(null, modifiedSentence);

    clientSocket.close();

    }
    catch (Exception e) {}
    }
    }
    ______________________________________________

    import java.io.*;
    import java.net.*;

    class UDPS
    {
    public static void main(String args[]) throws Exception
    {

    DatagramSocket serverSocket = new DatagramSocket(9876);

    byte[] receiveData = new byte[1024];

    byte[] sendData = new byte[1024];

    while(true)
    {

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

    serverSocket.receive(receivePacket);

    String sentence = new String(receivePacket.getData());

    InetAddress IPAddress = receivePacket.getAddress();

    int port = receivePacket.getPort();

    String capitalizedSentence = sentence.toUpperCase();

    sendData = capitalizedSentence.getBytes();

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

    serverSocket.send(sendPacket);
    }
    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Um this might sound a bit obvious but you sure you don't have a firewall running that is blocking this port? First thing I would try.

    Edit - now i don't know anything about java didn't touch it yet but you may have to grand permission in your app.

    This might look interesting: http://forums.devx.com/showthread.php?t=29907


  • Registered Users, Registered Users 2 Posts: 590 ✭✭✭bman


    Thanks for the reply Webmonkey. The reason I didn't think it was a firewall problem is because it works in the application format but not in the applet. I'll have a look at that thread you linked to.


  • Closed Accounts Posts: 198 ✭✭sh_o


    The issue is with the security model that is run when running an applet as opposed to an application.
    The following url has a good explanation from the source on how to do this.
    http://java.sun.com/developer/technicalArticles/Security/applets/

    For testing purposes, if you are trying the applet using appletviewer, use the command 'policytool' from the jre and add a new permission "all permissions" to your policy file and then you should be able to connect. Note this is just a quick workaround to save you signing the jar file etc which would be necessary for any production applet.


  • Registered Users, Registered Users 2 Posts: 590 ✭✭✭bman


    Thanks sh_o. I don't want to have to go through the whole signing jar files and all that if i can help it, but applet viewer will not do. Is there a quick workaround for running this in a browser without the signing of jar files? Its only a small part of a college project so I want to hack through it as quick as possible.


  • Registered Users, Registered Users 2 Posts: 5,397 ✭✭✭DublinDilbert


    From memory the only server an applet can open a TCP/UDP connection to is the server it was downloaded from, into the web-browser.

    Is your UDP server running on the same machine that serves the web-page that calls the applet?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 590 ✭✭✭bman


    Ok, so I went through signing the jar files and it works well. Now I want to have a C server talk to a java applet. It works fine for a C server and client, a java server and client applet, but for some reason I cannot send a datagram from the java applet to the C server. I have copied in the code I think is relevant below. Is there some sort of problems with interoperability between java and c strings/byte[]? Hopefully someone knows where I'm going wrong, thanks.

    java client code for sending datagram:

    InetAddress IPAddress = InetAddress.getByName("localhost");//("127.0.0.1");
    String s = "Hello";
    sendData = s.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,1099);
    clientSocket.send(sendPacket);


    c server code to receive datagram:

    int sock, length, fromlen, n, i = 0;
    struct sockaddr_in server;
    struct sockaddr_in from;
    char buf[1024];

    sock=socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) error("Opening socket");

    length = sizeof(server);

    bzero(&server,length);

    server.sin_family=AF_INET;

    server.sin_addr.s_addr=INADDR_ANY;

    server.sin_port=htons(PORT);//atoi(PORT));

    if (bind(sock,(struct sockaddr *)&server,length)<0)
    error("binding");

    fromlen = sizeof(struct sockaddr_in);

    printf("Waiting for datagram from client\n");
    n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
    if (n < 0) error("recvfrom");
    write(1,"Received a datagram: ",21);
    write(1,buf,n);


    The c program sits at "Waiting for datagram from client".
    Once again, thanks for all the help so far.


  • Closed Accounts Posts: 198 ✭✭sh_o


    I can only speak from the java end, but I presume the information is buffered so make sure you call clientSocket.flush() on the java client end to indicate you are finished.
    Also, should the c code not be in some form of a loop, put a while loop around the checking for the datagram bit


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yeah i was wondering about that while loop as well


  • Registered Users, Registered Users 2 Posts: 590 ✭✭✭bman


    It seems the problem was with this line although I can see no reason why it wouldn't work as it worked for the application?!

    InetAddress IPAddress = InetAddress.getByName("localhost");//("127.0.0.1");

    I deleted the "localhost" part and uncommented the "127.0.0.1" part. Works fine now. Thanks for the help.


Advertisement