java聊天小程序课程设计_第1页
java聊天小程序课程设计_第2页
java聊天小程序课程设计_第3页
java聊天小程序课程设计_第4页
java聊天小程序课程设计_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 Java语言程序设计 课程设计题 目 聊天小程序 (socket编程+多线程)学 院 商学院 专 业 信息管理与信息系统 班 级 信息111 学 号 8 学生姓名 张语轩 指导教师 余法红 编写日期 2013.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/2000操作系统下运行,是十分灵活的网络寻呼工具。它会自动检查您是否已联网,如果您的电脑已连入Internet,可以搜索网友、显示在线网友,可以根据QQ号、

3、昵称、姓名、email地址等关键词来查找,找到后可加入到通讯录中。当您的通讯录中的网友在线时,QQ中朋友的头像就会显示在线,根据提示就可以发送信息,如果对方登记了寻呼机或开通了GSM手机短消息,即使离线了,您也可及时将信息传递给您的好友。虽然以上这些软件是免费的,并且功能越来越强大。但是它们的唯一的不足就是必须要联网或者下载到它们的服务端,要不然是没法脱离Intenet使用的。为了能在一个没有连接到Intenet的局域网使用聊天工具,那就必须要有服务器端。本系统就是包括服务器端与客户端,可以在局域网聊天的一个工具。二、 系统概述我们实现的是局域网中的聊天功能,运行服务程序后,服务器创建成功,然

4、后使服务器对某一个设定的端口处于监听状态,当有客户端请求建立连接时,服务器就可接受请求,建立连接!这样服务器和客户端就可相互发送消息,实现聊天功能! 系统的功能要求:A.服务器端主要功能如下:1.在特定端口上进行侦听,等待客户端连接。2.用户可以配置服务器端的侦听端口,默认端口为8888。3.向已经连接到服务器端的用户发送系统消息。4.统计在线人数。5.当停止服务时,断开所有的用户连接。B.客户端的主要功能如下:1.连接到已经开启聊天服务的服务器端。2.用户可以配置要连接的服务器端的IP地址和端口号。3.用户可以配置连接后显示的用户名。4.当服务器端开启的话,用户可以随时登录和注销。5.用户可

5、以向所有人或某一个人发送消息。三、 系统运行环境本程序基于java和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地址为 向已经连接到服务器端的用户发送系统消息

7、。 当停止服务时,断开所有的用户连接。 系统运行稳定、安全可靠。 一台主机只能启动一个服务器。客户端: 连接到已经开启聊天服务的服务器端。 当服务器端开启后,用户可以发表聊天信息。 用户可以向所有人或某一个人发送消息。 系统运行稳定、安全可靠。五、 总体设计聊天系统的设计跟普通网站设计有着许多不同的地方,普通网站设计所考虑的因素,例如,普通网站需要对布局进入大量美化以及动画设计等等,而聊天室只要提供满足访客双方直接实时聊天即可。因此,在设计聊天系统的过程中,必须要考虑好以下几个设计要点:在Internet上的聊天程序一般都是以服务器提供服务端连接响应,使用者通过客户端程序登录到服务器,就可以与

8、登录在同一服务器上的用户交谈,这是一个面向连接的通信过程。因此,程序要在TCP/IP环境下,实现服务器端和客户端两部分程序。1首先通过继承JFrame类构造服务器界面。2设计客户端程序使其能够完成正常的通信功能3设计侦听程序,使其显示登录的ip地址端口号,以及用户昵称,通过侦听按钮可以调用客户端程序,实现聊天功能。六、 系统模块结构图主要功能框架如下图所示:服务器客户端A客户端E客户端D客户端C客户端B服务器连接服务器服务器监听上线通知发送消息退出下线通知用户设置发送文件连接设置退出客户端群 聊 私聊七、 程序模块设计(程序代码设计与分析、运行结果)代码:(1)ActionProcess.ja

9、va package chat; import java.awt.event.ActionEvent; import 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 sendtonam

10、e=; String sendtoID; String filename=; String filepath=; public ActionProcess(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:); InetA

11、ddress address=InetAddress.getByName(addr); InetSocketAddress socketAddress=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.

12、getText(); client.out.writeUTF(姓名为+name+的朋友风尘仆仆地上线了.); client.listen.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.sta

13、rt(); catch(IOException ee) if(e.getSource()=client.listen) try name=client.inputName.getText(); client.out.writeUTF(姓名为+name+的朋友正在监听中.); filesendServer filesendserver=new filesendServer(); filesendserver.filereceive(); catch (Exception ee) ee.printStackTrace(); if(e.getSource()=client.filesend) if(

14、client.socket.isConnected() try filesendClient filesendclient=new filesendClient(); 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+的朋友黯然下线了.);

15、 client.out.flush(); client.out.close(); client.socket.close(); catch (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.input

16、Name.getText(); String s=client.inputContent.getText(); if(s!=null) 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:); sen

17、dtoname=JOptionPane.showInputDialog(请输入对方姓名:); String s=client.inputContent.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;w

18、hile(true) try s=client.in.readUTF(); client.chatResult.append(n+s); 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 ja

19、vax.swing.*; public class Client public static void main(String args) new ChatClient(); class ChatClient extends Frame implements ActionListener, Runnable private static final long serialVersionUID = -L; public Button listen,connection,sendtoall,sendtoone,exit,filesend;public JComboBox emote; public

20、 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=Box.createHorizontalBox(); listen=new Button(作为服务器监听); connection=new Button(连接

21、服务器); filesend=new Button(发送文件); exit=new Button(退出); sendtoall=new 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); inputC

22、ontent=new TextField(22); inputContent.setBackground(new Color(162,231,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(connect

23、ion); box1.add(filesend); box1.add(exit); Box box2=Box.createHorizontalBox(); emote = new JComboBox(); emote.setModel(new DefaultComboBoxModel(new String 表情, 微笑, 甜笑, 惊喜, 嘻嘻, 扮酷, 嘿嘿, 傻笑, 好奇, 媚眼, 鬼脸, 陶醉, 害羞, 生气, 嚷嚷, 发怒, 伤心, 高明, 菜鸟, 问号, 狂笑, 大哭, 示爱, 呻吟, 想想 ); emote.setEnabled(false); JLabel jlintput = n

24、ew JLabel(输入聊天内容); box2.add(jlintput); box2.add(inputContent); box2.add(emote); box2.add(sendtoall); box2.add(sendtoone); listen.addActionListener(this); connection.addActionListener(this); filesend.addActionListener(this); exit.addActionListener(this); sendtoall.addActionListener(this); sendtoone.a

25、ddActionListener(this); Box box3=Box.createHorizontalBox(); box3.add(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

26、); box0.add(Box.createVerticalStrut(10); add(box0); setBounds(10,30,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.printStackTrac

27、e(); ); public void actionPerformed(ActionEvent e) ActionProcess actionProcess=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.set

28、Text(与服务器断开连接); try socket.close(); catch(Exception ep) break; (3)ChatServer.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 svSocke

29、t = new ServerSocket(5555); System.out.println (listening.); catch (Exception 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);

30、 st.setID(nid+); threads.add(st); new Thread(st).start(); for(int i=0;i threads.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

31、; private Socket socket = null; private DataInputStream in = null;private DataOutputStream 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(soc

32、ket.getOutputStream(); catch (Exception ex) public void run() System.out.println (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(*+

33、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(监听中) for(int i=0;i threads.size();i+) ServerThread st = (ServerThread)threads.elementAt(i); st.write(*+receive+* ); else if(r

34、eceive.contains(说) if(receive.contains(大家) for(int i=0;i threads.size();i+) 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)thread

35、s.elementAt(i);st.write(: +receive); st = (ServerThread)threads.elementAt(getID(); 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 e

36、x) ex.printStackTrace(); public void write(String msg) synchronized(out) try 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.Fi

37、leInputStream; import java.io.InputStreamReader; import java.io.OutputStream; 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 =

38、 new Socket(sendtoIP, 1234); BufferedReader br = new BufferedReader( new InputStreamReader(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) JO

39、ptionPane.showMessageDialog(null,文件已存在,请不要重复上传!); return; long finishLen = Long.parseLong(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

40、, 0, len); fis.close(); socket.close(); public File getFile() throws Exception File file=null; boolean flag=false; while(flag=false) String filepath=JOptionPane.showInputDialog(请输入要上传的路径:); file = new File(filepath); if(!file.exists() JOptionPane.showMessageDialog(null,您输入的路径不存在,请重新输入!); flag=false;

41、 else if(file.isDirectory() JOptionPane.showMessageDialog(null,占不支持文件夹上传!请输入一个 文件路径!); 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.InputSt

42、ream; 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 serv

43、erSocket = new ServerSocket(1234); JOptionPane.showMessageDialog(null,服务已启动,绑定1234端口!); while(true) Socket socket = serverSocket.accept(); new fileServerThread(socket).start(); class fileServerThread extends Thread Socket socket; public fileServerThread(Socket socket) this.socket = socket; public vo

44、id run() FileOutputStream fos = null; try BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(); PrintStream ps = new PrintStream(socket.getOutputStream(); String fileName = br.readLine(); long fileLen = Long.parseLong(br.readLine(); File dir = new File(upload); dir.mk

45、dir(); File file = new File(dir,fileName); if(file.exists() & file.length() = fileLen) ps.println(已存在); return; else ps.println(file.length(); String time = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date(); System.out.println(time + + (file.exists() ? 开始断点续传: : 开始上传文件: ) + file.getName();

46、 long start = System.currentTimeMillis(); InputStream in = socket.getInputStream(); fos = new FileOutputStream(file, true); byte buffer = new byte1024; int len; while(len = in.read(buffer) != -1) fos.write(buffer, 0, len); if(file.length() = fileLen) break; fos.close(); long end = System.currentTimeMillis(); time = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date(); System.out.println(time + + 上传文件结束: + file.getName() + , 耗时: + (end - start) + 毫秒); ps.println(上传成功); socket.close(); catch (IOException e) if(fos != null) try fos.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论