服务端代码
import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* @author lemon
* @date 2011/7/19
*/
public class ChatServer extends JFrame {
JTextArea ja = new JTextArea();
JTextField jf = new JTextField("服务器启动");
ServerSocket ss = null;
boolean bool = false;
ArrayList<Client> clients = new ArrayList<>();
public static void main(String[] args) {
new ChatServer().connect();
}
/**
* 服务器端界面
*/
ChatServer() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(d.width / 2, 0, d.width / 4, d.height);
setLayout(new BorderLayout());
ja.setBackground(Color.yellow);
jf.setBackground(Color.orange);
setVisible(true);
add(jf, BorderLayout.NORTH);
add(ja, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void connect() {
try {
ss = new ServerSocket(8880);
bool = true;
while (bool) {
//阻塞式连接
Socket s = ss.accept();
//连接一个创建一个客户
Client c = new Client(s);
//线程启动
new Thread(c).start();
clients.add(c);
jf.setText("有" + clients.size() + "个用户上线了");
}
} catch (IOException e) {
e.printStackTrace();
}
}
class Client implements Runnable {
Socket soc;
boolean bl;
DataInputStream dis = null;
DataOutputStream dos = null;
Client(Socket s) {
soc = s;
bl = true;
}
public void send(String str) {
try {
dos = new DataOutputStream(soc.getOutputStream());
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (bl) {
dis = new DataInputStream(soc.getInputStream());
//接收客户端信息
String str = dis.readUTF();
ja.setText(ja.getText() + str + "\n");
for (Client c : clients) {
//发送信息给所有客户端
c.send(str);
}
}
} catch (IOException e) {
System.out.println("connect closed");
} finally {
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author lemon
* @date 2011/7/19
*/
public class ChatGuest extends JFrame {
JTextArea jta = new JTextArea(15, 40);
JTextField jtf = new JTextField("");
JComboBox<String> box = new JComboBox<>();
String[] string = new String[3];
Socket s = null;
boolean bl = false;
DataOutputStream dos = null;
DataInputStream dis = null;
public static void main(String[] args) {
new ChatGuest().launchFrame();
}
/**
* 创建用户界面
*/
public void launchFrame() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(d.width / 2, d.height / 10, 400, 400);
setLayout(new BorderLayout());
setVisible(true);
textArea();
add(box, BorderLayout.NORTH);
add(jta, BorderLayout.CENTER);
add(jtf, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
connect();
new Thread(new ReceiveThread()).start();
}
/**
* 设置输入显示框
*/
public void textArea() {
box.setBackground(Color.orange);
jta.setBackground(Color.yellow);
jta.setForeground(Color.red);
jtf.setBackground(Color.orange);
jtf.setForeground(Color.blue);
jtf.addKeyListener(new JtaListener());
try {
InetAddress as = InetAddress.getLocalHost();
string[0] = as.getHostAddress();
string[1] = as.getHostName();
string[2] = System.getProperty("os.name");
box.addItem("您的ip地址是" + string[0]);
box.addItem("您的爱机是" + string[1]);
box.addItem("您的操作系统是" + string[2]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void connect() {
try {
//建立与服务端连接,这里ip是本机,端口:8880,可更改
s = new Socket("Localhost", 8880);
bl = true;
jtf.setText("本机连接成功");
} catch (IOException e) {
e.printStackTrace();
}
}
class JtaListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
dos = new DataOutputStream(s.getOutputStream());
//发送给服务端信息
dos.writeUTF(jtf.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.setText("");
if (jta.getLineCount() == 6) {
jta.setText("");
}
}
}
}
class ReceiveThread implements Runnable {
@Override
public void run() {
try {
//接收服务端信息
dis = new DataInputStream(s.getInputStream());
while (bl) {
String str = dis.readUTF();
jta.setText(jta.getText() + str + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}