Server服务器文件
public class Server extends Thread {
private ServerSocket serverSocket = null;//定义服务器
private ServerThread serverThread = null;
private JTextArea area = null;
// 创建一个客户端列表
private ArrayList<ServerThread> clientList = null;
//构造函数
public Server(int port,JTextArea area) {
this.area=area;
try {
//绑定端口
serverSocket = new ServerSocket(port);
clientList = new ArrayList<ServerThread>();
} catch (Exception e) {
System.out.println("端口绑定失败");
}
System.out.println("绑定成功");
start();//启动线程
area.setText("绑定成功");
}
// 服务器发送消息
public void sendMessage(String msg) {
if(clientList!=null){
for (int i = 0; i < clientList.size(); i++) {
clientList.get(i).sendMessage(msg);
}
}
}
// 服务等待客户端连接的线程
//线程程序
public void run() {
// 创建客户端Socket
while (true) {
try {
// 等待接收客户端的连接
Socket socket = serverSocket.accept();
// 接下来开启一条线程为该客户服务
serverThread = new ServerThread(socket);
serverThread.start();
clientList.add(serverThread);// 将新客户加入列表库
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 服务线程(内部类),用于处理客户端服务线程
class ServerThread extends Thread {
// 属性
private Socket socket = null;// 客户端的socket
private PrintWriter writer = null;// 用于通信用的输入输出流,用于向服务器的读取和发送数据
private BufferedReader reader = null;// 用于通信用的输入输出流,用于向服务器的读取和发送数据
// 构造器
public ServerThread(Socket socket) {
this.socket = socket;
try {
// 对Reader建立连接,用于读取数据
InputStream input = socket.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input);
reader = new BufferedReader(inputReader);
// 对writer建立连接,用于发送数据
OutputStream output = socket.getOutputStream();
writer = new PrintWriter(output);
} catch (IOException e) {
e.printStackTrace();
System.out.println("通信失败");
}
}
// 发送消息
public void sendMessage(String msg) {
if (writer != null) {
writer.println(msg);
writer.flush();
}
}
// 在线程中通信,主要用于接收消息
public void run() {
while(true){
// 创建变量,用于接收消息
String message = "";
try {
//读取输入流
message = reader.readLine();
if (message.equals("byebye")) {
// 接收到终止通信指令
reader.close();
writer.close();
socket.close();
} else {
// 将接收到的消息打印出来
System.out.println(message);
area.append(message+"\n");
//广播消息
if(clientList!=null){
for (int i = 0; i < clientList.size(); i++) {
clientList.get(i).sendMessage(message);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Client客户端文件
public class Client extends Thread {
private Socket socket = null;
private PrintWriter writer = null;
private BufferedReader reader = null;
private JTextArea area = null;
// 构造函数
public Client(String ip, int port, JTextArea area) {
this.area = area;
// 连接服务器
try {
socket = new Socket(ip, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
start();
System.out.println("连接服务器成功");
}
// 发送消息
public void sendMsg(String msg) {
writer.println(msg);
writer.flush();
}
// 线程,用于接收消息
public void run() {
try {
InputStream iStream = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(iStream);
reader = new BufferedReader(isr);
writer = new PrintWriter(socket.getOutputStream(), true);
while (true) {
//不停读取从服务器发来的信息
String message = reader.readLine();
if (!message.equals("")) {
System.out.println(message);
area.append(message + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
//扫尾
finally {
try {
writer.close();
reader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Mywindow界面文件
public class MyWindows extends JFrame {
// 屬性
private JTextArea area = null;// 用於到接收消息的顯示屏
private boolean isServer;
private Server server = null;
private Client client = null;
private String windowName = "";
// 方法
public MyWindows(String name, boolean isServer) {
this.isServer = isServer;
this.setTitle(name);
windowName = name;
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
initComponents();
setVisible(true);
}
// 網絡初始化
public void setNet(String ip, int port) {
if (isServer) {
server = new Server(port, area);
} else {
client = new Client(ip, port, area);
}
}
public void initComponents() {
// 接收顯示屏
area = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(area);
jScrollPane.setBounds(10, 10, 260, 200);
area.setLineWrap(true);
this.add(jScrollPane);
// 發送編輯框
final JTextField text = new JTextField();
text.setBounds(10, 240, 260, 30);
add(text);
// 發送按鈕
JButton button = new JButton("發送 ");
button.setBounds(10, 290, 80, 30);
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String msg = text.getText();
if (isServer) {
server.sendMessage(windowName + msg);
} else {
client.sendMsg(windowName + msg);
}
}
});
}
}
Javamain程序入口
public class JavaMain {
public static void main(String[] args) {
MyWindows w1 = new MyWindows("服务器", true);
w1.setNet("", 12345);
MyWindows w2 = new MyWindows("客户端", false);
w2.setNet("127.0.0.1", 12345);
MyWindows w3 = new MyWindows("客户端2", false);
w3.setNet("127.0.0.1", 12345);
}
}
效果图![](https://img-blog.csdn.net/20170809225959653?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRnVsbFFpYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)