局域网聊天系统-Java

news/2024/11/17 3:37:13/

这个局域网聊天系统,是为了能够在一个没有连接到intent的局域网中,进行聊天的一个程序。本程序包括服务器和客户机两部分,可以实现多人聊天和两个人私聊的一个工具。
开发语言:Java,开发工具:eclipse。
客户机代码如下:
Client.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;public class Client{//客户端JFrame frame;JList<String> userList;//列表组件JTextArea textArea;//文本域JTextField textField;JTextField txt_port,txt_Ip,txt_name;JButton button_start,button_stop,button_send;//连接按钮,断开按钮,发送按钮JPanel north,south;//上、下面板JScrollPane rightScroll,leftScroll;//左右滚动面板JSplitPane centerSplit;//分割面板DefaultListModel<String> listModel;//点击事件private boolean crossFire = false;    Socket socket;//套接字PrintWriter writer;BufferedReader reader;MessageThread messageThread;// 负责接收消息的线程private Map<String, User> onLineUsers = new HashMap<String, User>();// 所有在线用户集合public void send() {//发送方法if (!crossFire) {JOptionPane.showMessageDialog(frame, "Warning!无法连接服务器", "错误",JOptionPane.ERROR_MESSAGE);return;}String message = textField.getText().trim();if (message == null || message.equals("")) {JOptionPane.showMessageDialog(frame, "Warning! 消息不能为空!", "错误",JOptionPane.ERROR_MESSAGE);return;}sendMessage(frame.getTitle() + "@" + "ALL" + "@" + message);textField.setText(null);}public Client() {//创建构造方法textArea = new JTextArea();textArea.setEditable(false);//设置文本域不可读textArea.setForeground(Color.RED);//设置背景颜色位红色textField = new JTextField();txt_port = new JTextField();//端口号txt_Ip = new JTextField();//服务器IPtxt_name = new JTextField();//用户名button_start = new JButton("连接");button_stop = new JButton("断开");button_send = new JButton("发送");listModel = new DefaultListModel<String>();userList = new JList<String>(listModel);north = new JPanel();north.setLayout(new GridLayout(1, 4));//设置布局方式north.add(new JLabel("端口"));north.add(txt_port);//在JPanel组件中添加端口组件north.add(new JLabel("服务器IP:"));north.add(txt_Ip);//在JPanel组件中添加IP组件 north.add(new JLabel("姓名:"));north.add(txt_name);//添加文本域组件north.add(button_start);//添加按钮组件north.add(button_stop);//添加按钮组件north.setBorder(new TitledBorder("连接信息"));rightScroll = new JScrollPane(textArea);rightScroll.setBorder(new TitledBorder("聊天记录区"));leftScroll = new JScrollPane(userList);leftScroll.setBorder(new TitledBorder("在线用户明细"));//设置组件的边框south = new JPanel(new BorderLayout());south.add(textField, "Center");south.add(button_send, "East");south.setBorder(new TitledBorder("写消息"));centerSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScroll,rightScroll);centerSplit.setDividerLocation(100);//设置分隔条的相对位置frame = new JFrame("客户端");frame.add(north, "North");frame.add(centerSplit, "Center");frame.add(south, "South");frame.setSize(750, 350);int screen_width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽度int screen_height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高度frame.setLocation((screen_width - frame.getWidth()) / 2,(screen_height - frame.getHeight()) / 2);//设置窗口的相对位置frame.setVisible(true);//设置组件可见// 写消息的文本框中按回车键时事件textField.addActionListener(new ActionListener() {//添加监听,监听文本域消息public void actionPerformed(ActionEvent arg0) {send();}});// 单击发送按钮时事件button_send.addActionListener(new ActionListener() {//添加监听,监听发送按钮消息public void actionPerformed(ActionEvent e) {send();}});// 单击连接按钮时事件button_start.addActionListener(new ActionListener() {//添加监听,监听连接按钮消息public void actionPerformed(ActionEvent e) {int port;if (crossFire) {JOptionPane.showMessageDialog(frame, "WARNING!  重复链接","错误", JOptionPane.ERROR_MESSAGE);return;}try {try {port = Integer.parseInt(txt_port.getText().trim());} catch (NumberFormatException e2) {throw new Exception("端口号错误!   端口应为整数!");}String hostIp = txt_Ip.getText().trim();String name = txt_name.getText().trim();if (name.equals("")) {throw new Exception("姓名不能为空!");}if(hostIp.equals(""))throw new Exception("服务器IP不能为空!");boolean flag = connectServer(port, hostIp, name);if (flag == false) {throw new Exception("无连接,或连接失败!");}frame.setTitle(name);JOptionPane.showMessageDialog(frame, "成功连接!");} catch (Exception exc) {JOptionPane.showMessageDialog(frame, exc.getMessage(),"错误", JOptionPane.ERROR_MESSAGE);}}});// 单击断开按钮时事件button_stop.addActionListener(new ActionListener() {//添加监听,监听断开按钮消息public void actionPerformed(ActionEvent e) {if (!crossFire) {JOptionPane.showMessageDialog(frame, "Warning!重复连接","错误", JOptionPane.ERROR_MESSAGE);return;}try {boolean flag = closeConnection();// 断开连接if (flag == false) {throw new Exception("断开连接发生异常!");}JOptionPane.showMessageDialog(frame, "已断开!");} catch (Exception exc) {JOptionPane.showMessageDialog(frame, exc.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);}}});// 关闭窗口时事件frame.addWindowListener(new WindowAdapter() {//添加窗口监听public void windowClosing(WindowEvent e) {if (crossFire) {closeConnection();//关闭连接}System.exit(0);//退出程序}});}public boolean connectServer(int port, String hostIp, String name) {//连接服务器try {socket = new Socket(hostIp, port);// 根据端口号和服务器Ip建立连接writer = new PrintWriter(socket.getOutputStream());reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));sendMessage(name + "@" + socket.getLocalAddress().toString()); //发送客户端用户基本信息(用户名和ip地址)messageThread = new MessageThread(reader, textArea);//开启接收消息的线程messageThread.start();crossFire = true;//已经连接上了return true;} catch (Exception e) {textArea.append("与端口号为:" + port + "    IP地址为:" + hostIp+ "   的服务器连接失败!" + "\r\n");crossFire = false;//未连接上return false;}}public void sendMessage(String message) {writer.println(message);writer.flush();}@SuppressWarnings("deprecation")public synchronized boolean closeConnection() {//同步方法try {sendMessage("CLOSE");//发送断开连接命令给服务器messageThread.stop();//停止接受消息线程reader.close();//释放资源writer.close();socket.close();crossFire=false;return true;} catch (IOException e1) {e1.printStackTrace();crossFire = true;return false;}}class MessageThread extends Thread {//接收消息线程private BufferedReader reader;private JTextArea textArea;public MessageThread(BufferedReader reader, JTextArea textArea) {//创建线程的构造方法this.reader = reader;this.textArea = textArea;}public synchronized void closeCon() throws Exception {//同步方法listModel.removeAllElements();// 清空用户列表reader.close();//关闭连接释放资源writer.close();     socket.close();crossFire = false;//修改状态为断开}@SuppressWarnings("unlikely-arg-type")public void run() {String message = "";while (true) {try {message = reader.readLine();StringTokenizer stringTokenizer = new StringTokenizer(message, "/@");//分解字符串String command = stringTokenizer.nextToken();//返回下一个if (command.equals("CLOSE")){// 服务器已关闭命令textArea.append("服务器已关闭!\r\n");closeCon();//关闭连接return;//结束线程} else if (command.equals("ADD")) {//有用户上线更新在线列表String username;String userIp;if ((username = stringTokenizer.nextToken()) != null&& (userIp = stringTokenizer.nextToken()) != null) {User user = new User(username, userIp);onLineUsers.put(username, user);listModel.addElement(username);}} else if (command.equals("DELETE")) {//有用户下线更新在线列表String username = stringTokenizer.nextToken();User user = (User) onLineUsers.get(username);onLineUsers.remove(user);//删除下线用户名及iplistModel.removeElement(username);//删除在线列表中用户名} else if (command.equals("USERLIST")) {// 加载在线用户列表int size = Integer.parseInt(stringTokenizer.nextToken());//使用下一个参数指定的基数,将字符串参数解析为有符号的整数String username = null;String userIp = null;for (int i = 0; i < size; i++) {username = stringTokenizer.nextToken();userIp = stringTokenizer.nextToken();User user = new User(username, userIp);onLineUsers.put(username, user);//更新在线用户listModel.addElement(username);//添加用户名}} else if (command.equals("MAX")) {//人数达到上限textArea.append(stringTokenizer.nextToken()+ stringTokenizer.nextToken() + "\r\n");//将用户名及用户IP添加到文本域,提示用户达到上限closeCon();//关闭连接JOptionPane.showMessageDialog(frame, "服务器缓冲区已满!", "错误",JOptionPane.ERROR_MESSAGE);//消息对话框return;// 结束线程} else {//消息textArea.append(message + "\r\n");}} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}}public static void main(String[] args) {new Client();}}

服务器代码如下:
Sever.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.StringTokenizer;import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;public class Sever  {JFrame frame;JTextArea contentArea;  //文本域JTextField txt_message,txt_max,txt_port;  //用于显示文本信息,设置最大连接人数,设置端口号JButton button_start,button_stop ,button_send;//启动按钮,停止按钮,发送按钮JPanel north,south;        //上面面板,下面面板JScrollPane right,left; //左边滚动面板,右边滚动面板JSplitPane centerSplit; //分割面板JList<String> userList;         //列表组件DefaultListModel<String> listModel;//点击事件ServerSocket serverSocket;//创建套接字                                        ServerThread serverThread;//创建线程private ArrayList<ClientThread> clients;//创建客户端的集合private boolean Fire = false;//创建一个布尔值public void send() {//创建一个发送消息的方法sendif (!Fire) {//判断是是否启动服务器JOptionPane.showMessageDialog(frame, "服务器还未启动,不能发送消息!", "错误",JOptionPane.ERROR_MESSAGE);//类提示框return;}if (clients.size() == 0) {JOptionPane.showMessageDialog(frame, "没有用户在线,不能发送消息!", "错误",JOptionPane.ERROR_MESSAGE);//判断是否有用户在线return;}String message = txt_message.getText().trim();//文本框内容获取if (message == null || message.equals("")) {JOptionPane.showMessageDialog(frame, "消息不能为空!", "错误",JOptionPane.ERROR_MESSAGE);//判断消息框是否有文字return;}sendServerMessage(message);// 群发服务器消息(调用sendServerMessage方法)contentArea.append("服务器:" + txt_message.getText() + "\r\n");//在文本域添加服务器发送的新内容txt_message.setText(null);}public Sever() {//创建构造函数frame = new JFrame("服务器");//实例化一个服务器窗体对象contentArea = new JTextArea();//实例化文本域contentArea.setEditable(false);//设置文本为不可读状态contentArea.setForeground(Color.red);//设置背景颜色为红色txt_message = new JTextField();//消息编辑txt_max = new JTextField();//设置允许连接的最多人数txt_port = new JTextField();//设置端口号button_start = new JButton("启动");//创建启动按钮button_stop = new JButton("停止");//创建停止按钮button_send = new JButton("发送");//创建发送按钮button_stop.setEnabled(false);//设置按钮不可用listModel = new DefaultListModel<String>();//实例化鼠标点击userList = new JList<String>(listModel);//实例化列表框对象south = new JPanel(new BorderLayout());//实例化下面面板south.setBorder(new TitledBorder("写消息"));//文本按钮south.add(txt_message, "Center");//添加文本south.add(button_send, "East");//发送文本left = new JScrollPane(userList);//实例化左边滚动条,传入用户的名字left.setBorder(new TitledBorder("在线用户明细"));right = new JScrollPane(contentArea);//实例化右边滚动条,传入文本域right.setBorder(new TitledBorder("聊天记录区"));//添加滚动面板内容centerSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left,right);//实例化分割线 将面板分割成左右两部分centerSplit.setDividerLocation(100);//设置centerSplit的分隔线的相对位置north = new JPanel();//实例化上面面板north.setLayout(new GridLayout(1, 4));//窗口布局north.add(new JLabel("人数上限"));//JButton实例添加到JPanel中north.add(txt_max);//JButton实例添加到JPanel中north.add(new JLabel("端口"));//JButton实例添加到JPanel中north.add(txt_port);//JButton实例添加到JPanel中north.add(button_start);//JButton实例添加到JPanel中north.add(button_stop);//JButton实例添加到JPanel中north.setBorder(new TitledBorder("配置信息"));frame.setLayout(new BorderLayout());//容器使用绝对布局frame.add(north, "North");//添加上面板组件frame.add(centerSplit, "Center");//添加分割线组件frame.add(south, "South");//条件下面板组件frame.setSize(750, 350);//设置组件的宽和高int s_width=Toolkit.getDefaultToolkit().getScreenSize().width;//获取电脑屏幕的宽int s_height=Toolkit.getDefaultToolkit().getScreenSize().height;//获取电脑屏幕的高frame.setLocation((s_width - frame.getWidth()) / 2,(s_height - frame.getHeight()) / 2);//设置组件左上角的坐标,将其放在屏幕中间frame.setVisible(true);//设置窗体可见frame.addWindowListener(new WindowAdapter() {//添加窗体监听事件public void windowClosing(WindowEvent e) {// 窗口关闭时触发if (Fire) {closeServer();// 关闭服务器}System.exit(0);// 退出程序}});txt_message.addActionListener(new ActionListener() {//添加监听,监听文本框按回车键按钮事件public void actionPerformed(ActionEvent e) {send();}});button_send.addActionListener(new ActionListener() {//添加监听,监听发送按钮事件public void actionPerformed(ActionEvent arg0) {send();}});button_start.addActionListener(new ActionListener() {//添加监听,监听启动服务器按钮时事件public void actionPerformed(ActionEvent e) {if (Fire) {JOptionPane.showMessageDialog(frame, "Warning!重复启动","错误", JOptionPane.ERROR_MESSAGE);return;}int max = 0;//最大人数int port = 0;//端口号try {try {max = Integer.parseInt(txt_max.getText());} catch (Exception e1) {throw new Exception("人数上限不能为空");}if (max <= 0) {throw new Exception("人数上限应为正整数!");}try {port = Integer.parseInt(txt_port.getText());} catch (Exception e1) {throw new Exception("端口号应为整数!");}if (port <= 0) {throw new Exception("端口号应为正整数!");}	serverStart(max, port);contentArea.append("服务器已成功启动!人数上限:" + max + ",端口:" + port+ "\r\n");JOptionPane.showMessageDialog(frame, "服务器成功启动!");button_start.setEnabled(false);//设置按钮不可用txt_max.setEnabled(true);txt_port.setEnabled(true);button_stop.setEnabled(true);//设置按钮可用} catch (Exception exc) {JOptionPane.showMessageDialog(frame, exc.getMessage(),"错误", JOptionPane.ERROR_MESSAGE);}}});// 单击停止服务器按钮时事件button_stop.addActionListener(new ActionListener() {//添加监听停止按钮时间public void actionPerformed(ActionEvent e) {if (!Fire) {JOptionPane.showMessageDialog(frame, "服务器还未启动,无需停止!", "错误",JOptionPane.ERROR_MESSAGE);return;}try {closeServer();button_start.setEnabled(true);txt_max.setEnabled(true);txt_port.setEnabled(true);button_stop.setEnabled(false);contentArea.append("服务器成功停止!\r\n");JOptionPane.showMessageDialog(frame, "服务器成功停止!");} catch (Exception exc) {JOptionPane.showMessageDialog(frame, "停止服务器发生异常!", "错误",JOptionPane.ERROR_MESSAGE);}}});}// 启动服务器public void serverStart(int max, int port) throws java.net.BindException {try {clients = new ArrayList<ClientThread>();//创建一个客户端集合serverSocket = new ServerSocket(port);//套接字创建serverThread = new ServerThread(serverSocket, max);//实例化线程serverThread.start();//启动线程Fire = true;} catch (BindException e) {Fire = false;throw new BindException("端口号已被占用,请更换!");} catch (Exception e1) {e1.printStackTrace();Fire = false;throw new BindException("启动服务器异常!");}}@SuppressWarnings("deprecation")public void closeServer() { //关闭服务器方法try {if (serverThread != null)serverThread.stop();//关闭线程for (int i = clients.size() - 1; i >= 0; i--) {//创建for循环设置i的初始值位客户端数值减一// 给所有在线用户发送关闭命令clients.get(i).getWriter().println("CLOSE");clients.get(i).getWriter().flush();// 释放资源clients.get(i).stop();// 停止此条为客户端服务的线程clients.get(i).reader.close();clients.get(i).writer.close();clients.get(i).socket.close();clients.remove(i);}if (serverSocket != null) {serverSocket.close();//关闭服务器端连接}listModel.removeAllElements();//清空用户列表Fire = false;} catch (IOException e) {e.printStackTrace();Fire = true;}}public void sendServerMessage(String message) {// 群发服务器消息for (int i = clients.size() - 1; i >= 0; i--) {clients.get(i).getWriter().println("服务器:" + message + "(多人发送)");clients.get(i).getWriter().flush();}}class ServerThread extends Thread {//服务器线程private ServerSocket serverSocket;private int max;//人数上限public ServerThread(ServerSocket serverSocket, int max) {//服务器线程的构造方法this.serverSocket = serverSocket;this.max = max;}public void run() {//重写run方法while (true) {//不停的等待客户端的链接try {Socket socket = serverSocket.accept();//服务器套接字开启监听,返回Socket,抛出异常if (clients.size() == max) {// 如果已达人数上限BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));//将字节流转化成字符流,字节流的来源(套接字socket)PrintWriter w = new PrintWriter(socket.getOutputStream());//将传进来的值写入// 接收客户端的基本用户信息String inf = r.readLine();StringTokenizer st = new StringTokenizer(inf, "@");User user = new User(st.nextToken(), st.nextToken());//获取用户名和IP地址// 反馈连接成功信息w.println("服务器:对不起," + user.getName()+ user.getIp() + ",服务器在线人数已达上限,请稍后尝试连接!");w.flush();//将传进来的值写入r.close();// 释放资源w.close();socket.close();continue;}ClientThread client = new ClientThread(socket);client.start();//开启对此客户端服务的线程clients.add(client);listModel.addElement(client.getUser().getName());//更新在线列表contentArea.append(client.getUser().getName()+ client.getUser().getIp() + "上线!\r\n");} catch (IOException e) {e.printStackTrace();}}}}class ClientThread extends Thread {//为一个客户端服务的线程private Socket socket;private BufferedReader reader;private PrintWriter writer;private User user;//get方法public BufferedReader getReader() {return reader;}public PrintWriter getWriter() {return writer;}public User getUser() {return user;}public ClientThread(Socket socket) {//客户端线程的构造方法try {this.socket = socket;reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//将字节流转化成字符流,字节流的来源(套接字socket)writer = new PrintWriter(socket.getOutputStream());// 接收客户端的基本用户信息String inf = reader.readLine();//读取客户端字符串StringTokenizer st = new StringTokenizer(inf, "@");user = new User(st.nextToken(), st.nextToken());// 反馈连接成功信息writer.println(user.getName() + user.getIp() + "与服务器连接成功!");writer.flush();// 反馈当前在线用户信息if (clients.size() > 0) {String temp = "";for (int i = clients.size() - 1; i >= 0; i--) {temp += (clients.get(i).getUser().getName() + "/" + clients.get(i).getUser().getIp())+ "@";}writer.println("USERLIST@" + clients.size() + "@" + temp);writer.flush();}// 向所有在线用户发送该用户上线命令for (int i = clients.size() - 1; i >= 0; i--) {clients.get(i).getWriter().println("ADD@" + user.getName() + user.getIp());clients.get(i).getWriter().flush();}} catch (IOException e) {e.printStackTrace();}}@SuppressWarnings("deprecation")public void run() {//不断接收客户端的消息,进行处理。重写run方法String message = null;while (true) {try {message = reader.readLine();//接收客户端消息if (message.equals("CLOSE"))//下线命令{contentArea.append(this.getUser().getName()+ this.getUser().getIp() + "下线!\r\n");reader.close();writer.close();socket.close();//向所有在线用户发送该用户的下线命令for (int i = clients.size() - 1; i >= 0; i--) {clients.get(i).getWriter().println(  user.getName());clients.get(i).getWriter().flush();}listModel.removeElement(user.getName());//更新在线列表//删除此条客户端服务线程for (int i = clients.size() - 1; i >= 0; i--) {if (clients.get(i).getUser() == user) {ClientThread temp = clients.get(i);clients.remove(i);//删除此用户的服务线程temp.stop();//停止这条服务线程return;}}} else {dispatcherMessage(message);//转发消息}} catch (IOException e) {e.printStackTrace();}}}// 转发消息public void dispatcherMessage(String message) {StringTokenizer stringTokenizer = new StringTokenizer(message, "@");String source = stringTokenizer.nextToken();String owner = stringTokenizer.nextToken();String content = stringTokenizer.nextToken();message = source + ":" + content;contentArea.append(message + "\r\n");if (owner.equals("ALL")) {// 群发for (int i = clients.size() - 1; i >= 0; i--) {clients.get(i).getWriter().println(message + "(多人发送)");clients.get(i).getWriter().flush();}}}}public static void main(String[] args) {new Sever();//实例化}}

user类如下:
User.java

public class User{private String name;private String ip;//构造函数public User(String name, String ip) {this.name = name;this.ip = ip;}//get、set方法public String getName() {return name;}public void setName(String name) {this.name = name;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}
}

http://www.ppmy.cn/news/933711.html

相关文章

Java实现局域网聊天软件分享

前言 翻翻u盘里面的东西,发现了自己以前实现的局域网聊天软件。这里共享出来给各位学习 Java的童鞋。对知识的渴望源自于人的内心&#xff0c;对知识的分享也源自于人的本性。 功能罗列 啥功能笔者就不去重新实践了&#xff0c;简单的罗列下功能列表&#xff1a; 1. 消息发送&a…

Java实现多线程局域网聊天室

Java多线程局域网聊天室 局域网聊天室1.TCP2.套接字3.C/S架构4.多线程5.服务器客户端 局域网聊天室 在学习了一个学期的java以后&#xff0c;觉得java真是博大精深&#xff0c;彻底放弃了因为c而轻视java的心态&#xff0c;搞了一个多线程的聊天室&#xff0c;熟悉了一下服务器…

局域网内互发消息

主机相互发送消息 背景 本学期的计算机网络课非常有意思&#xff0c;它与我们的生活息息相关&#xff0c;曾经我也为电脑发生的网络问题而困惑&#xff0c;总是没有根据的瞎尝试来试图解决问题。但是在老师的一些简单指导之后&#xff0c;有了一个基本的ip之间通信以及局域网…

基于java编写局域网多人聊天室

基于java编写局域网多人聊天室 由于需要制作网络计算机网络课程设计&#xff0c;并且不想搞网络布线或者局域网路由器配置等等这种完全搞不懂的东西&#xff0c;最后决定使用socket基于java编写一个局域网聊天室&#xff1a; 关于socket以及网络编程的相关知识详见我另一篇文…

Java实战_局域网多人聊天室

由于需要制作网络计算机网络课程设计&#xff0c;并且不想搞网络布线或者局域网路由器配置等等这种完全搞不懂的东西&#xff0c;最后决定使用socket基于java编写一个局域网聊天室&#xff1a; 关于socket以及网络编程的相关知识详见我另一篇文章:Java基于socket编程 程序基于…

JAVA实现聊天室(局域网)

JAVA用局域网实现聊天室 类和接口 Server类&#xff08;服务器端&#xff09; package Test;import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket;public class Server {private static final int SERVER_PORT30…

基础聊天室的实现,实现同局域网的聊天,有详细代码

** 聊天室的实现&#xff0c;基本步骤解析 ** 总体思想结构 1.建立服务器端&#xff0c;服务器端不需要界面 建立一个包为服务器包 serve 包内应该有服务器的主方法用来启动服务器&#xff0c;和一个用来接收客户端消息并且转发给所有的客户端的线程类 服务器给所有客户端发消…

web网络聊天室

花了四天时间做了一个简单的web聊天室 前端页面效果大概是下面这个样子 1.登陆界面 2.注册界面 这里首先会检查用户名是否重复&#xff0c; 如果重复&#xff0c;会提示你用户名已存在 反正显示可用 3.聊天室界面 登陆成功会在左边显示登陆人的名称以及当前在线人数。 然后…