简单的群聊例子
分为服务器端和客户端
服务器端的代码
package com.gxuwz.fulunyong;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* @author:fulunyong
* @email:fulunyong@qq.com
* @datetime:2015年8月20日下午4:36:14
* @version 1.0
* @serial 简单JAVA群聊例子 服务器端
*/
public class ServerWindows extends JFrame{
private static final long serialVersionUID = 9150820189192868510L;
public static void main(String[] args){
new ServerWindows();
}
/***************************华丽丽的分割线**********************************/
public static List<Socket> sockets;//客户端列表
private static ServerSocket serverSocket;
public static JTextArea show;//消息显示框
private static JLabel label;
public static JTextField jTextField;
public ServerWindows(){
if (null==sockets) {
sockets=new ArrayList<Socket>();
}
setTitle("梧州学院聊天室-服务器端");//标题
setSize(600, 800);//
setLocationRelativeTo(null);//居中出现
setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭窗口时推出
//组件初始化
show=new JTextArea();
show.setEditable(false);
label=new JLabel("当前在线人数:");
jTextField=new JTextField();
jTextField.setEditable(false);
//添加到面板
JPanel jPanel1=new JPanel();
GridLayout gridLayout1=new GridLayout(1, 1);
jPanel1.setLayout(gridLayout1);
jPanel1.add(new JScrollPane(show));
JPanel jPanel2=new JPanel(new GridLayout(1, 5));
jPanel2.add(label);
jPanel2.add(jTextField);
add(jPanel1,BorderLayout.CENTER);
add(jPanel2,BorderLayout.SOUTH);
try {
serverSocket=new ServerSocket(5200);
} catch (IOException e){
e.printStackTrace();
}
show.append("服务器开启成功!等待客户端连接。。。。\n");
jTextField.setText(""+sockets.size());
setVisible(true);
while (true){
try {
Socket socket=serverSocket.accept();
if(!sockets.contains(socket)){
sockets.add(socket);
jTextField.setText(""+sockets.size());
for (Socket socket2 : sockets){
PrintWriter printWriter=new PrintWriter(socket2.getOutputStream());
printWriter.println("欢迎 "+socket.getInetAddress()+"加入聊天室");
printWriter.flush();
}
}
new ServerReceiveThread(socket).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ServerReceiveThread extends Thread{
private Socket socket;
public ServerReceiveThread(Socket socket){
this.socket=socket;
}
@Override
public void run(){
BufferedReader reader = null;
try {
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true){
String info=reader.readLine();
if (null!=info){
ServerWindows.show.append(info+"\n");
for (Socket socket : ServerWindows.sockets){
if (!socket.isClosed()){
PrintWriter printWriter=new PrintWriter(socket.getOutputStream());
printWriter.println(info);
printWriter.flush();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
ServerWindows.sockets.remove(socket);
ServerWindows.jTextField.setText(""+ServerWindows.sockets.size());
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}finally {
ServerWindows.sockets.remove(socket);
ServerWindows.jTextField.setText(""+ServerWindows.sockets.size());
try {
socket.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端的代码
package com.gxuwz.fulunyong;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* @author:fulunyong
* @email:fulunyong@qq.com
* @datetime:2015年8月20日下午4:36:14
* @version 1.0
* @serial 简单JAVA群聊例子 服务器端
*/
public class ServerWindows extends JFrame{
private static final long serialVersionUID = 9150820189192868510L;
public static void main(String[] args){
new ServerWindows();
}
/***************************华丽丽的分割线**********************************/
public static List<Socket> sockets;//客户端列表
private static ServerSocket serverSocket;
public static JTextArea show;//消息显示框
private static JLabel label;
public static JTextField jTextField;
public ServerWindows(){
if (null==sockets) {
sockets=new ArrayList<Socket>();
}
setTitle("梧州学院聊天室-服务器端");//标题
setSize(600, 800);//
setLocationRelativeTo(null);//居中出现
setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭窗口时推出
//组件初始化
show=new JTextArea();
show.setEditable(false);
label=new JLabel("当前在线人数:");
jTextField=new JTextField();
jTextField.setEditable(false);
//添加到面板
JPanel jPanel1=new JPanel();
GridLayout gridLayout1=new GridLayout(1, 1);
jPanel1.setLayout(gridLayout1);
jPanel1.add(new JScrollPane(show));
JPanel jPanel2=new JPanel(new GridLayout(1, 5));
jPanel2.add(label);
jPanel2.add(jTextField);
add(jPanel1,BorderLayout.CENTER);
add(jPanel2,BorderLayout.SOUTH);
try {
serverSocket=new ServerSocket(5200);
} catch (IOException e){
e.printStackTrace();
}
show.append("服务器开启成功!等待客户端连接。。。。\n");
jTextField.setText(""+sockets.size());
setVisible(true);
while (true){
try {
Socket socket=serverSocket.accept();
if(!sockets.contains(socket)){
sockets.add(socket);
jTextField.setText(""+sockets.size());
for (Socket socket2 : sockets){
PrintWriter printWriter=new PrintWriter(socket2.getOutputStream());
printWriter.println("欢迎 "+socket.getInetAddress()+"加入聊天室");
printWriter.flush();
}
}
new ServerReceiveThread(socket).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ServerReceiveThread extends Thread{
private Socket socket;
public ServerReceiveThread(Socket socket){
this.socket=socket;
}
@Override
public void run(){
BufferedReader reader = null;
try {
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true){
String info=reader.readLine();
if (null!=info){
ServerWindows.show.append(info+"\n");
for (Socket socket : ServerWindows.sockets){
if (!socket.isClosed()){
PrintWriter printWriter=new PrintWriter(socket.getOutputStream());
printWriter.println(info);
printWriter.flush();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
ServerWindows.sockets.remove(socket);
ServerWindows.jTextField.setText(""+ServerWindows.sockets.size());
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}finally {
ServerWindows.sockets.remove(socket);
ServerWindows.jTextField.setText(""+ServerWindows.sockets.size());
try {
socket.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
很简单的一个例子,希望各位大神指点指点。