java网络编程,通过TCP,Socket实现多对一的局域网聊天室
可以实现多个客户端连接服务器,服务器接收到信息就会把信息广播到所有的客户端
这是服务器端的代码
View Code
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.Socket; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /*这个类是服务器端的UI*/ public class ServerUI extends JFrame { public static void main(String[] args) { ServerUI serverUI = new ServerUI(); } public JButton btStart;//启动服务器 public JButton btSend;//发送信息按钮 public JTextField tfSend;//需要发送的文本信息 public JTextArea taShow;//信息展示 public Server server;//用来监听客户端连接 static List<Socket> clients;//保存连接到服务器的客户端 public ServerUI() { super("服务器端"); btStart = new JButton("启动服务"); btSend = new JButton("发送信息"); tfSend = new JTextField(10); taShow = new JTextArea(); btStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server = new Server(ServerUI.this); } }); btSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server.sendMsg(tfSend.getText()); tfSend.setText(""); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", JOptionPane.YES_NO_OPTION); if (a == 1) { server.closeServer(); System.exit(0); // 关闭 } } }); JPanel top = new JPanel(new FlowLayout()); top.add(tfSend); top.add(btSend); top.add(btStart); this.add(top, BorderLayout.SOUTH); final JScrollPane sp = new JScrollPane(); sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); sp.setViewportView(this.taShow); this.taShow.setEditable(false); this.add(sp, BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400, 300); this.setLocation(100, 200); this.setVisible(true); } }
View Code
import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; /*这个类是服务器端的等待客户端连接*/ public class Server extends Thread { ServerUI ui; ServerSocket ss; BufferedReader reader; PrintWriter writer; public Server(ServerUI ui) { this.ui = ui; this.start(); } public void run() { try { ss = new ServerSocket(1228); ui.clients=new ArrayList<Socket>(); println("启动服务器成功:端口1228"); while (true) { println("等待客户端"); Socket client = ss.accept(); ui.clients.add(client); println("连接成功" + client.toString()); new ListenerClient(ui, client); } } catch (IOException e) { println("启动服务器失败:端口1228"); println(e.toString()); e.printStackTrace(); } } public synchronized void sendMsg(String msg) { try { for (int i = 0; i < ui.clients.size(); i++) { Socket client = ui.clients.get(i); writer = new PrintWriter(client.getOutputStream(), true); writer.println(msg); } } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } public void closeServer() { try { if (ss != null) ss.close(); if (reader != null) reader.close(); if (writer != null) writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
View Code
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /*这个类是服务器端的等待客户端发送信息*/ public class ListenerClient extends Thread { BufferedReader reader; PrintWriter writer; ServerUI ui; Socket client; public ListenerClient(ServerUI ui, Socket client) { this.ui = ui; this.client=client; this.start(); } //为每一个客户端创建线程等待接收信息,然后把信息广播出去 public void run() { String msg = ""; while (true) { try { reader = new BufferedReader(new InputStreamReader( client.getInputStream())); writer = new PrintWriter(client.getOutputStream(), true); msg = reader.readLine(); sendMsg(msg); } catch (IOException e) { println(e.toString()); // e.printStackTrace(); break; } if (msg != null && msg.trim() != "") { println(">>" + msg); } } } //把信息广播到所有用户 public synchronized void sendMsg(String msg) { try { for (int i = 0; i < ui.clients.size(); i++) { Socket client = ui.clients.get(i); writer = new PrintWriter(client.getOutputStream(), true); writer.println(msg); } } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } }
客户端代码
View Code
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ClientUI extends JFrame { public static void main(String[] args) { ClientUI client = new ClientUI(); } public ClientUI() { super("客户端"); btStart = new JButton("启动连接"); btSend = new JButton("发送信息"); tfSend = new JTextField(10); tfIP = new JTextField(10); tfPost = new JTextField(5); taShow = new JTextArea(); btStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server = new ClientThread(ClientUI.this); } }); btSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { server.sendMsg(tfSend.getText()); tfSend.setText(""); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", JOptionPane.YES_NO_OPTION); if (a == 1) { System.exit(0); // 关闭 } } }); JPanel top = new JPanel(new FlowLayout()); top.add(tfSend); top.add(btSend); top.add(btStart); this.add(top, BorderLayout.SOUTH); final JScrollPane sp = new JScrollPane(); sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); sp.setViewportView(this.taShow); this.taShow.setEditable(false); this.add(sp, BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400, 300); this.setLocation(600, 200); this.setVisible(true); } public JButton btStart; public JButton btSend; public JTextField tfSend; public JTextField tfIP; public JTextField tfPost; public JTextArea taShow; public ClientThread server; }
View Code
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ClientThread extends Thread { ClientUI ui; Socket client; BufferedReader reader; PrintWriter writer; public ClientThread(ClientUI ui) { this.ui = ui; try { client = new Socket("127.0.0.1", 1228);//这里设置连接服务器端的IP的端口 println("连接服务器成功:端口1228"); reader = new BufferedReader(new InputStreamReader( client.getInputStream())); writer = new PrintWriter(client.getOutputStream(), true); // 如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区 } catch (IOException e) { println("连接服务器失败:端口1228"); println(e.toString()); e.printStackTrace(); } this.start(); } public void run() { String msg = ""; while (true) { try { msg = reader.readLine(); } catch (IOException e) { println("服务器断开连接"); break; } if (msg != null && msg.trim() != "") { println(">>" + msg); } } } public void sendMsg(String msg) { try { writer.println(msg); } catch (Exception e) { println(e.toString()); } } public void println(String s) { if (s != null) { this.ui.taShow.setText(this.ui.taShow.getText() + s + "\n"); System.out.println(s + "\n"); } } }
相关推荐
广工 java与计算机网络的作业大作业,java聊天室,需要的自取
用java socket编写的局域网聊天和文件传送,类似飞秋,但没那么多功能,目前实现的功能有:聊天,发送窗口抖动,传送文件,其中传送文件采用TCP实现,其他的用UDP实现。本文件包含源代码和用到的jar包,代码结构清晰...
Java代码写的基于TCP和Socket编程的局域网多线程多人聊天室(Java网络编程)实训小例子。
详细的博客描述:https://blog.csdn.net/qq_43757282/article/details/108694697 此资源包括源代码和可执行的exe文件。
基于TCP SOCKET实现的局域网聊天室
VC++实现的基于SOCKET的简易网络聊天室,服务器端定义端口号,多个客户端输入服务器端IP和端口号即可进入聊天室
Java基于局域网的聊天室系统(源代码+论文) Java基于局域网的聊天室系统(源代码+论文) Java基于局域网的聊天室系统(源代码+论文) Java基于局域网的聊天室系统(源代码+论文) Java基于局域网的聊天室系统(源代码+论文) ...
采用C/S架构,主函数在template\localChat下,客户端client.c和服务端server.c。 template下Makefile进行的是lib和...主要知识点:socket套接字、链表用户管理、线程创建管理、IO复用、select监听套接字、文件读写等。
利用Socket API编程实现基于网络聊天室 2.系统分为服务端和客户端两部分: 2.1 客户端向局域网广播自己的消息,包括用户昵称、消息等; 2.2 客户端启动后可以接收广播信息, 2.3 想办法使客户端能够了解其它...
本文是该系列第一篇,主要讲述了基于套接字(Socket)进行网络编程的基本概念,其中包括TCP协议、套接字、聊天程序的三种开发模式,以及两个基本操作:侦听端口、连接远程服务端;第二篇讲述了一个简单的范例:从...
项目名称 基于TCP协议模型的聊天室 项目功能 支持最多100人同时在线聊天,要求每个客户端登陆时需要输入昵称,然后发送任意想说的内容 项目的架构和分析 采用C/S架构进行设计
这个聊天室中,使用了TCP/IP socket 网络编程,多线程,使用Linux(ubantu)MySQL。
基于TCP/IP协议的一对多的通讯实例。实现局域网内的多台计算机的实时信息交互的聊天室功能(外网未测试)。主要使用多线程加侦听实现,服务器负责转发所有客户端的信息。
然后分多次发送这个jpg图像,每次大小为2KB(虽然socket是TCP的,但我仍让它多次发送),服务器端接收完所有的分片后,根据这段jpg内存流创建图像,然后显示,显示完后再发送给客户端一条最开始的那条要监控你的消息...
基于控制台的局域网聊天室程序,用C++编写,VC++6.0 VC++2010编译通过 2.程序中调用了windows API函数,利用了WinSock以及多线程等编程技巧 3.程序采用TCP-IP协议的C/S模式,并且服务端与客户端使用同一程序中 4.程序...
在网络编程中对信息的读取、发送,是利用流来实现信息的交换,其中介绍了对实现一个系统的信息流的分析,包含了一些基本的软件工程的方法。经过分析这些情况,该局域网聊天工具采用Eclipse为基本开发环境和java语言...
实现多人在线聊天功能,基于java开发。局域网亲测可用,适合研究学习
一个简单的聊天室, 从程序员的观点来看就是在多个I/O端点之间实现多对多的通信。基于SOCKET的局域网通信是一种灵活的、易于实现的、低成本的方法。它可以运行在各种使用TCP/IP协议作为通讯协议的网络上。而在SOCKET ...
本程序是基于TCP实现的简单局域网聊天室源代码.
自己写的基于TCP协议的MFC多线程聊天室,用的是阻塞SOCKET,下载后请先看说明文件。