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