Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet in a HTML file (URGENT)

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>*/
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
muthu muruges
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic