Server 端:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ChatServer
{
public static void main(String args[])
{
new ServerFrame();
}
}
class ServerFrame extends Frame
{
ServerSocket server ;
Socket client;
Panel p1,p2,p3;
TextArea ta1,ta2;
Button btn1 ,btn2;
BufferedReader in;
PrintWriter sout;
String s;
public ServerFrame()
{
setTitle("服务器窗口");
setSize(400,400);
setLayout(new BorderLayout());
ta1 = new TextArea(12,50);
ta2 = new TextArea(5,50);
ta1.setEditable(false);
btn1 = new Button("发送");
btn2 = new Button("关闭");
btn1.addActionListener(new BtnSend());
btn2.addActionListener(new BtnClose());
p1 = new Panel();
p1.setSize(390,300);
p1.setLayout(new FlowLayout());
p1.add(ta1);
p2 = new Panel();
p2.setSize(390,100);
p2.setLayout(new FlowLayout());
p2.add(ta2);
p3 = new Panel();
p3.setSize(390,40);
p3.setLayout(new GridLayout(1,5,10,10));
for(int i=0; i<3; i++)
p3.add(new Label());
p3.add(btn1);
p3.add(btn2);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
addWindowListener(new WinL());
setVisible(true);
try
{
server = new ServerSocket(8000);
client = new Socket();
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
sout = new PrintWriter(client.getOutputStream());
ta1.append("Message from Client: " + in.readLine());
}
catch (Exception e)
{
System.out.println("连接错误");
System.exit(0);
}
}
class BtnSend implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
s = ta2.getText();
ta1.append("\nYou are saying: " + s);
sout.println(s);
sout.flush();
ta1.append("\nMessage from Clinet: " + in.readLine());
}catch (IOException e1){}
ta2.setText("");
}
}
public void close()
{
try{
// in.close();
// sout.close();
server.close();
client.close();
}catch(IOException e){}
}
class BtnClose implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
close();
System.exit(0);
}
}
class WinL extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
close();
System.exit(0);
}
}
}
Client 端:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ChatClient
{
public static void main(String args[])
{
new ClientFrame();
}
}
class ClientFrame extends Frame
{
Socket myclient;
Panel p1,p2,p3;
TextArea ta1,ta2;
Button btn1 ,btn2;
BufferedReader in,cin;
PrintWriter out;
String s;
public ClientFrame()
{
setTitle("客户端");
setSize(400,400);
setLayout(new BorderLayout());
ta1 = new TextArea(12,50);
ta2 = new TextArea(5,50);
ta1.setEditable(false);
btn1 = new Button("发送");
btn2 = new Button("关闭");
btn1.addActionListener(new BtnSend());
btn2.addActionListener(new BtnClose());
p1 = new Panel();
p1.setSize(390,300);
p1.setLayout(new FlowLayout());
p1.add(ta1);
p2 = new Panel();
p2.setSize(390,100);
p2.setLayout(new FlowLayout());
p2.add(ta2);
p3 = new Panel();
p3.setSize(390,40);
p3.setLayout(new GridLayout(1,5,10,10));
for(int i=0; i<3; i++)
p3.add(new Label());
p3.add(btn1);
p3.add(btn2);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
addWindowListener(new WinL());
setVisible(true);
try
{
myclient = new Socket(InetAddress.getLocalHost(),8000);
in = new BufferedReader(new InputStreamReader(myclient.getInputStream()));
out = new PrintWriter(myclient.getOutputStream());
}catch (Exception e)
{
System.out.println("连接错误");
System.exit(0);
}
}
public void close()
{
try
{
myclient.close();
in.close();
out.close();
}catch(Exception e){}
System.exit(0);
}
class BtnSend implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
s = ta2.getText();
try
{
ta1.append("You are saying: " + s + "\n");
out.println(s);
out.flush();
s=in.readLine();
ta1.append("Message from server: " + s + "\n");
}catch(IOException e1 ) { }
ta2.setText("");
}
}
class BtnClose implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
close();
System.exit(0);
}
}
class WinL extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
close();
System.exit(0);
}
}
}