1、3.用户可以配置连接后显示的用户名。4.当服务器端开启的话,用户可以随时登录和注销。5.用户可以向所有人或某一个人发送消息。三、 系统运行环境本程序基于 java 和 Eclipse 开发环境, 可以在一般的装有 windows 系统的主 机上运行。Eclipse 是一个开放源代码的软件开发项目, 专注于为高度集成的工具开发提 供一个全功能的、具有商业品质的工业平台。它主要由 Eclipse 项目、 Eclipse 工具项目和 Eclipse 技术项目三个项目组成, 具体包括四个部分组成 Eclipse Platform、JDT CDT和PDE JDT支持Java幵发、CDT支持C幵发、PDE
2、用来支持发平台。它提供建造块和构造并运行集成软件幵发工具的基础。四、功能需求描述本系统针对局域网进行联机聊天。聊天室分为服务器端和客户端两部分,服 务器端程序主要负责侦听客户端发来的信息,客户端需要登陆到服务器端才可以 实现正常的聊天功能。本系统主要实现如下功能:其界面主要采用了包,包以及 包等。程序实现了聊天室的基本功能,其中有:1、 启动服务器。2、 进入客户端界面。3、 登录聊天室。4、 发送信息(实现按回车键发送消息)。5、 关闭服务器。服务器:在特定端口上进行侦听,等待客户端连接。向已经连接到服务器端的用户发送系统消息。当停止服务时,断幵所有的用户连接。系统运行稳定、安全可靠。一台主
3、机只能启动一个服务器。客户端:连接到已经幵启聊天服务的服务器端。当服务器端幵启后,用户可以发表聊天信息。用户可以向所有人或某一个人发送消息。系统运行稳定、安全可靠五、总体设计聊天系统的设计跟普通网站设计有着许多不同的地方,普通网站设计所考虑 的因素,例如,普通网站需要对布局进入大量美化以及动画设计等等,而聊天室 只要提供满足访客双方直接实时聊天即可。因此,在设计聊天系统的过程中,必 须要考虑好以下几个设计要点:在In ternet上的聊天程序一般都是以服务器提供服务端连接响应, 使用者通过客户端程序登录到服务器,就可以与登录在同一服务器上的用户交谈,这是一 个面向连接的通信过程。因此,程序要在
4、 TCP/IP环境下,实现服务器端和客户端两部分程序。1.首先通过继承JFrame类构造服务器界面。2.设计客户端程序使其能够完成正常的通信功能3.设计侦听程序,使其显示登录的 ip地址端口号,以及用户昵称,通过侦听按钮可以调用客户端程序,实现聊天功能。六、系统模块结构图主要功能框架如下图所示:代码:客户端A客户端C丿 T 、上服务器 发客户端 .J退出线 纟线 送服聊1) package chat;import .*;public class ActionProcess implements Runnable private ChatClient client;String name=;St
5、ring sendtoname=String sendtoID;String filename=String filepath=public ActionProcess(ChatClient client) =client;public void action(ActionEvent e)throws Exception if()=tryelseString addr=( 请输入服务器 IP:);InetAddress address=(addr);InetSocketAddress socketAddress=new InetSocketAddress(address,5555);=new
6、Thread(this);catch(IOException ee) if()=filesendServer filesendserver=new filesendServer();();catch (Exception ee) (); if()=try filesendClient filesendclient=new filesendClient();catch (Exception ee) catch (IOException e1) (0);if ( 表情) else em += 着 if()=if(s!=null)catch(IOException e1)sendtoID=( 请输入
7、对方 ID: sendtoname=( 请输入对方姓名 : if(s! public void run()String s=null; while(true) catch(IOException e) catch(Exception ep) break;(2) package chat; import .*;public class Client public static void main(String args)new ChatClient();class ChatClient extends Frame implements ActionListener, Runnable publi
8、c Button listen,connection,sendtoall,sendtoone,exit,filesend; 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 b
9、ox1=();listen=new Button( 作为服务器监听 connection=new Button( 连接服务器 filesend=new Button( 发送文件 exit=new Button( 退出 sendtoone=new Button( 私聊 (false);inputName=new TextField(6);(new Color(162,231,250);inputContent=new TextField(22);chatResult= new TextArea(, 17,20,;JLabel jlname=new JLabel( 输入昵称 (jlname);(i
10、nputName);(listen);(connection);(filesend);(exit);Box box2=();emote = new JComboBox();(new DefaultComboBoxModel(new String 表情 , 微笑 甜笑 惊喜 嘻嘻 扮酷 嘿嘿 傻笑 好奇 媚眼 鬼脸 陶醉 害羞 生气 嚷嚷 发怒 伤心 高明 菜鸟 问号 狂笑 大哭 示爱 呻吟 想想 );JLabel jlintput = new JLabel( 输入聊天内容 (jlintput);(inputContent);(emote);(sendtoall);(sendtoone);(th
11、is);Box box3=();(chatResult);thread=new Thread(this);Box box0=();(10);(box1);(box3);(box2);add(box0);setBounds(10,30,500,400);setBackground(new Color(80,212,248);setVisible(true);validate();addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) (0););public void actionPerform
12、ed(ActionEvent e) ActionProcess actionProcess=new ActionProcess(this);(e); catch (Exception e1) public void run()while(true) s=();(n+s);catch(IOException e) 与服务器断开连接 catch(Exception ep)break;(3)package chat;public class ChatServerpublic static void main(String args)throws Exception ServerSocket svSo
13、cket =null;Vector threads = new Vector();svSocket = new ServerSocket(5555);catch (Exception ex) 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(Ex
14、ception ex)class ServerThread implements Runnableprivate Vector threads;private Socket socket = null;private DataInputStream in = null;private DataOutputStream out = null;private int nid;public ServerThread(Socket socket,Vector threads) = socket;= threads;in = new DataInputStream(); out = new DataOu
15、tputStream();catch (Exception ex) public void run() try while(true) String receive = (); if(receive = null) return;if( 黯然下线了 ) for(int i=0;i+) ServerThread st = (ServerThread)(i); (*+receive+ else if( 上线了 ServerThread st = (ServerThread)(i);+getID()+: +receive);else if( 监听中 )* 说 大家 elseint temp=(int
16、 temp1=(String tempS=(temp+1,temp1);int i=(tempS);st = (ServerThread)(getID();ServerThread st = (ServerThread)(getID();try ();public void write(String msg)synchronized(out) try(msg);public int getID()return ;public void setID(int nid)= nid;(4)public class filesendClient public void filesend() throws
17、 ExceptionString sendtoIP=( 请输入对方 IP: Socket socket = new Socket(sendtoIP, 1234); BufferedReader br = new BufferedReader( new InputStreamReader();PrintStream ps = new PrintStream();File file = getFile();();String msg = (); 已存在 .equals(msg)(null, 文件已存在 , 请不要重复上传 ! return;long finishLen = (msg);FileIn
18、putStream fis = new FileInputStream(file);OutputStream 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);
19、if(!() 您输入的路径不存在 , 请重新输入! flag=false;else if() 占不支持文件夹上传 ! 请输入一个 文件路径!else flag=true;return file;(5)public class filesendServerpublic void filereceive() throws ExceptionServerSocket serverSocket = new ServerSocket(1234); 服务已启动 , 绑定 1234 端口!new fileServerThread(socket).start();class fileServerThread
20、extends ThreadSocket socket;public fileServerThread(Socket socket) = socket;public void run() FileOutputStream fos = null;BufferedReader br = new BufferedReader(new InputStreamReader();String fileName = ();long fileLen = ();File dir = new File(uploadFile file = new File(dir,fileName);if() & () = fil
21、eLen) (String time = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date();long start = ();InputStream in = ();fos = new FileOutputStream(file, true);= -1)if() = fileLen)long end = ();time = new SimpleDateFormat( 上传成功 catch (IOException e) if(fos != null)运行结果:将 5 个 Java 源文件:、。分别编译这 5 个 Java 源文件,或运行“ javac *.java 命令,编译全部的源文件,然后运行连接服务器的主类, 即。服务器启动成功 结果分析:在进入聊天室之前必须先启动服务器,实现服务器端的连接。然后运行结果分析:
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2