收藏 分销(赏)

java聊天小程序专业课程设计.doc

上传人:精*** 文档编号:2727006 上传时间:2024-06-05 格式:DOC 页数:31 大小:479.54KB
下载 相关 举报
java聊天小程序专业课程设计.doc_第1页
第1页 / 共31页
java聊天小程序专业课程设计.doc_第2页
第2页 / 共31页
java聊天小程序专业课程设计.doc_第3页
第3页 / 共31页
java聊天小程序专业课程设计.doc_第4页
第4页 / 共31页
java聊天小程序专业课程设计.doc_第5页
第5页 / 共31页
点击查看更多>>
资源描述

1、 Java语言程序设计 课程设计题 目 聊天小程序 (socket编程+多线程)学 院 商学院 专 业 信息管理和信息系统 班 级 信息111 学 号 52275118 学生姓名 张语轩 指导老师 余法红 编写日期 .12.25 目录一、 需求分析3二、 系统概述3三、 系统运行环境4四、 功效需求描述4五、 总体设计5六、 系统模块结构图7七、 程序模块设计(程序代码设计和分析、运行结果)8八、 设计体会23九、 参考文件23一、 需求分析当今主流聊天工含有QQ,UC,MSN Messenger等,中国最热门当属QQ,腾讯QQ(OICQ)是由深圳市腾讯计算机系统企业开发,基于Internet

2、即时聊天工具。您能够使用QQ和好友用户进行交流,信息即时发送,即时回复,收发立即、功效全方面。另外QQ还含有BP机网上寻呼、聊天室、传输文件、语音邮件、手机短讯服务等功效,QQ不仅仅是虚拟网络寻呼机,更可和传统无线寻呼网、GSM移动电话短消息系统互联,现在QQ和全国多家寻呼台、移动通信企业有业务合作。是中国不可多得汉字网络寻呼机。QQ支持显示好友在线信息、即时传送信息、即时交谈、即时发送文件和网址。QQ能够在Win95/98/NT/操作系统下运行,是十分灵活网络寻呼工具。它会自动检验您是否已联网,假如您电脑已连入Internet,能够搜索网友、显示在线网友,能够依据QQ号、昵称、姓名、emai

3、l地址等关键词来查找,找到后可加入到通讯录中。当您通讯录中网友在线时,QQ中好友头像就会显示在线,依据提醒就能够发送信息,假如对方登记了寻呼机或开通了GSM手机短消息,即使离线了,您也可立即将信息传输给您好友。即使以上这些软件是无偿,而且功效越来越强大。不过它们唯一不足就是必需要联网或下载到它们服务端,要不然是没法脱离Intenet使用。为了能在一个没有连接到Intenet局域网使用聊天工具,那就必需要有服务器端。本系统就是包含服务器端和用户端,能够在局域网聊天一个工具。二、 系统概述我们实现是局域网中聊天功效,运行服务程序后,服务器创建成功,然后使服务器对某一个设定端口处于监听状态,当有用户

4、端请求建立连接时,服务器就可接收请求,建立连接!这么服务器和用户端就可相互发送消息,实现聊天功效! 系统功效要求:A.服务器端关键功效以下:1.在特定端口上进行侦听,等候用户端连接。2.用户能够配置服务器端侦听端口,默认端口为8888。3.向已经连接到服务器端用户发送系统消息。4.统计在线人数。5.当停止服务时,断开全部用户连接。B.用户端关键功效以下:1.连接到已经开启聊天服务服务器端。2.用户能够配置要连接服务器端IP地址和端口号。3.用户能够配置连接后显示用户名。4.当服务器端开启话,用户能够随时登录和注销。5.用户能够向全部些人或某一个人发送消息。三、 系统运行环境本程序基于java和

5、Eclipse开发环境,能够在通常装有windows系统主机上运行。Eclipse是一个开放源代码软件开发项目,专注于为高度集成工具开发提供一个全功效、含有商业品质工业平台。它关键由Eclipse项目、Eclipse工具项目和Eclipse技术项目三个项目组成,具体包含四个部分组成Eclipse Platform、JDT、CDT和PDE。JDT支持Java开发、CDT支持C开发、PDE用来支持插件开发,Eclipse Platform则是一个开放可扩展IDE,提供了一个通用开发平台。它提供建造块和结构并运行集成软件开发工具基础。四、 功效需求描述本系统针对局域网进行联机聊天。聊天室分为服务器端

6、和用户端两部分,服务器端程序关键负责侦听用户端发来信息,用户端需要登陆到服务器端才能够实现正常聊天功效。本系统关键实现以下功效:其界面关键采取了java.awt包,java.swing包和java.util包等。程序实现了聊天室基础功效,其中有:1、开启服务器。2、进入用户端界面。3、登录聊天室。4、发送信息(实现按回车键发送消息)。5、关闭服务器。服务器: 在特定端口上进行侦听,等候用户端连接。 用户能够配置服务器端IP地址,默认IP地址为10.5.51.4 向已经连接到服务器端用户发送系统消息。 当停止服务时,断开全部用户连接。 系统运行稳定、安全可靠。 一台主机只能开启一个服务器。用户端

7、: 连接到已经开启聊天服务服务器端。 当服务器端开启后,用户能够发表聊天信息。 用户能够向全部些人或某一个人发送消息。 系统运行稳定、安全可靠。五、 总体设计聊天系统设计跟一般网站设计有着很多不一样地方,一般网站设计所考虑原因,比如,一般网站需要对布局进入大量美化和动画设计等等,而聊天室只要提供满足访客双方直接实时聊天即可。所以,在设计聊天系统过程中,必需要考虑好以下多个设计关键点:在Internet上聊天程序通常全部是以服务器提供服务端连接响应,使用者经过用户端程序登录到服务器,就能够和登录在同一服务器上用户交谈,这是一个面向连接通信过程。所以,程序要在TCP/IP环境下,实现服务器端和用户

8、端两部分程序。1首先经过继承JFrame类结构服务器界面。2设计用户端程序使其能够完成正常通信功效3设计侦听程序,使其显示登录ip地址端口号,和用户昵称,经过侦听按钮能够调用用户端程序,实现聊天功效。六、 系统模块结构图关键功效框架以下图所表示:服务器用户端A用户端E用户端D用户端C用户端B服务器连接服务器服务器监听上线通知发送消息退出下线通知用户设置发送文件连接设置退出用户端群 聊 私聊七、 程序模块设计(程序代码设计和分析、运行结果)代码:(1)ActionProcess.java package chat; import java.awt.event.ActionEvent; impor

9、t java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import .*; import javax.swing.JOptionPane; public class ActionProcess implements Runnable private ChatClient client; String name=; String sendtoname=; String sendtoID; String filename=; String filepath=; public A

10、ctionProcess(ChatClient client) this.client=client; public void action(ActionEvent e)throws Exception if(e.getSource()=client.connection) try if(client.socket.isConnected() else String addr=JOptionPane.showInputDialog(请输入服务器IP:); InetAddress address=InetAddress.getByName(addr); InetSocketAddress soc

11、ketAddress=new InetSocketAddress(address,5555); client.socket.connect(socketAddress); client.in=new DataInputStream(client.socket.getInputStream(); client.out=new DataOutputStream(client.socket.getOutputStream(); name=client.inputName.getText(); client.out.writeUTF(姓名为+name+好友风尘仆仆地上线了.); client.list

12、en.setEnabled(true); client.filesend.setEnabled(true); client.sendtoall.setEnabled(true); client.sendtoone.setEnabled(true); client.emote.setEnabled(true); if(!(client.thread.isAlive() client.thread=new Thread(this); client.thread.start(); catch(IOException ee) if(e.getSource()=client.listen) try na

13、me=client.inputName.getText(); client.out.writeUTF(姓名为+name+好友正在监听中.); filesendServer filesendserver=new filesendServer(); filesendserver.filereceive(); catch (Exception ee) ee.printStackTrace(); if(e.getSource()=client.filesend) if(client.socket.isConnected() try filesendClient filesendclient=new f

14、ilesendClient(); filesendclient.filesend(); client.out.writeUTF(成功发送文件!); catch (Exception ee) if(e.getSource()=client.exit) if(client.socket.isConnected() try name=client.inputName.getText(); client.out.writeUTF(姓名为+name+好友黯然下线了.); client.out.flush(); client.out.close(); client.socket.close(); catc

15、h (IOException e1) e1.printStackTrace(); System.exit(0); String em=client.emote.getSelectedItem().toString(); if (em.equals(表情) em = ; else em += 着; if(e.getSource()=client.sendtoall) if(client.socket.isConnected() name=client.inputName.getText(); String s=client.inputContent.getText(); if(s!=null)

16、try client.out.writeUTF(name+em+对大家说:+s); client.out.flush(); client.inputContent.setText(); catch(IOException e1) if(e.getSource()=client.sendtoone) if(client.socket.isConnected() sendtoID=JOptionPane.showInputDialog(请输入对方ID:); sendtoname=JOptionPane.showInputDialog(请输入对方姓名:); String s=client.input

17、Content.getText(); name=client.inputName.getText(); if(s!=null) try client.out.writeUTF(name+ 悄 悄 地 +em+ 对 +sendtoname+说:+s); client.out.flush(); client.inputContent.setText(); catch(IOException e1) public void run() String s=null;while(true) try s=client.in.readUTF(); client.chatResult.append(n+s);

18、 catch(IOException e) client.chatResult.setText(和服务器断开连接); try client.socket.close(); catch(Exception ep) break; (2)Clicent.javapackage chat; import java.awt.*; import java.io.*; import .*; import java.awt.event.*; import javax.swing.*; public class Client public static void main(String args) new Ch

19、atClient(); class ChatClient extends Frame implements ActionListener, Runnable private static final long serialVersionUID = -45330513L; public Button listen,connection,sendtoall,sendtoone,exit,filesend;public JComboBox emote; public TextField inputName,inputContent; public TextArea chatResult; publi

20、c Socket socket=null; public DataInputStream in=null; public DataOutputStream out=null; public Thread thread; ChatClient() socket=new Socket(); Box box1=Box.createHorizontalBox(); listen=new Button(作为服务器监听); connection=new Button(连接服务器); filesend=new Button(发送文件); exit=new Button(退出); sendtoall=new

21、Button(群聊); sendtoone=new Button(私聊); listen.setEnabled(false); filesend.setEnabled(false); sendtoall.setEnabled(false); sendtoone.setEnabled(false); inputName=new TextField(6); inputName.setBackground(new Color(162,231,250); inputContent=new TextField(22); inputContent.setBackground(new Color(162,2

22、31,250); chatResult= new TextArea(, 17,20,TextArea.SCROLLBARS_VERTICAL_ONLY); chatResult.setBackground(new Color(162,231,250); JLabel jlname=new JLabel(输入昵称); box1.add(jlname); box1.add(inputName); box1.add(listen); box1.add(connection); box1.add(filesend); box1.add(exit); Box box2=Box.createHorizon

23、talBox(); emote = new JComboBox(); emote.setModel(new DefaultComboBoxModel(new String 表情, 微笑, 甜笑, 惊喜, 嘻嘻, 扮酷, 嘿嘿, 傻笑, 好奇, 媚眼, 鬼脸, 沉醉, 害羞, 生气, 嚷嚷, 发怒, 难过, 高明, 菜鸟, 问号, 狂笑, 大哭, 示爱, 呻吟, 想想 ); emote.setEnabled(false); JLabel jlintput = new JLabel(输入聊天内容); box2.add(jlintput); box2.add(inputContent); box2.

24、add(emote); box2.add(sendtoall); box2.add(sendtoone); listen.addActionListener(this); connection.addActionListener(this); filesend.addActionListener(this); exit.addActionListener(this); sendtoall.addActionListener(this); sendtoone.addActionListener(this); Box box3=Box.createHorizontalBox(); box3.add

25、(chatResult); thread=new Thread(this); Box box0=Box.createVerticalBox(); box0.add(Box.createVerticalStrut(10); box0.add(box1); box0.add(Box.createVerticalStrut(10); box0.add(box3); box0.add(Box.createVerticalStrut(10); box0.add(box2); box0.add(Box.createVerticalStrut(10); add(box0); setBounds(10,30,

26、500,400); setBackground(new Color(80,212,248); setVisible(true); validate(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); try socket.close(); catch (IOException e1) e1.printStackTrace(); ); public void actionPerformed(ActionEvent e) ActionProcess act

27、ionProcess=new ActionProcess(this); try actionProcess.action(e); catch (Exception e1) e1.printStackTrace(); public void run() String s=null; while(true) try s=in.readUTF(); chatResult.append(n+s); catch(IOException e) chatResult.setText(和服务器断开连接); try socket.close(); catch(Exception ep) break; (3)Ch

28、atServer.javapackage chat; import .*; import java.io.*; import java.util.*; public class ChatServer public static void main(String args)throws Exception ServerSocket svSocket =null; Vector threads = new Vector(); try svSocket = new ServerSocket(5555); System.out.println (listening.); catch (Exceptio

29、n ex) System.out.println (Server create ServerSocket failed!); return; try int nid = 0; while(true) Socket socket = svSocket.accept(); System.out.println (accept a client); ServerThread st = new ServerThread(socket,threads); st.setID(nid+); threads.add(st); new Thread(st).start(); for(int i=0;i thre

30、ads.size();i+) ServerThread temp = (ServerThread)threads.elementAt(i); System.out.println (Listen again.); catch(Exception ex) System.out.println (server is down); class ServerThread implements Runnableprivate Vector threads; private Socket socket = null; private DataInputStream in = null;private Da

31、taOutputStream out = null; private int nid; public ServerThread(Socket socket,Vector threads) this.socket = socket; this.threads = threads; try in = new DataInputStream(socket.getInputStream(); out = new DataOutputStream(socket.getOutputStream(); catch (Exception ex) public void run() System.out.pri

32、ntln (Thread is running); try while(true) String receive = in.readUTF(); if(receive = null) return; if(receive.contains(黯然下线了) for(int i=0;i threads.size();i+) ServerThread st = (ServerThread)threads.elementAt(i);st.write(*+receive+*); else if(receive.contains(上线了) for(int i=0;i threads.size();i+) S

33、erverThread st = (ServerThread)threads.elementAt(i);st.write(: +receive); else if(receive.contains(监听中) for(int i=0;i threads.size();i+) ServerThread st = (ServerThread)threads.elementAt(i); st.write(*+receive+* ); else if(receive.contains(说) if(receive.contains(大家) for(int i=0;i threads.size();i+)

34、ServerThread st = (ServerThread)threads.elementAt(i); st.write(: +receive); else int temp=receive.indexOf(); String tempS=receive.substring(temp+1,temp1); int i=Integer.parseInt(tempS); ServerThread st = (ServerThread)threads.elementAt(i);st.write(: +receive); st = (ServerThread)threads.elementAt(ge

35、tID(); st.write(: +receive); else ServerThread st = (ServerThread)threads.elementAt(getID(); st.write(*+receive+*); catch(Exception ex) threads.removeElement(this); ex.printStackTrace(); try socket.close(); catch(Exception ex) ex.printStackTrace(); public void write(String msg) synchronized(out) try

36、 out.writeUTF(msg); catch(Exception ex) public int getID() return this.nid; public void setID(int nid) this.nid = nid; (4)fliesendClient.javapackage chat; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStream

37、; import java.io.PrintStream; import .Socket; import javax.swing.JOptionPane; public class filesendClient public void filesend() throws Exception String sendtoIP=JOptionPane.showInputDialog(请输入对方IP:); Socket socket = new Socket(sendtoIP, 1234); BufferedReader br = new BufferedReader( new InputStream

38、Reader(socket.getInputStream(); PrintStream ps = new PrintStream(socket.getOutputStream(); File file = getFile(); ps.println(file.getName(); ps.println(file.length(); String msg = br.readLine(); if(已存在.equals(msg) JOptionPane.showMessageDialog(null,文件已存在,请不要反复上传!); return; long finishLen = Long.pars

39、eLong(msg); FileInputStream fis = new FileInputStream(file); OutputStream out = socket.getOutputStream(); byte buffer = new byte1024; int len; fis.skip(finishLen); while(len = fis.read(buffer) != -1) out.write(buffer, 0, len); fis.close(); socket.close(); public File getFile() throws Exception File

40、file=null; boolean flag=false; while(flag=false) String filepath=JOptionPane.showInputDialog(请输入要上传路径:); file = new File(filepath); if(!file.exists() JOptionPane.showMessageDialog(null,您输入路径不存在,请重新输入!); flag=false; else if(file.isDirectory() JOptionPane.showMessageDialog(null,占不支持文件夹上传!请输入一个 文件路径!);

41、 flag=false; else flag=true; return file; (5)filesendServer.java package chat; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import .ServerSocket; import .Socket; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; public class filesendServer public void filereceive() throws Exception ServerSocket serverSocket = new ServerSocket(1234); JOptionPane.showMessageDialog(null,服务

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服