• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple chat Applet, please see the code inside.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
please see this code. when i run this program it produces strange result.
what is wrong with this?
it works fine for the first time. i mean when i type some thing in the text field the server response and shows it in the textarea, but i can't write it for the second time.

please make neccessary changes.
thank you.
nima lama
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import java.net.*;
import java.io.*;
//<applet code=client.class height=300 width=400></applet>
public class client extends Applet implements ActionListener
{
TextArea txta;
TextField txt;
Button b;
Socket client;

BufferedReader keyin;
BufferedReader in;
PrintWriter out;
String keyinput;
String msg;

public void init()
{
txta=new TextArea(20,30);
txt=new TextField(40);
b=new Button("send");
b.addActionListener(this);
add(txta);
add(txt);
add(b);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{

try
{

client=new Socket("127.0.0.1",8080);
//keyin=new BufferedReader(new InputStreamReader(System.in));

in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream(),true);

while(true)
{
try
{
//keyinput=keyin.readLine();

out.println(txt.getText());
out.flush();
msg=in.readLine();
txta.setText(msg);
}
catch(Exception ex)
{
System.out.println("error: "+ex.getMessage());
}
}
}
catch(Exception ex)
{
System.out.println("error :"+ex.getMessage());
}
}

}
}
****************
SERVER PROGRAM***
*****************
import java.net.*;
import java.io.*;
public class server extends Thread
{
ServerSocket sersoc;
Socket client;

public server()
{
try
{
sersoc=new ServerSocket(8080);
while(true)
{
try
{
client=sersoc.accept();
processclient pc=new processclient(client.getInputStream(),client.getOutputStream());
pc.start();
}
catch(Exception e)
{
System.out.println("Error: "+e.getMessage());
System.exit(0);
}
}
}
catch(Exception e)
{
System.out.println("error :"+e.getMessage());
System.exit(0);
}
}
public static void main(String args[])
{
server vserver=new server();
}
}
class processclient extends Thread
{
BufferedReader in;
PrintWriter out;
String msg;

public processclient(InputStream is,OutputStream os)
{
super();
in=new BufferedReader(new InputStreamReader(is));
out=new PrintWriter(os,true);
}
public void run()
{
while(true)
{
try
{
msg=in.readLine();
System.out.println(msg);
out.println(msg);
}
catch(Exception e)
{
System.out.println("error: "+e.getMessage());
System.exit(0);
}
}
}
}
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi lima,
try multithreading the client also, read and write messages to the server in diffreent threads. Hope this works
Manoj pandey,
SCJP.
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic