Skip to main content
gamedev.net
🔒 Locked

[java] Applets with network capabilities?

Started by HopeDagger 2 replies 1.6k views
Original Post
HopeDagger
HopeDagger
I'm toying with a game idea (okay, I'm already at the coding stage :P), but I need to unravel this mystery of giving applets networking capabilities. The results and information on the net about this are sparse and VERY diverse, so I'm still pretty lost. Where better to go to find out than the local Java gurus on GD.NET? [smile] Some websites (like Sun's) tell me that an applet can only connect to the machine that that the applet originated from. Others say that you need to digitally sign the applet, and even then the user needs to confirm the signature before anything can happen. Then I come across TMC's Java Telnet Client that proves both of the above information bits wrong, since there is no confirmation pop-up, and it can telnet to any host freely. What's going on here? How can I enable my applet to be able to connect to remote computers? Thanks in advance!
woogley
woogley
it is true that any applet, signed or not, can connect to the website it was launched from. to connect to other servers, you need to digitally sign the applet.

there is a way around this, however. you could write a quick server in Java that runs on the machine the applet is hosted on. This applet can freely connect to this server. That server has unlimited access to sockets because it's running as a java application on the hosting machine. the server can be coded to take queries from the java applet to connect to outside sources. a proxy server of sorts..

I'll code up an example if I have any free time sometime
woogley
woogley
shazam, an example

this would be the server you run on the same server that is hosting the applet:

[Download: Server.java]
/* Server.java */import java.net.*;import java.io.*;import java.util.*;class Client implements Runnable {	protected Socket sock;	protected BufferedReader in;	protected PrintStream out;	public Client(Socket sock) throws IOException {		this.sock = sock;		in = new BufferedReader(new InputStreamReader(sock.getInputStream()));		out = new PrintStream(sock.getOutputStream());		new Thread(this).start();	}	public void run() {		try {			process();		}		catch (IOException e) {			e.printStackTrace();		}	}	protected void process() throws IOException {		String s = in.readLine();		if (s == null) {			return;		}		if (s.startsWith("GET")) {			StringTokenizer st = new StringTokenizer(s);			if (st.countTokens() <= 1) {				sock.close();				return;			}			st.nextToken();			String host = st.nextToken();			URL url;			try {				url = new URL(host);			}			catch (MalformedURLException e) {				out.println("Malformed URL");				sock.close();				return;			}			Socket tmp;			try {				tmp = new Socket(url.getHost(),80);			}			catch (UnknownHostException e) {				out.println("Can't connect to URL");				sock.close();				return;			}			PrintStream p = new PrintStream(tmp.getOutputStream());			p.print("GET "+url.getPath()+url.getFile()+" HTTP/1.0\r\n\r\n");			BufferedReader b = new BufferedReader(new InputStreamReader(url.openStream()));			String str;			while ((str = b.readLine()) != null) {				out.println(str);			}			sock.close();		}	}}public class Server {	public Server() throws IOException {		ServerSocket ss = new ServerSocket(1234);		System.out.println("Server running on port 1234");		while (true) {			new Client(ss.accept());			System.out.println("New client connected");		}	}	public static void main(String args[]) {		try {			new Server();		}		catch (IOException e) {			e.printStackTrace();		}	}}


and this is an example applet that would work..

[Download: Test.java]
/* Test.java */import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;public class Test extends JApplet implements ActionListener {	JTextField url;	JButton go;	JTextArea content;	String host;	public void init() {		Container c = getContentPane();		c.setLayout(new BorderLayout());		JPanel north = new JPanel();		north.setLayout(new FlowLayout(FlowLayout.CENTER));		url = new JTextField(20);		go = new JButton("Go");		north.add(url);		north.add(go);		url.addActionListener(this);		go.addActionListener(this);		c.add(north,BorderLayout.NORTH);		content = new JTextArea();		host = getDocumentBase().getHost();		if (host == null || host.equals("")) host = "localhost";		c.add(new JScrollPane(content),BorderLayout.CENTER);	}	public void actionPerformed(ActionEvent e) {		if (e.getSource() == url || e.getSource() == go) {			if (url.getText() == null || url.getText().equals("")) {				return;			}			if (!url.getText().toLowerCase().startsWith("http:&#<span class="java-number">47</span>;&#<span class="java-number">47</span>;"</span>)) {<br>				url.setText(<span class="java-literal">"http:&#<span class="java-number">47</span>;&#<span class="java-number">47</span>;"</span>+url.getText());<br>			}<br>			<span class="java-keyword">try</span> {<br>				query(url.getText());<br>			}<br>			<span class="java-keyword">catch</span> (IOException ioe) {<br>				content.setText(ioe.getMessage());<br>			}<br>		}<br>	}<br>	<span class="java-visibilitymodifier">protected</span> <span class="java-primitives">void</span> query(String s) <span class="java-keyword">throws</span> IOException {<br>		Socket sock = <span class="java-keyword">new</span> Socket(host,<span class="java-number">1234</span>);<br>		PrintStream p = <span class="java-keyword">new</span> PrintStream(sock.getOutputStream());<br>		BufferedReader b = <span class="java-keyword">new</span> BufferedReader(<span class="java-keyword">new</span> InputStreamReader(sock.getInputStream()));<br>		p.println(<span class="java-literal">"GET "</span>+s);<br>		String str;<br>		String result = <span class="java-literal">""</span>;<br>		<span class="java-keyword">while</span> ((str = b.readLine()) != <span class="java-keyword">null</span>) {<br>			result+=str+<span class="java-literal">"\n"</span>;<br>		}<br>		content.setText(result);<br>	}<br>}<br><br></pre></div><!--ENDSCRIPT--><br><b>edit:</b> figured out how to properly post code. boom.<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - woogley on July 5, 2006 4:23:44 AM]<!--EDIT--></span><!--/EDIT-->

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.