• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Applet in Networking

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);*/
}
}
}

 
permaculture is largely about replacing oil with people. And one tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic