目录
问题描述:
编写一个简易的套接字通信的简易聊天程序,要求:程序的界面必须是
GUI
,服务端必须以多线程的方式处理客户请求。
配置环境:
eclipse、Java8
目录结构:
UML类图分析:
效果展示:
代码:
Chat是主窗口,通过登录来确定姓名和密码(姓名用来作为交流代号,密码未设置用处)
Client是客户端,每个Chat可以触发一次客户端
Sever是服务器端,端口号固定,每次连接不同的客户端相当于开启不同的线程
SeverThread是服务端开启的线程,控制不同客户端的信息
在该程序中,是通过主窗口来调用不同的客户端,通过客户端连接服务器的一条新线程,来进行信息的传递
而在每个客户端之间进行的信息的展示是通过重写每次的信息来达到信息的交流完整。
// Chat.java
package gui4_2_2;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Chat {
static JTextField jtext0,jtext1;
static JButton jb0;
static String jt0,jt1;
public static void main(String[] args) {
JFrame jf = new JFrame("登录聊天室");
jf.setSize(300, 300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jp0 = new JPanel(new FlowLayout(FlowLayout.CENTER));
jp0.add(new JLabel("请输入名字:"));
jtext0 = new JTextField(10);
jp0.add(jtext0);
JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
jp1.add(new JLabel("请输入密码:"));
jtext1 = new JTextField(10);
jp1.add(jtext1);
// 第 3 个 JPanel, 使用浮动布局, 并且容器内组件居中显示
JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
jb0 = new JButton("确认进入聊天室");
jp2.add(jb0);
jb0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jt0 =jtext0.getText();
jt1=jtext1.getText();
System.out.println(Integer.valueOf(jt1).intValue());
if(Integer.valueOf(jt1).intValue()<10000) {
JOptionPane.showMessageDialog(null, "密码少于5位哦!");
}else{
new Client(jt0);
}
}
});
// 创建一个垂直盒子容器, 把上面 3 个 JPanel 串起来作为内容面板添加到窗口
Box vBox = Box.createVerticalBox();
vBox.add(jp0);
vBox.add(jp1);
vBox.add(jp2);
jf.setContentPane(vBox);
// jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}
// Client.java
package gui4_2_2;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextArea textArea;//显示信息的文本区
Socket socket;
private String name;
public Client(String name){
this.name = name;
try {
socket = new Socket("localhost",9999);
} catch (IOException e) {
e.printStackTrace();
}
loadframe();
new Thread(this).start();//开启接受消息的线程
}
public void loadframe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 733, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 10, 697, 276);
contentPane.add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(10, 296, 565, 50);
contentPane.add(textArea_1);
JButton btn0 = new JButton("发送");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendmessage(textArea_1.getText());
textArea_1.setText(null);
}
});
btn0.setBounds(580, 310, 97, 23);
contentPane.add(btn0);
setVisible(true);
}
public void run(){
while (true){
acceptmessage();
}
}
public void sendmessage(String message){
try {
OutputStream write = socket.getOutputStream();
write.write((name+"说: "+message).getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void acceptmessage(){
String message=null;
try {
InputStream read = socket.getInputStream();
byte[] bytes = new byte[1024];
while (read.read(bytes)!=-1){
message = new String(bytes);
bytes = new byte[1024];
System.out.println(message);
textArea.setText(textArea.getText()+"\n"+message);
}
textArea.setText(textArea.getText()+"\n"+message);
} catch (IOException e) {
e.printStackTrace();
}
}
// public static void main(String[] args) {
// new Client();
// }
}
//Sever.java
package gui4_2_2;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class Sever implements Runnable{
ArrayList<Socket> sockets = new ArrayList<>();
int port;
ServerSocket socket;
public Sever() {
}
public Sever(int port) {
this.port = port;
try {
System.out.println("服务器ip"+InetAddress.getLocalHost()+"\n服务器端口号"+port);
try {
socket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void run(){
while (true){
try {
Socket accept = socket.accept();
synchronized (accept){
sockets.add(accept);
}
System.out.println(accept);
Thread t = new Thread(new SeverThread(accept,sockets));
t.start();
} catch (IOException e) {
}
}
}
public static void main(String[] args) {
new Thread(new Sever(9999)).start();
}
}
// SeverThread.java
package gui4_2_2;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
public class SeverThread extends Sever{
Socket socket;
public SeverThread(Socket socket,ArrayList<Socket> sockets) {
this.socket = socket;
super.sockets = sockets;
}
public void run(){
while (true)
accepmessage();
}
public void accepmessage() {
try {
InputStream reader = socket.getInputStream();
String acceptmessage=String.valueOf(socket.getPort());
byte[] bytes = new byte[1024];
int len;
while ((len=reader.read(bytes) )!= -1) {
// acceptmessage="端口号为"+String.valueOf(socket.getPort())+"发送的信息: ";
acceptmessage="";
acceptmessage += new String(bytes,0,len);
sendmessage(acceptmessage);
bytes = new byte[1024];
System.out.println(acceptmessage);
}
} catch (Exception e) {
try {
socket.close();
} catch (IOException ioException) {
}
sockets.remove(socket);
}
}
public void sendmessage(String message){
OutputStream write1 = null;
for (Socket socket1 : sockets) {
try {
write1 = socket1.getOutputStream();
write1.write(message.getBytes());
} catch (IOException e) {
}
}
}
}
参考资料:
(97条消息) Java Swing 图形界面开发(目录)_xietansheng的博客-CSDN博客_swing
(97条消息) JavaTCP协议实现多线程模式多人聊天,开启服务端后,只需要开启多个客户端的类,就可以进行多人聊天了,Java网络编程_程东南的博客-CSDN博客_java tcp 多线程
版权声明:本文为www2429原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。