




已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
XXXXXXXXXXXXX学院网络工程系网络应用开发综合实践基于TCP的java及时聊天系统设计报告姓 名:XXXXXXXX学 号:XXXXXXXXXXXX班 级:XXXXXXXXX指导老师:XXXXX完成时间:XXXXXXX基于TCP的java及时聊天系统摘要 TCP/IP网络体系结构和协议标准是近年来计算机网络研究和应用的热点技术,以它为基础组建的INTERNET已成为目前国际上规模最大的计算机网络系统。在我国, TCP/IP也越来越受到广泛关注,并得以多层次推广。在TCP/IP的模型中,传输层有两个并列的协议: TCP(Transport Control Protocol,传输控制协议)和UDP (User Datagram Protocol,用户数据报协议)。TCP和UDP共存于一个网络中, TCP是面向连接的,提供可靠性服务, UDP是无连接的,提供高效率服务。高可靠性的TCP通常用于一次传输要交换大量报文的情形,比如文件传输、远程登录等;高效率的UDP一般用于一次传输交换少量报文,尤其是交易型应用,比如数据库查询的情形,其可靠性由应用程序提供,因为交换次数不多,即便发生传输错误,必须重传,应用程序也不会为此付出太大的代价。 在网络越来越发达的今天,人们对网络的依赖越来越多,越来越离不开网络,由此而产生的聊天工具越来越多,网络聊天时一类的聊天系统的发展日新月异。本java及时聊天系统即是一个使用套接字(socket)实现C/S(client and server)模式的TCP/IP协议下连接的及时通信系统。 本论文主要分为六章:第一章引言,介绍本java及时聊天系统的需求分析,阐述了项目的开发背景、系统设计方法,明确了该设计的主要任务。第二章是系统的概要设计,详细描述了聊天系统的运行环境,系统的结构设计以及设计实现的基本功能。第三章描述了使用MyEclipse完成系统架构设计,实现简便切逻辑化的程序设计。第四章是系统的详细设计,详细介绍了系统的结构以及各个功能模块的设计细节。第五章是对本java及时聊天系统的测试结果报告,主要针对系统各功能模块进行细节测试。最后是本次网络应用开发综合实践的总结及体会。 关键词:java;TCP/IP;C/S;MyEclipse;及时通信abstract TCP/IP network system structure and protocol standards in recent years is to study and application of computer network by its hotspot technologies based INTERNET has become a current internationals largest computer network system. In our country, the TCP/IP is also more and more attention, and to multi-level promotion. On TCP/IP model, the Transport layer has two paratactic agreement: Transport Control Protocol (TCP, TCP and UDP (User Datagram) Protocol, User data reported Protocol). TCP and UDP coexist in a network, the TCP is connection-oriented, provide reliability service, UDP is no connection, with efficient service. High reliability of the TCP normally used for a transmission of messages to exchange the situation, such as file transfer, remote login, etc.; Efficient UDP general for a transmission message, especially for small amounts, such as transactional database query application situation, its reliability by applications provide, because few, even happen exchange frequency transmission error, necessitating retransmission, applications also wont pay the too big price. In network is more and more developed today, people depending on the Internet, more and more tied to the Internet, which produces more chatting tools, online chat chatting system when the development of a day. The Java timely chat system is a use socket (socket) to implement C/S (and server) mode are under the TCP/IP protocol connect timely communication system. This thesis mainly divided into six chapters: the first chapter preface, introduce the Java chat system timely requirement analysis, expounds the development background, system project design methods, clear the design of the main task. The second chapter is system of general design, describes the detailed chat the operation of the system environment, system design and implementation of structural design and basic function. The third chapter describes MyEclipse complete use of system architecture design, realization of simple cut logic programming. The fourth chapter is system of the detailed design, detailed introduction of system structure and modules design details. The fifth chapter is illustrative of the Java timely chat systems test results report, the function module mainly aimed at the system detail test. The last is the network application development and comprehensive the summary of practice and experience. Keywords: Java; TCP/IP; C/S;MyEclipse; Timely communication目 录摘要3abstract4一 引言71.1 背景71.2 需求分析7二 系统设计82.1 功能概述82.2 运行环境82.3 系统结构9三 系统架构设计93.1 整体设计93.2 服务器端设计103.3 客户端设计11四 详细设计114.1 聊天室服务器端设计114.1.1 server.java114.1.2 ServerListen.java174.1.3 ServerReceive.java184.1.4 PorConf.java214.1.5 Node.java234.1.6 UserLinkList.java234.1.7 Help.java254.2 聊天室客户端设计254.2.1 client.java264.2.2 ClientReceive.java314.2.3 ConnectConf.java324.2.4 UserConf.java33五 测试报告355.1 系统界面预览355.2 测试过程355.2.1服务器端端口设置及侦听功能355.2.2客户端连接功能365.2.2用户信息设置功能365.2.3用户登录/注销功能375.2.4聊天通信功能385.2.5帮助服务功能40六总结及体会41七参考文献411 引言1.1 背景在网络越来越发达的今天,人们对网络的依赖越来越多,越来越离不开网络,由此而产生的聊天工具越来越多,类似飞鸽等一类的聊天、文件传送系统的发展日新月异,应20102011学年第二学期网络应用开发综合实践课程要求,我选择制作一个局域网的网络聊天工具作为本次的综合实践课程;以求通过制作该系统能更好的学习网络编程及应用开发。本次选题为“基于TCP的java聊天系统”的目的就是指直接或间接地通过网络协议与其他计算机进行通讯。网络编程中有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输。在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机。而TCP层则提供面向应用的可靠的或非可靠的数据传输机制,这是网络编程的主要对象,一般不需要关心IP层是如何处理数据的。目前较为流行的网络编程模型是客户机/服务器(C/S)结构。即通信双方一方作为服务器等待客户提出请求并予以响应。客户则在需要服务时向服务器提出申请。服务器一般作为守护进程始终运行,监听网络端口,一旦有客户请求,就会启动一个服务进程来响应该客户,同时自己继续监听服务端口,使后来的客户也能及时得到服务。1.2 需求分析本基于TCP的java聊天系统具体目的是通过网络协议与其他计算机进行通讯。主要实现如下需求目的:登录/注销、实时聊天、状态显示、帮助操作等。作为当今最流行的及时通信工具,腾讯QQ、MSN等软件具有强大的聊天、文件传输、图片保存、远程协助等功能,在日常的生活工作中对人与人之间通信帮助巨大,在一定程度上甚至已经取代传统的信件通信。本次的网络应用开发综合实践我就是以通信中最基本的聊天消息收发方式设计一个基于TCP的java及时聊天系统,由于能力有限,所以把一些要求较高的功能删除了,只留下了聊天这一简单的通信功能。此简单及时聊天通信系统通过java语言编写,提供给用户基本的纯文本消息收发功能,能够通过C/S结构实现消息的转发和接收。2 系统设计2.1 功能概述 本java即时聊天系统是一个基于TCP的、由java语言编写的典型应用程序,它运用目前较为流行的网络编程模型是客户机/服务器(C/S)结构,实现通信双方一方作为服务器等待客户提出请求并予以响应。客户则在需要服务时向服务器提出申请。服务器一般作为守护进程始终运行,监听网络端口,一旦有客户请求,就会启动一个服务进程来响应该客户,同时自己继续监听服务端口,使后来的客户也能及时得到服务,其系统主要功能概述如下:(1)登录/注销功能:在服务器端启动服务开始侦听后,客户端可以登录聊天系统进行及时聊天;或者结束聊天注销登录。(2)聊天功能:客户可以通过服务器转发消息,实现一对一和多对多聊天。(3)设置功能:客户端可以在登录前进行基本信息设置功能。(4)显示功能:客户端程序应该可以实时显示目前所有客户端用户的状态。2.2 运行环境硬件:PC机操作系统:Windows 2003/XP/Vista/7编译环境:MyEclipse 8.5+Tomcat 6.0开发工具:MyEclipse 8.5+Tomcat 6.0编程语言:javaJava聊天系统2.3 系统结构客户端服务器端群聊私聊用户显示用户设置用户登录注销登录群聊端口设置启动服务私聊停止服务用户显示图2.3 系统结构3 系统架构设计3.1 整体设计本java聊天系统在MyEclipse 8.5设计时严格按照C/S结构设计,客户端“client”及服务器端“server”分别实现需求分析中的目的,具体整体设计结构如下图所示:图3.1 系统整体架构设计3.2 服务器端设计ThreadActionListenerJFrame ChatServer-port:public static in-serverSocket:ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ ChatServer():public+init():public void +startService():public void+stopService():public void +sendStopToAll():public void+sendMsgToAll(String):public void+ sendSystemMessage():public voidServerListen- server: ServerSocket- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)ServerReceive-textarea:JTextArea- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)图3.2 服务器端设计3.3 客户端设计ThreadActionListenerJFrameChatClient-port:public static in-serverSocket:ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ ChatClient():public+init():public void +Connect():public void+DisConnect():public void +SendMessage():public voidClientReceive- socket: Socket-output:ObjectOutputStream-input:ObjectInputStream +ClientReceive(Socket,ObjectOutputStream,ObjectInputStream,JComboBox,JTextArea,JTextField )图3.2 客户器端设计4 详细设计 本基于TCP的java及时聊天系统一共包含2个部分:server部分和client部分,分别实现服务器端功能和客户端功能。4.1 聊天室服务器端设计 聊天室服务端主要包括7个文件,它们的功能如下:4.1.1 server.javaserver.java包含名为server的public类,其主要功能为定义服务器端的界面,添加事件侦听与事件处理。调用ServerListen类来实现服务端用户上线与下线的侦听,调用ServerReceive类来实现服务器端的消息的收发。主要实现方法源代码如下:package server;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import .*;import java.io.*;/* * 聊天服务端的主框架类 */public class server extends JFrame implements ActionListenerpublic static int port = 8888;/服务端的侦听端口ServerSocket serverSocket;/服务端SocketImage icon;/程序图标JComboBox combobox;/选择发送消息的接受者JTextArea messageShow;/服务端的信息显示JScrollPane messageScrollPane;/信息显示的滚动条JTextField showStatus;/显示用户连接状态JLabel sendToLabel,messageLabel;JTextField sysMessage;/服务端消息的发送JButton sysMessageButton;/服务端消息的发送按钮UserLinkList userLinkList;/用户链表/建立菜单栏JMenuBar jMenuBar = new JMenuBar(); /建立菜单组JMenu serviceMenu = new JMenu (服务(V);/建立菜单项JMenuItem portItem = new JMenuItem (端口设置(P);JMenuItem startItem = new JMenuItem (启动服务(S);JMenuItem stopItem=new JMenuItem (停止服务(T);JMenuItem exitItem=new JMenuItem (退出(X);JMenu helpMenu=new JMenu (帮助(H);JMenuItem helpItem=new JMenuItem (帮助(H);/建立工具栏JToolBar toolBar = new JToolBar();/建立工具栏中的按钮组件 /框架的大小 /* * 服务端构造函数 */ public server() /添加框架的关闭事件处理 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); /设置框架的大小 this.setSize(faceSize); /设置运行时窗口的位置 this.setTitle(聊天室服务端); /设置标题 /* * 程序初始化函数 */ public void init() Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout(); /添加菜单栏 /初始化按钮 /将按钮添加到工具栏 /初始时,令停止服务按钮不可用 stopServer.setEnabled(false); stopItem .setEnabled(false); /为菜单栏添加事件监听 portItem.addActionListener(this); startItem.addActionListener(this); stopItem.addActionListener(this); exitItem.addActionListener(this); helpItem.addActionListener(this); /添加按钮的事件侦听 portSet.addActionListener(this); startServer.addActionListener(this); stopServer.addActionListener(this); exitButton.addActionListener(this); combobox = new JComboBox(); combobox.insertItemAt(所有人,0); combobox.setSelectedIndex(0); messageShow = new JTextArea(); messageShow.setEditable(false); /添加滚动条 /添加系统消息的事件侦听 sysMessage.addActionListener(this); sysMessageButton.addActionListener(this); sendToLabel = new JLabel(发送至:); messageLabel = new JLabel(发送消息:); downPanel = new JPanel(); girdBag = new GridBagLayout(); downPanel.setLayout(girdBag); girdBagCon = new GridBagConstraints(); JLabel none = new JLabel( ); girdBagCon = new GridBagConstraints(); girdBagCon = new GridBagConstraints(); girdBagCon = new GridBagConstraints(); girdBagCon = new GridBagConstraints(); girdBagCon = new GridBagConstraints(); girdBagCon = new GridBagConstraints(); /关闭程序时的操作 this.addWindowListener( new WindowAdapter() public void windowClosing(WindowEvent e) stopService(); System.exit(0); ); /* * 事件处理 */ public void actionPerformed(ActionEvent e) Object obj = e.getSource(); if (obj = startServer | obj = startItem) /启动服务端 startService(); else if (obj = stopServer | obj = stopItem)/停止服务端 else if (obj = portSet | obj = portItem) /端口设置 /调出端口设置的对话框 PortConf portConf = new PortConf(this); portConf.show(); else if (obj = exitButton | obj = exitItem) /退出程序 else if (obj = helpItem) /菜单栏中的帮助 /调出帮助对话框 Help helpDialog = new Help(this); helpDialog.show(); else if (obj = sysMessage | obj = sysMessageButton) /发送系统消息 sendSystemMessage(); /* * 启动服务端 */ public void startService() try serverSocket = new ServerSocket(port,10); messageShow.append(服务端已经启动,在+port+端口侦听.n); catch (Exception e) /System.out.println(e); userLinkList = new UserLinkList(); listenThread = new ServerListen(serverSocket,combobox,messageShow,showStatus,userLinkList); listenThread.start(); /* * 关闭服务端 */ public void stopService() try /向所有人发送服务器关闭的消息 messageShow.append(服务端已经关闭n); combobox.removeAllItems(); combobox.addItem(所有人); catch(Exception e) /System.out.println(e); /* * 向所有人发送服务器关闭的消息 */ public void sendStopToAll() int count = userLinkList.getCount();/用户总数 int i = 0; while(i count) Node node = userLinkList.findUser(i); if(node = null) i +; continue; try node.output.writeObject(服务关闭); node.output.flush(); catch (Exception e) /System.out.println($+e); i+; /* * 向所有人发送消息 */ public void sendMsgToAll(String msg) int count = userLinkList.getCount();/用户总数 int i = 0; while(i count) Node node = userLinkList.findUser(i); if(node = null) i +; continue; try node.output.writeObject(系统信息); node.output.flush(); node.output.writeObject(msg); node.output.flush(); catch (Exception e) /System.out.println(+e); i+; sysMessage.setText(); /* * 向客户端用户发送消息 */ public void sendSystemMessage() String toSomebody = combobox.getSelectedItem().toString(); String message = sysMessage.getText() + n; messageShow.append(message); /向所有人发送消息 if(toSomebody.equalsIgnoreCase(所有人) sendMsgToAll(message); else /向某个用户发送消息 Node node = userLinkList.findUser(toSomebody); try node.output.writeObject(系统信息); node.output.flush(); node.output.writeObject(message); node.output.flush(); catch(Exception e) /System.out.println(!+e); sysMessage.setText();/将发送消息栏的消息清空 public static void main(String args) server app=new server(); 4.1.2 ServerListen.java该类实现服务端用户上线与下线的侦听。该类对用户上线下线的侦听是通过调用用户链表类(UserLinkList)来实现的,当用户上线与下线情况发生变化时,该类会对主类的界面进行相应的修改。主要实现方法源代码如下:public class ServerListen extends ThreadServerSocket server;JComboBox combobox;JTextArea textarea;JTextField textfield;UserLinkList userLinkList;/用户链表Node client;ServerReceive recvThread;public boolean isStop;/* * 聊天服务端的用户上线于下线侦听类 */public ServerListen(ServerSocket server,JComboBox combobox,JTextArea textarea,JTextField textfield,UserLinkList userLinkList)this.server = server;bobox = combobox;this.textarea = textarea;this.textfield = textfield;this.userLinkList = userLinkList;isStop = false;public void run()while(!isStop & !server.isClosed()tryclient = new Node();client.socket = server.accept();client.output = new ObjectOutputStream(client.socket.getOutputStream();client.output.flush();client.input = new ObjectInputStream(client.socket.getInputStream();client.username = (String)client.input.readObject();/显示提示信息combobox.addItem(client.username);userLinkList.addUser(client);textarea.append(用户 + client.username + 上线 + n);textfield.setText(在线用户 + userLinkList.getCount() + 人n);recvThread = new ServerReceive(textarea,textfield,combobox,client,userLinkList);recvThread.start();catch(Exception e)4.1.3 ServerReceive.java该类是实现服务器端的消息的收发的类。该类分别定义了向某用户及所有人发送消息的方法,发送的消息会显示在主界面类的界面上。主要实现方法源代码如下:/* * 服务器收发消息的类 */public class ServerReceive extends Thread JTextArea textarea;JTextField textfield;JComboBox combobox;Node client;UserLinkList userLinkList;/用户链表public boolean isStop;public ServerReceive(JTextArea textarea,JTextField textfield,JComboBox combobox,Node client,UserLinkList userLinkList)this.textarea = textarea;this.textfield = textfield;this.client = client;this.userLinkList = userLinkList;bobox = combobox;isStop = false;public void run()/向所有人发送用户的列表sendUserList();while(!isStop & !client.socket.isClosed()tryString type = (String)client.input.readObject();if(type.equalsIgnoreCase(聊天信息)String toSomebody = (String)client.input.readObject();String status = (String)client.input.readObject();String action = (String)client.input.readObject();String message = (String)client.input.readObject();String msg = client.username + + action+ 对 + toSomebody + 说 : + message+ n;if(status.equalsIgnoreCase(悄悄话)msg = 悄悄话 + msg;textarea.append(msg);if(toSomebody.equalsIgnoreCase(所有人)sendToAll(msg);/向所有人发送消息elsetryclient.output.writeObject(聊天信息);client.output.flush();client.output.writeObject(msg);client.output.flush();catch (Exception e)/System.out.println(#+e);Node node = userLinkList.findUser(toSomebody);if(node != null)node.output.writeObject(聊天信息); node.output.flush();node.output.writeObject(msg);node.output.flush();else if(type.equalsIgnoreCase(用户下线)Node node = userLinkList.findUser(client.username);userLinkList.delUser(node);String msg = 用户 + client.username + 下线n;int count = userLinkList.getCount();combobox.removeAllItems();combobox.addItem(所有人);int i = 0;while(i count)node = userLinkList.findUser(i);if(node = null) i +;continue; combobox.addItem(node.username);i+;combobox.setSelecte
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 子宫肌瘤剔除术护理查房
- 2026届广东省普宁市华南实验学校英语九年级第一学期期末达标检测模拟试题含解析
- 2026届四川省绵阳富乐园际学校化学九年级第一学期期末学业质量监测模拟试题含解析
- 2026届江苏省泰州市姜堰区实验初级中学化学九年级第一学期期中监测试题含解析
- 2026届吉林省前郭尔罗斯蒙古族自治县化学九年级第一学期期中监测试题含解析
- 商场员工培训资料
- 汽车线束销售年终总结
- 2026届宁夏吴忠市红寺堡二中学化学九上期中调研试题含解析
- 山东省济南市育英中学2026届化学九年级第一学期期中达标检测模拟试题含解析
- 山西省山西大学附属中学等校2025-2026学年高三上学期9月月考政治试题(含答案)
- 2024玻璃钢贮罐拆除解体施工合同
- 2022年中国居民膳食指南
- 2024-2030年中国病理检查市场专题研究及市场前景预测评估报告
- 第3章 即时定位与地图构建技术课件讲解
- 汽车用品采购合同
- P.E.T.父母效能训练
- 《研学旅行课程设计》课件-了解研学旅行概念
- 传统中医针灸技术操作规范
- 沅陵旅游宣传方案
- 个人防护用品使用和检查知识
- MBTI量表完整版本
评论
0/150
提交评论