Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple chat Applet, please see the code inside.

 
nima lama
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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);
}
}
}
}
 
Manoj Pandey
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic