• Advertisement
Sign in to follow this  

[java] Applets with network capabilities?

This topic is 4312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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&#47;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:&#47;&#47;")) {
url.setText("http:&#47;&#47;"+url.getText());
}
try {
query(url.getText());
}
catch (IOException ioe) {
content.setText(ioe.getMessage());
}
}
}
protected void query(String s) throws IOException {
Socket sock = new Socket(host,1234);
PrintStream p = new PrintStream(sock.getOutputStream());
BufferedReader b = new BufferedReader(new InputStreamReader(sock.getInputStream()));
p.println("GET "+s);
String str;
String result = "";
while ((str = b.readLine()) != null) {
result+=str+"\n";
}
content.setText(result);
}
}


edit: figured out how to properly post code. boom.

[Edited by - woogley on July 5, 2006 4:23:44 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement