import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ChatClient extends Frame
implements ActionListener, Runnable {
TextArea ta;
TextField tf;
BufferedReader br;
PrintWriter pw;
List l1;
String name;
ChatClient(String title, String address, int port)
{
super(title);
l1=new List(8);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
ta = new TextArea(10, 20);
ta.setEditable(false);
setLayout(new BorderLayout());
add(ta, "Center");
add(l1,"East");
tf = new TextField("", 25);
tf.addActionListener(this);
add(tf, "South");
try
{
Socket s = new Socket(address, port);
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
pw = new PrintWriter(os, true);
pw.println("~"+title);
name="~"+title;
}
catch(Exception e)
{
}
Thread thread = new Thread(this);
thread.start();
}
public void actionPerformed(ActionEvent ae)
{
String rec;
try
{
rec=l1.getSelectedItem();
String str = "<"+rec+">"+name.substring(1)+" : "+tf.getText();
pw.println(str);
tf.setText("");
}
catch(Exception e)
{
}
}
public void run()
{
String rec;
try
{
while(true)
{
String str = br.readLine();
if(str.charAt(0)=='~')
{
if(!str.equals(name))
l1.add(str.substring(1));
}
else
{
ta.append(str + "\n");
}
}
}
catch(Exception e)
{
}
}
}
also i have attached my server and client,which works fine over sending a string. i need to send file.

New Topic/Question
Reply



MultiQuote




|