Dear friends ,
i need a pgrm in
applet to chat from one m/c to another m/c.i can do it with applet to application pgm. when i come in for an applet the conection get refused.i did an exmple prgm as sending
a integer no form an applet window as client to server pgm which is written in apln.the server receives the no from aplet &
sends back to client (after squaring).the squared value is displayed in the applet.here by i have given the pgm i have written for server & client chat..pl let me know the errors.
/* the server pgm*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
/*<applet code=Server.class width="400" height="500">
</applet>*/
public class Server extends Applet implements ActionListener
{
Button b ;
TextArea tr,ts;
public void init()
{
ts=new TextArea(4,10);
tr=new TextArea(4,10);
add(ts);
add(tr);
b=new Button("Send");
add(b);
b.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
if(e.getSource()==b)
{ int port= 8080;
ServerSocket ss;
try{
ss=new ServerSocket(port);
Socket s=ss.accept();
PrintWriter toClient= new PrintWriter(s.getOutputStream());
BufferedReader isfromClient=new BufferedReader(new InputStreamReader(s.getInputStream()));
//String data=(
String)(isfromClient.readLine());
toClient.println((String)ts.getText());
toClient.flush();
toClient.close();
String str;
while(isfromClient.readLine()!=null)
{
str=isfromClient.readLine();
tr.setText(str);
}
s.close();
}
catch(Exception e1)
{
System.out.println("EROROR is "+e1);
}
/* Socket s=ss.accept();
PrintWriter toClient= new PrintWriter(s.getDataOutputStream());
BufferedReader isfromClient=new BufferedReader(new InputStreamReader(toClient.getDataInputStream()));
String data=(String)(isfromClient.readLine());
tr.setText(data);*/
}
}
}
/* the client pgm*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
/*<applet code=Client.class width="400" height="500">
</applet>*/
public class Client extends Applet implements ActionListener
{
Button b ;
TextArea tr,ts;
public void init()
{
ts=new TextArea(4,10);
tr=new TextArea(4,10);
add(ts);
add(tr);
b=new Button("Send");
add(b);
b.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
if(e.getSource()==b)
{ int port= 8080;
///ServerSocket ss;
try{
//ss=new ServerSocket(port);
Socket s=new Socket("localhost",port);
PrintWriter toServer= new PrintWriter(s.getOutputStream());
BufferedReader isfromServer=new BufferedReader(new InputStreamReader(s.getInputStream()));
//String data=(String)(isfromServer.readLine());
toServer.println(ts.getText());
String str;
while(isfromServer.readLine()!=null)
{
str=isfromServer.readLine();
tr.setText(str);
}
s.close();
}
catch(Exception e1)
{
System.out.println(" The err is "+e1);
}
/* Socket s=ss.accept();
PrintWriter toClient= new PrintWriter(s.getDataOutputStream());
BufferedReader isfromClient=new BufferedReader(new InputStreamReader(toClient.getDataInputStream()));
String data=(String)(isfromClient.readLine());
tr.setText(data);*/
}
}
}