import java.applet.*; import java.net.*; import java.io.*; public class AtmoBaseApplet extends Applet { /** * This is a basic method with we call with our * Javascript. */ public String runFetchData() { return "Applet: runFetchData() has been called"; } /** * This method tests the AtmoServerApp component. Sends * a message to the server and gets the response. */ public String runFetchNetworkData() { return "Applet: "+writeAndReply("TestAgent".getBytes()); } /** * Takes care of networking component */ private String writeAndReply(byte[] write) { URLConnection conn =null; try { // The "host" can be any address of any machine you've started your // server on, except for "localhost". If you start it on your local // machine, you'll find an applet security exception will arise when you // attempt to connect. URL url = new URL("http://"+getCodeBase().getHost()+":5481"); byte[] bytes = new byte[100000]; OutputStream os = null; InputStream is = null; int read = 0; String s = null; conn = url.openConnection(); conn.setDoOutput(true); os = conn.getOutputStream(); os.write(write); os.flush(); conn.connect(); is = conn.getInputStream(); read = is.read( bytes ); s = new String( bytes, 0, read ); return s; } catch (Exception e) { // Exception message is returned if something goes wrong... StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); // return sw.toString(); return "Error - "+e.getMessage(); } } }