ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:22.55KB ,
资源ID:10744629      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10744629.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java聊天小程序课程设计.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java聊天小程序课程设计.docx

1、java聊天小程序课程设计课程设计题 目 聊天小程序 (socket编程+多线程)学 院 商学院 专 业 信息管理与信息系统 班 级 信息111 学 号8 学生姓名 张语轩 指导教师 余法红 编写日期 、 需求分析当今主流的聊天工具有 QQ UC MSN Messenger等,国内最热门的当属 QQ天工具。您可以使用 QQ和好友用户进行交流,信息即时发送,即时回复,收发及 时、功能全面。此外 QQ还具有BP机网上寻呼、聊天室、传输文件、语音邮件、 手机短讯服务等功能,QQ不仅仅是虚拟的网络寻呼机, 更可与传统的无线寻呼网、 GSM移动电话的短消息系统互联,目前 QQ和全国多家寻呼台、移动通信公

2、司有业务合作。是国内不可多得的中文网络寻呼机。 QQ支持显示朋友在线信息、即时传送信息、即时交谈、即时发送文件和网址。QC可以在 Win95/98/NT/2000操作系统 下运行,是十分灵活的网络寻呼工具。它会自动检查您是否已联网,如果您的电 脑已连入In ter net ,可以搜索网友、显示在线网友,可以根据QC号、昵称、姓名、 email 地址等关键词来查找, 找到后可加入到通讯录中。 当您的通讯录中的网友在 线时,QQ中朋友的头像就会显示在线,根据提示就可以发送信息,如果对方登记 了寻呼机或幵通了 GSM手机短消息,即使离线了,您也可及时将信息传递给您的 好友。虽然以上这些软件是免费的,

3、并且功能越来越强大。但是它们的唯一的不足 就是必须要联网或者下载到它们的服务端,要不然是没法脱离 Intenet 使用的。 为了能在一个没有连接到 Intenet 的局域网使用聊天工具,那就必须要有服务器 端。本系统就是包括服务器端与客户端,可以在局域网聊天的一个工具。二、 系统概述我们实现的是局域网中的聊天功能,运行服务程序后,服务器创建成功,然后使服务器对某一个设定的端口处于监听状态,当有客户端请求建立连接时,服 务器就可接受请求,建立连接!这样服务器和客户端就可相互发送消息,实现聊天功能!系统的功能要求A. 服务器端主要功能如下:1.在特定端口上进行侦听,等待客户端连接。2.用户可以配置

4、服务器端的侦听端口,默认端口为 8888。3.向已经连接到服务器端的用户发送系统消息。4.统计在线人数。5.当停止服务时,断开所有的用户连接。B. 客户端的主要功能如下:1.连接到已经开启聊天服务的服务器端。2.用户可以配置要连接的服务器端的 IP 地址和端口号。3.用户可以配置连接后显示的用户名。4.当服务器端开启的话,用户可以随时登录和注销。5.用户可以向所有人或某一个人发送消息。三、 系统运行环境本程序基于 java 和 Eclipse 开发环境, 可以在一般的装有 windows 系统的主 机上运行。Eclipse 是一个开放源代码的软件开发项目, 专注于为高度集成的工具开发提 供一个

5、全功能的、具有商业品质的工业平台。它主要由 Eclipse 项目、 Eclipse 工具项目和 Eclipse 技术项目三个项目组成, 具体包括四个部分组成 Eclipse Platform、JDT CDT和PDE JDT支持Java幵发、CDT支持C幵发、PDE用来支持发平台。它提供建造块和构造并运行集成软件幵发工具的基础。四、功能需求描述本系统针对局域网进行联机聊天。聊天室分为服务器端和客户端两部分,服 务器端程序主要负责侦听客户端发来的信息,客户端需要登陆到服务器端才可以 实现正常的聊天功能。本系统主要实现如下功能:其界面主要采用了包,包以及 包等。程序实现了聊天室的基本功能,其中有:1

6、、 启动服务器。2、 进入客户端界面。3、 登录聊天室。4、 发送信息(实现按回车键发送消息)。5、 关闭服务器。服务器:在特定端口上进行侦听,等待客户端连接。向已经连接到服务器端的用户发送系统消息。当停止服务时,断幵所有的用户连接。系统运行稳定、安全可靠。一台主机只能启动一个服务器。客户端:连接到已经幵启聊天服务的服务器端。当服务器端幵启后,用户可以发表聊天信息。用户可以向所有人或某一个人发送消息。系统运行稳定、安全可靠五、总体设计聊天系统的设计跟普通网站设计有着许多不同的地方,普通网站设计所考虑 的因素,例如,普通网站需要对布局进入大量美化以及动画设计等等,而聊天室 只要提供满足访客双方直

7、接实时聊天即可。因此,在设计聊天系统的过程中,必 须要考虑好以下几个设计要点:在In ternet上的聊天程序一般都是以服务器提供服务端连接响应, 使用者通过客户端程序登录到服务器,就可以与登录在同一服务器上的用户交谈,这是一 个面向连接的通信过程。因此,程序要在 TCP/IP环境下,实现服务器端和客户端两部分程序。1.首先通过继承JFrame类构造服务器界面。2.设计客户端程序使其能够完成正常的通信功能3.设计侦听程序,使其显示登录的 ip地址端口号,以及用户昵称,通过侦听按钮可以调用客户端程序,实现聊天功能。六、系统模块结构图主要功能框架如下图所示:代码:客户端A客户端C丿 T 、上服务器

8、 发客户端 .J退出线 纟线 送服聊退聊1) package chat;import .*;public class ActionProcess implements Runnable private ChatClient client;String name=;String sendtoname=;String sendtoID;String filename=;String filepath=;public ActionProcess(ChatClient client) =client;public void action(ActionEvent e)throws Exception i

9、f()=tryelseString addr=( 请输入服务器 IP:);InetAddress address=(addr);InetSocketAddress socketAddress=new InetSocketAddress(address,5555);=new Thread(this);catch(IOException ee) if()=tryfilesendServer filesendserver=new filesendServer();();catch (Exception ee) (); if()=try filesendClient filesendclient=ne

10、w filesendClient();();catch (Exception ee)if()=try catch (IOException e1) ();(0);if ( 表情) else em += 着 ; if()=if(s!=null)trycatch(IOException e1) if()=sendtoID=( 请输入对方 ID:); sendtoname=( 请输入对方姓名 :); if(s!=null)try catch(IOException e1) public void run()String s=null; while(true)try catch(IOException

11、 e)try catch(Exception ep) break;(2) package chat; import .*; import .*; import .*; import .*;public class Client public static void main(String args)new ChatClient();class ChatClient extends Frame implements ActionListener, Runnable public Button listen,connection,sendtoall,sendtoone,exit,filesend;

12、 public JComboBox emote;public TextField inputName,inputContent;public TextArea chatResult;public Socket socket=null;public DataInputStream in=null;public DataOutputStream out=null;public Thread thread;ChatClient()socket=new Socket();Box box1=();listen=new Button( 作为服务器监听 );connection=new Button( 连接

13、服务器 );filesend=new Button( 发送文件 );exit=new Button( 退出 );sendtoone=new Button( 私聊 );(false);(false);(false);(false);inputName=new TextField(6);(new Color(162,231,250);inputContent=new TextField(22);(new Color(162,231,250);chatResult= new TextArea(, 17,20,;(new Color(162,231,250);JLabel jlname=new JLa

14、bel( 输入昵称 );(jlname);(inputName);(listen);(connection);(filesend);(exit);Box box2=();emote = new JComboBox();(new DefaultComboBoxModel(new String 表情 , 微笑 , 甜笑 , 惊喜 , 嘻嘻, 扮酷, 嘿嘿, 傻笑, 好奇, 媚眼, 鬼脸, 陶醉, 害羞, 生气, 嚷嚷, 发怒, 伤心, 高明, 菜鸟, 问号, 狂笑 大哭, 示爱, 呻吟, 想想 );(false);JLabel jlintput = new JLabel( 输入聊天内容 );(jl

15、intput);(inputContent);(emote);(sendtoall);(sendtoone);(this);(this);(this);(this);(this);(this);Box box3=();(chatResult);thread=new Thread(this);Box box0=();(10);(box1);(box3);(10);(box2);(10);add(box0);setBounds(10,30,500,400);setBackground(new Color(80,212,248);setVisible(true);validate();addWind

16、owListener(new WindowAdapter() public void windowClosing(WindowEvent e) (0);try (); catch (IOException e1) (););public void actionPerformed(ActionEvent e) ActionProcess actionProcess=new ActionProcess(this);try (e); catch (Exception e1) ();public void run()String s=null;while(true) trys=();(n+s);cat

17、ch(IOException e)( 与服务器断开连接 );try(); catch(Exception ep)break;(3)package chat;import .*;import .*;import .*;public class ChatServerpublic static void main(String args)throws Exception ServerSocket svSocket =null;Vector threads = new Vector();try svSocket = new ServerSocket(5555);catch (Exception ex)

18、 return;tryint nid = 0;while(true)Socket socket = ();ServerThread st = new ServerThread(socket,threads);(nid+);(st);new Thread(st).start();for(int i=0;i ();i+)ServerThread temp = (ServerThread)(i);catch(Exception ex)class ServerThread implements Runnableprivate Vector threads;private Socket socket =

19、 null;private DataInputStream in = null;private DataOutputStream out = null;private int nid;public ServerThread(Socket socket,Vector threads) = socket;= threads;try in = new DataInputStream(); out = new DataOutputStream();catch (Exception ex) public void run() try while(true) String receive = (); if

20、(receive = null) return;if( 黯然下线了 ) for(int i=0;i ();i+) ServerThread st = (ServerThread)(i); (*+receive+*); else if( 上线了 ) for(int i=0;i ();i+)ServerThread st = (ServerThread)(i);(: +receive);else if( 监听中 )for(int i=0;i ();i+)ServerThread st = (ServerThread)(i); (*+receive+* );else if( 说)if( 大家 )fo

21、r(int i=0;i ();i+)ServerThread st = (ServerThread)(i);(: +receive);elseint temp=();String tempS=(temp+1,temp1);int i=(tempS);ServerThread st = (ServerThread)(i);(: +receive);st = (ServerThread)(getID();(: +receive);elseServerThread st = (ServerThread)(getID();(*+receive+*);catch(Exception ex)(this);

22、();try ();catch(Exception ex)();public void write(String msg)synchronized(out) try(msg);catch(Exception ex)public int getID()return ;public void setID(int nid)= nid;(4)package chat;public class filesendClient public void filesend() throws ExceptionString sendtoIP=( 请输入对方 IP:); Socket socket = new So

23、cket(sendtoIP, 1234); BufferedReader br = new BufferedReader( new InputStreamReader();PrintStream ps = new PrintStream();File file = getFile();();();String msg = ();if( 已存在 .equals(msg)(null, 文件已存在 , 请不要重复上传 !); return;long finishLen = (msg);FileInputStream fis = new FileInputStream(file);OutputStre

24、am out = ();byte buffer = new byte1024;int len;(finishLen);while(len = (buffer) != -1)(buffer, 0, len);();();public File getFile() throws Exception File file=null;boolean flag=false;while(flag=false)String filepath=( 请输入要上传的路径 :);file = new File(filepath);if(!()(null, 您输入的路径不存在 , 请重新输入! ); flag=fals

25、e;else if()(null, 占不支持文件夹上传 ! 请输入一个 文件路径! ); flag=false;else flag=true;return file;(5)package chat;public class filesendServerpublic void filereceive() throws ExceptionServerSocket serverSocket = new ServerSocket(1234);(null, 服务已启动 , 绑定 1234 端口!);while(true)Socket socket = ();new fileServerThread(so

26、cket).start();class fileServerThread extends ThreadSocket socket;public fileServerThread(Socket socket) = socket;public void run() FileOutputStream fos = null;try BufferedReader br = new BufferedReader(new InputStreamReader();PrintStream ps = new PrintStream();String fileName = ();long fileLen = ();

27、File dir = new File(upload);();File file = new File(dir,fileName);if() & () = fileLen) ( 已存在 );return;else();String time = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date();long start = ();InputStream in = ();fos = new FileOutputStream(file, true);byte buffer = new byte1024;int len;while(l

28、en = (buffer) != -1)(buffer, 0, len);if() = fileLen)break;();long end = ();time = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date();( 上传成功 );(); catch (IOException e) if(fos != null)try (); catch (IOException e1) ();运行结果:将 5 个 Java 源文件:、。分别编译这 5 个 Java 源文件,或运行“ javac *.java 命令,编译全部的源文件,然后运行连接服务器的主类, 即。服务器启动成功 结果分析:在进入聊天室之前必须先启动服务器,实现服务器端的连接。然后运行结果分析:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1