一、服务器端代码
package org.clentserver;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.Socket;
import java.net.ServerSocket;
public class csDemo01 extends JFrame implements ActionListener
{
ServerSocket server;//服务器端套接字
Socket theClient;//与客户端通信的套接字
boolean done;//通信是否结束
JButton sent;//发送按钮
JTextArea chatContent;//聊天内容区
JTextField sentence;//聊天信息栏
DataInputStream in = null;//来自客户端的输入流
DataOutputStream out = null;//发送到客户端的输出流
public csDemo01()
{
buildGUI("聊天室----服务器端");
try
{
server = new ServerSocket(8888);//创建服务器套接字对象
}
catch (IOException e)
{
System.out.println(e);
}
while (true)
{
try
{
theClient = server.accept();
out = new DataOutputStream(theClient.getOutputStream());
in = new DataInputStream(theClient.getInputStream());
done = true;
String line = null;
while(done)
{
while((line = in.readUTF())!=null)
{
chatContent.append("对方:"+line+"\n");
}
in.close();
out.close();
theClient.close();
}
}
catch (Exception e1)
{
chatContent.append("对方("+theClient.getInetAddress()+")已经离开聊天室");
}
}
}
public void buildGUI(String title)//构造图形界面
{
this.setTitle(title);
this.setSize(400,300);
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
JScrollPane centerPane = new JScrollPane();
chatContent = new JTextArea();
centerPane.setViewportView(chatContent);
container.add(centerPane,BorderLayout.CENTER);
chatContent.setEditable(false);
JPanel bottomPanel = new JPanel();
sentence = new JTextField(20);
sent = new JButton("发送");
bottomPanel.add(new JLabel("聊天信息"));
bottomPanel.add(sentence);
bottomPanel.add(sent);
container.add(bottomPanel,BorderLayout.SOUTH);
sent.addActionListener(this);
sentence.addActionListener(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter()//匿名内部内监听窗口关闭操作
{
public void windowClosing(WindowEvent e)
{
try
{
out.writeUTF("bye");
}
catch (IOException e2)
{
System.out.println("服务器端窗口关闭。。。");
}
finally
{
System.exit(0);
}
}
});
}
public void actionPerformed(ActionEvent e)
{
String str = sentence.getText();//获取聊天信息栏的聊天内容
if(str!=null&&!str.equals(""))//如果聊天内容不为空,则发送信息
{
chatContent.append("本人:"+str+"\n");
try
{
out.writeUTF(sentence.getText());
}
catch (Exception e3)
{
chatContent.append("客户端不存在...\n");
}
}
else
{
chatContent.append("聊天信息不能为空\n");
}
sentence.setText("");//清空聊天信息栏的内容
}
public static void main(String[] args)
{
new csDemo01();
}
}
二、 客户端代码
package org.clentserver;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.Socket;
public class OfcsDemo02 extends JFrame implements ActionListener
{
Socket client;
boolean done;
JButton sent;
JTextArea chatContent;
JTextField sentence;
DataInputStream in = null;
DataOutputStream out = null;
public OfcsDemo02()
{
buildGUI("聊天室----客户机端");
try
{
client = new Socket("localhost",8888);
out = new DataOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
done = false;
String line = null;
while(!done)
{
while((line = in.readUTF())!=null)
{
chatContent.append("对方:"+line+"\n");
if(line.equals("bte"))
{
String msg = "服务器发来结束通信命令!\n";
msg += "系统将在您确认此对话框的8秒钟后关闭,\n";
JOptionPane.showMessageDialog(this, msg);
Thread.sleep(8000);
done = true;
break;
}
}
in.close();
out.close();
System.exit(0);
}
}
catch (Exception e)
{
chatContent.append("服务器已关闭。。。\n");
}
}
public void buildGUI(String title)
{
this.setTitle(title);
this.setSize(400,300);
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
JScrollPane centerPane = new JScrollPane();
chatContent = new JTextArea();
centerPane.setViewportView(chatContent);
container.add(centerPane,BorderLayout.CENTER);
chatContent.setEditable(false);
JPanel bottomPanel = new JPanel();
sentence = new JTextField(20);
sent = new JButton("发送");
bottomPanel.add(new JLabel("聊天信息"));
bottomPanel.add(sentence);
bottomPanel.add(sent);
container.add(bottomPanel,BorderLayout.SOUTH);
sent.addActionListener(this);
sentence.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String str = sentence.getText();
if(str!=null&&!str.equals(""))
{
chatContent.append("本人:"+str+"\n");
try
{
out.writeUTF(sentence.getText());
}
catch (Exception e3)
{
chatContent.append("服务器没有启动...\n");
}
}
else
{
chatContent.append("聊天信息不能为空\n");
}
sentence.setText("");
}
public static void main(String[] args)
{
new OfcsDemo02();
}
}
三、 效果展示