aspose file tools*
The moose likes Applets and the fly likes applet in a HTML file (URGENT) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "applet in a HTML file (URGENT)" Watch "applet in a HTML file (URGENT)" New topic
Author

applet in a HTML file (URGENT)

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello,
I just created a Single server - client program. It works fine when i excute it using "appletviewer". The problem i am having is when i create two HTML files with the "<APPLET> tag it doesn't work. I have specified the host as the "local host". So can you please tell me how to make this work. This is urgent.
Server
========
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Applet implements Runnable,ActionListener
{
TextArea mainta;
TextArea messageta;
Button b;
BufferedReader br;
ServerSocket ser=null;
Socket s=null;
Thread t;

public void init()
{
mainta=new TextArea(20,10);
messageta=new TextArea(3,10);
b=new Button("Send");
b.addActionListener(this);
setLayout(new GridLayout(3,1));
t=new Thread(this);
add(mainta);
add(messageta);
add(b);
try
{
InetAddress servAddr=InetAddress.getByName(null);
ser=new ServerSocket(5001,1);
s=ser.accept();
t.start();
Thread.sleep(500);
}
catch(Exception e)
{
mainta.setText("Error Contacting the Client");
}
}
public void run()
{
try
{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String temp;
while(true)
{
String str=br.readLine();
mainta.setText(mainta.getText()+str+"\n");
}
}
catch(Exception e)
{
mainta.setText("Error");
}

}
public void actionPerformed(ActionEvent ae)
{

try
{
String send=messageta.getText();
PrintWriter pw=new PrintWriter(s.getOutputStream());
pw.println(send);
messageta.setText("");
pw.flush();
}
catch(Exception e)
{
mainta.setText("Error Sending data");
}

}
}
/*<applet code=chatServer.class height=400 width=400>
</applet>*/
Client
=======
import java.applet.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class chatClient extends Applet implements Runnable,ActionListener
{
TextArea mainta;
TextArea messageta;
Button b;
BufferedReader br;
Socket s;
Thread t;

public void init()
{
mainta=new TextArea(20,10);
messageta=new TextArea(3,10);
b=new Button("Send");
b.addActionListener(this);
setLayout(new GridLayout(3,1));
t=new Thread(this);
add(mainta);
add(messageta);
add(b);
try
{
InetAddress serverAddr=InetAddress.getByName(null);
s=new Socket(serverAddr.getHostName(),5001);
t.start();
Thread.sleep(500);
}
catch(Exception e)
{
mainta.setText("Error Contacting the Server");
}
}
public void run()
{
try
{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String temp;
while(true)
{
String str=br.readLine();
mainta.setText(mainta.getText()+str+"\n");
}
}
catch(Exception e)
{
mainta.setText("Error");
}
}
public void actionPerformed(ActionEvent ae)
{
try
{
String send=messageta.getText();
PrintWriter pw=new PrintWriter(s.getOutputStream());
pw.println(send);
messageta.setText("");
pw.flush();
}
catch(Exception e)
{
mainta.setText("Error Sending data");
}

}
}

/*<applet code=chatClient.class height=400 width=400>
</applet>*/


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

There's need to post "URGENT". Most messages get an answer in a day or 2, except when nobody else knows the answer either.
The #1 question to ask when an applet isn't running in a browser when it did in AppletViewer is "What sandbox rules am I violating"?
In this case, it's that you're trying to open a network connection to some other server than the one the applet was loaded from. Ony signed applets can do that.
BTW, if you use the UBB CODE feature (there's a button you can click) it'll wrap your sample code with markers tha keep the indentation from getting all messed up.


Customer surveys are for companies who didn't pay proper attention to begin with.
muthu muruges
Ranch Hand

Joined: Feb 28, 2003
Posts: 70
Dont use applet (in Browser) as a server. use plain java class as server.Run the server in a centralized place (i.e. where webserver running). Clients can be applets. connect the clients to that m/c.
 
Don't get me started about those stupid light bulbs.
 
subject: applet in a HTML file (URGENT)