• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Applet in Networking

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);*/
}
}
}

 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic