用两个类来实现,一个Server类和Client类,在MyEclipse下调试通过。
Server类:
/**
* 聊天软件Server程序段
* @author Rabbit
* @version 0.9 2007-11-6
*/
package com.sinojava.three;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server {
private JFrame frame;
private JTextField enter;
private JTextArea display;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket ser;
private Socket soc;
private Container container;
private int counter = 0;
//服务器端GUI
public Server() {
frame = new JFrame("服务器端");
container = frame.getContentPane();
enter = new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendData(e.getActionCommand());
}
});
container.add(enter,BorderLayout.NORTH);
display = new JTextArea();
container.add(new JScrollPane(display),BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void sendData(String message) {
// 向客户端发送信息
try {
output.writeObject("服务器>>>"+message);
output.flush();
display.append("\n服务器>>>"+message);
}
catch (IOException e) {
display.append("\n错误写入对象");
}
}
public void runServer() {
try {
// 第一步:创建一个ServerSocket
ser = new ServerSocket(5000,100);
while(true) {
counter++;
// 第二步:等待一个连接
waitForConnection();
// 第三步:获取接受数据
getStreams();
// 第四步:连接处理
processConnection();
// 第五步:关闭连接
closeConnection();
}
}catch (IOException e) {
e.printStackTrace();
}
}
//等待连接服务器
private void waitForConnection() throws IOException {
display.setText("等待连接\n");
soc = ser.accept();
//InetAddress类采用工厂设计模式,有三个静态工厂方法,如getHostName or getLocalHost。
display.append("Socket "+counter+" 接收来至:"+soc.getInetAddress().getHostName());
}
// 获取接受数据
private void getStreams() throws IOException {
output = new ObjectOutputStream(soc.getOutputStream());
output.flush();
input = new ObjectInputStream(soc.getInputStream());
display.append("\n获得I/O流\n");
}
// 连接处理
private void processConnection() throws IOException {
String message ="服务器: 连接成功\n";
output.writeObject(message);
output.flush();
enter.setEnabled(true);
do {
try {
message = (String)input.readObject();
enter.setText(null);
display.append("\n"+message);
display.setCaretPosition(display.getText().length());
}
catch(ClassNotFoundException e) {
display.append("\n未知对象类型接收");
}
} while (!message.equals("客户端>>>终端"));
}
//关闭Socket
private void closeConnection() throws IOException {
display.append("\n用户终端连接");
enter.setEnabled(false);
output.close();
input.close();
soc.close();
}
public static void main(String args[]) {
Server server = new Server();
server.runServer();
}
}
Client类:
/**
* 聊天软件Client程序段
* @author Rabbit
* @version 0.9 2007-11-6
*/
package com.sinojava.three;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client {
private JFrame frame;
private JTextField enter;
private JTextArea display;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket soc;
private Container container;
// 客户端GUI
public Client(String host) {
frame = new JFrame("客户端");
// 设置服务器到客户端连接
chatServer = host;
container = frame.getContentPane();
enter = new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendData(e.getActionCommand());
}
});
container.add(enter,BorderLayout.NORTH);
display = new JTextArea();
container.add(new JScrollPane(display),BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 连接服务器处理信息
public void runClient() {
try {
// 第一步:创建一个Socket,连接服务器
connectToServer();
// 第二步:获取接受数据
getStreams();
// 第三步:连接处理
processConnection();
// 第四步:关闭连接
closeConnection();
}
catch (IOException e) {
e.printStackTrace();
}
}
// 获取接受数据
private void getStreams() throws IOException {
output = new ObjectOutputStream(soc.getOutputStream());
output.flush();
input = new ObjectInputStream(soc.getInputStream());
display.append("\n获得I/O流\n");
}
// 创建一个Socket,连接服务器
private void connectToServer() throws IOException {
display.setText("连接中\n");
//InetAddress类采用工厂设计模式,有三个静态工厂方法,如getByName。
soc = new Socket(InetAddress.getByName(chatServer),5000);
display.append("连接到:"+soc.getInetAddress().getHostName());
}
// 连接处理
private void processConnection() throws IOException {
enter.setEnabled(true);
do {
try {
message = (String)input.readObject();
enter.setText("");
display.append("\n"+message);
display.setCaretPosition(display.getText().length());
}
catch (ClassNotFoundException e) {
display.append("\n没有对象接受");
}
} while (!message.equals("服务器>>>终端"));
}
// 关闭连接
private void closeConnection() throws IOException {
display.append("\n关闭连接");
output.close();
input.close();
soc.close();
}
// 发送信息到服务器
private void sendData(String message) {
try {
output.writeObject("客户端>>>"+message);
output.flush();
display.append("\n客户端>>>"+message);
}
catch (IOException e) {
display.append("\n错误的对象");
}
}
public static void main(String args[]) {
Client client = new Client("127.0.0.1");
client.runClient();
}
}