Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem

Options
  • 23-03-2009 3:16pm
    #1
    Registered Users Posts: 757 ✭✭✭


    Hi all,
    I'm having a really hard time making an instant messenger as a college project in Java. Is there anyone who knows the language well who can PM me and look at my code and point me towards where I'm going wrong. I will post the code publically once I have submitted the project.
    Really appreciate any help here lads & ladies!
    Regards,
    Jer


Comments

  • Registered Users Posts: 3,945 ✭✭✭Anima


    Why dont you post up a small bit of the code where you think the problem is? I doubt anyone is going to debug your entire project (if its big).


  • Registered Users Posts: 757 ✭✭✭Signpost


    I dont think I have an error in my code. Its when I try to open an applet with the socket connection established it wont load the applet. Is there some setting in my browser that might be preventing this Applet from loading?


  • Registered Users Posts: 757 ✭✭✭Signpost


    when I comment out this code

    try
    {
    connection = new Socket("localhost",8000);
    output = new ObjectOutputStream(connection.getOutputStream());
    input = new ObjectInputStream(connection.getInputStream());
    }
    catch (IOException e)
    {
    System.out.println("Problem creating socket");
    }

    Once this is commented out I can view my applet no problem so it is obviously an issue with this piece of code


  • Closed Accounts Posts: 1,827 ✭✭✭Donny5


    Does it print "Problem creating socket" to the console?
    Is there an error?
    Does the program continue to run, just without a GUI?
    Use a debugger or shove in some printlns and find out exactly were it's failing.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    If you are getting an error, you might try putting:

    [PHP]e.printStackTrace();[/PHP]

    into the catch as well.

    Where abouts is that that connection code in your main program, is it at the very start before the applet code?


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    put in printlns into the code as follows and lets us know how far it gets
    try
    {
    System.out.println("Into Try Statement");
    connection = new Socket("localhost",8000);
    System.out.println("Connection achieved");
    output = new ObjectOutputStream(connection.getOutputStream());
    System.out.println("Output Stream received");
    input = new ObjectInputStream(connection.getInputStream());
    System.out.println("Input Stream received");
    }
    catch (IOException e)
    {
    System.out.println("Problem creating socket");
    }
    


  • Closed Accounts Posts: 198 ✭✭sh_o


    Is your applet signed? I presume it works in appletviewer?
    I would google for applet and sandbox....


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    change
    catch (IOException e)
    {
    System.out.println("Problem creating socket");
    }
    

    to
    catch (IOException e)
    {
    e.printStackTrace();
    }
    

    Then print the error.


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    just my 2 cents:
    Look into RMI, unless the project requires you to use sockets.

    Write a server that will allow clients to connect in. This would involve:
    - Write an interface class for the server (e.g. MyInterface). Has methods like connect(), sendMessage(String message, String toUser), getMessages(), disconnect(), could be enough.
    - Have MyInterface extend java.rmi.Remote.
    - Actually write the server class. ServerClass implements MyInterface, extends java.rmi.server.UnicastRemoteObject. Once you've implemented the methods in MyInterface you're basically done.
    - Write a separate ServerApplication class that creates a new ServerClass, and registers it in an RMI registry:

    In ServerApplication.java:
    try {
    java.rmi.registry.Registry registry = LocateRegistry.createRegistry(rmiPortNo); // an integer port no
    MyInterface serverObject = new ServerClass();
    Naming.rebind(bindingName, serverObject); // bindingName = name clients will use to connect to your server
    } catch(blah blah blah) {

    }


    So this is now available to your clients.
    The client application does this to get a reference to the server object:

    final MyInterface remoteObject =
    (MyInterface ) Naming.lookup("rmi://" + hostName + ":"+ portNo + "/"+ bindingName);
    // same bindingName and port number you used for the server. hostName could be localhost.

    And voila! you can call the methods: connect(), sendMessage(String message, String toUser), getMessages(), disconnect() directly on the server from your client.
    No more messing with sockets.

    The trickiest part would be clients actually getting new messages that have been sent to them, but you could create a thread to do that:

    Runnable getterOfMessages = new Runnable() {

    public void run() {
    while(true) { // oh I know I know
    String[] messages = remoteObject.getMessages();
    sleep(1000);
    };
    } // end run()

    } // end Runnable

    Thread pollingThread = new Thread(getterOfMessages);
    pollingThread.start();

    Even if your project says you have to use Sockets, mention RMI because : it gives you type safety, because you define a remote interface and you use this on the client to do the remote accesses, so you can be sure clients won't fook around with your server object in unexpected ways.

    Also it's tried and tested. You can implement something with Sockets but it'll hardly be as reliable as Sun's RMI.
    Even though it looks like gobbledegook, it's actually fairly simple.

    ACtually pm if it's a problem but it's actually fairly simple.


Advertisement