here's a part of a chat server program.Iam not able to correct some
errors here.Can somebody help me with this?
import java.applet.*;
import java.awt.*;
import java.awt.List;
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
/*
<applet code ="chatApplet" width =475 height =350>
</applet>
*/
public class chatApplet extends Applet implements ActionListener
{
static TextArea msg_txt = null;
static TextArea chat_txt = null;
static TextField name_txt = null;
String CONNECT ="Connect";
String DISCONNECT = "Disconnect";
String send ="send";
static Socket soc = null;
PrintStream ps= null;
static List list = null;
static Listen listen = null;
public void init()
{
resize(475,350);
setLayout( new BorderLayout());
Panel con = new Panel();
con.setLayout(new FlowLayout());
name_txt = new TextField(20);
name_txt.setEditable(true);
con.add(new Label("Name:"));
con.add(name_txt);
con.add(new Button("Connect"));
con.add(new Button("Disconnect"));
add("North",con);
Panel msg = new Panel();
msg.setLayout(new FlowLayout());
msg_txt = new TextArea(3,30);
msg_txt.setEditable(true);
msg.add(new Label("Message:"));
msg.add(msg_txt);
msg.add(new Button("Send"));
add("South",msg);
Panel view = new Panel();
view.setLayout(new FlowLayout());
chat_txt = new TextArea(15,30);
chat_txt.setEditable(false);
view.add(chat_txt);
list = new List(13,false);
view.add(list);
add("Center",view);
}
public void stop()
{
disconnect();
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Connect"))
{
if(soc==null)
{
try
{
soc = new Socket
(InetAddress.getLocalHost(),2525);
ps= new PrintStream
(soc.getOutputStream());
ps.println(name_txt.getText
());
ps.flush();
listen = new Listen
(this,name_txt.getText(),soc);
listen.start();
}
catch(IOException e)
{
System.out.println("Error" +
e);
disconnect();
}
}
}
else if (str.equals("Disconnect"))
{
disconnect();
}
else if (str.equals("Send"))
{
if(socket != null)
{
StringBuffer msg = new StringBuffer
("MSG: ");
ps.println(msg.append(msg_txt.getText
()));
ps.flush();
}
}
public void disconnect()
{
if(soc != null)
{
try
{
listen.suspend();
ps.println("QUIT");
ps.flush();
soc.close();
}
catch(IOException e)
{
System.out.println("error" +
e);
}
finally
{
listen.stop();
listen=null;
soc=null;
list.clear();
}
}
}
}
class Listen extends Thread
{
String name = null;
DataInputStream dis = null;
PrintStream ps = null;
Socket socket = null;
chatApplet parent = null;
public Listen(chatApplet p,String n, Socket s)
{
parent =p;
name= n;
socket=s;
try
{
dis = new DataInputStream(s.getInputStream
());
ps = new PrintStream(s.getOutputStream());
}
catch(IOException e)
{
System.out.println("Error:" +e );
parent.disconnect();
}
}
public void run()
{
String msg = null;
while(true)
{
try
{
msg.readLine();
}
catch(IOException e)
{
System.out.println("Error:" +e );
parent.disconnect();
}
if (msg == null)
{
chatApplet.listen = null;
chatApplet.socket=null;
chatApplet.list.clear();
return;
}
StringTokenizer st = new StringTokenizer
(msg,":");
String keyword = st.nextToken();
if(keyword.equals("PEOPLE"))
{
chatApplet.list.clear();
while(st.hasMoreTokens())
{
String str =st.nexttoken();
chatApplet.list.addItem(str);
}
}
else if(keyword.equals("MSG"))
{
String usr=st.nextToken();
chatApplet.chat_txt.appendText(usr);
chatApplet.chat_txt.appendText
(st.nextToken("\0"));
chatApplet.chat_txt.appendText
("\n\n");
}
else if(keyword.equals("QUIT"))
{
chatApplet.listen= null;
chatApplet.socket=null;
chatApplet.list.clear();
}
}
}
}