当前位置:文档之家› 局域网聊天课程设计

局域网聊天课程设计

局域网聊天程序课程设计目录第1章需求分析 (4)第2章系统设计方案 (4)第3章性能测试 (5)3.1课程设计成果 (5)第4章结论 (12)第1章需求分析该程序实现局域网内的聊天功能,包括服务器端程序和客户端程序两部分。

客户端程序:包括登陆界面和聊天界面,可连接到服务器,并将消息发送到服务器端和接受服务器端发送来的信息。

服务器端程序:负责发送用户列表和转发客户端发送来的信息功能如下:连接:登陆界面输入完整之后将用户的信息发送到服务器端,服务器端创建相应的连接,并将更新后的用户列表发送给所有用户。

刷新列表:使客户端发送一段指令到服务器端,然后由服务器端广播新的用户列表到所有的客户端。

发送:根据发送者、发送对象、发送的话创建一个字段发送到服务器端。

接收:根据从服务器端接收到的信息判断发送者、接受者、以及要听的话,选择显示在公共聊天区还是私人聊天区。

颜色:设置字体颜色。

字体:设置字体,在楷体和宋体之间切换。

主题:设置背景图片。

快捷键:设置是否开启shift+enter发送快捷键。

第2章系统设计方案本程序根据功能设置了两个界面:登陆界面以及聊天界面。

服务器端后台运行监听,没有设置界面。

用例图如下:本程序可以有多个用户,只需在一台服务器上运行服务端程序,所有的用户连接上了之后就可以正常通信。

本程序共有四个类,在三个源程序里。

In和out是客户端和服务器端通信的关键。

把发送功能写在了发送按钮的事件响应代码中;由于接收是要保持监听状态的,所以把接收功能写在了一个进程中。

快捷键即键盘监听功能写在输入文本框中以实现输入完信息后按shift+enter就能等同点击了发送按钮。

改变颜色功能通过调用系统的showDialog。

第3章性能测试3.1课程设计成果(1).主要代码:登陆界面连接服务器:public void link() throws Exception{ //连接服务器核心代码hostName = jTextField2.getText().trim(); //主机地址port = Integer.parseInt(jTextField3.getText()); //端口号,默认是4331,参考教科书client = new Socket(hostName,port); //主机地址和端口号组成套接字in=new InputStreamReader(client.getInputStream())); //从服务器接收到的参考,不是很懂out = new PrintWriter(client.getOutputStream()); //发送出去的out.println(jTextField1.getText()+"&"+sex); //类似这种格式 123&Boyout.flush(); //刷新.输出缓冲区}聊天界面发送信息:public void enter(){String mywords,outmsg;String withWho = (String)jComboBox1.getSelectedItem(); //获取是和谁说话try{mywords = jTextArea4.getText(); //我说的话if ((mywords.trim()).length() != 0){ //不能发送空消息也不能都发空格outmsg = "withWho&"+name+"&"+withWho+"&"+mywords;//类似withWho&tom&helloout.println(outmsg);out.flush();if (withWho.equals("所有人")){jTextArea2.append(name+"说:"+mywords+"\n");}else { //对某个人交谈jTextArea2.append(name+" 对"+withWho+"说: "+mywords+"\n");}}}catch (Exception ee){System.out.println(ee);jTextArea2.append("与服务器连接中断,请重新登陆!\n");} finally{jTextArea4.setText("");}}聊天界面接收信息代码:public void run() {String inmsg;while (true){ //循环try{inmsg = in.readLine(); //从流中输入System.out.println("inmsg "+inmsg);jList1.setModel(model1); //model和jList参考网上String[] userInfo = inmsg.split("&");if (inmsg.startsWith("new")){ //新人jTextArea1.append("欢迎 "+userInfo[1]+"\n");model1.addElement(userInfo[1]+"〖"+userInfo[2]+"〗");}else if( inmsg.startsWith("old")) {model1.addElement(userInfo[1]+" 〖"+userInfo[2]+"〗"); //更新用户列表 }//一般消息if (inmsg.startsWith("withWho")){String showmsg[] = inmsg.split("&");System.out.println("接收者的名字:"+showmsg[2]+"我的名字"+name+";\n");if (showmsg[2].equals(name)){ //如果是发给自己的消息jTextArea2.append(showmsg[1]+"说: "+showmsg[3]+"\n"); //显示到我的频道} else{jTextArea1.append(showmsg[1]+"说:"+showmsg[3]+"\n") }}}catch (Exception ee){System.out.println("Error at run()"+ee);jTextArea2.append("与服务器连接中断,请重新登陆!\n");// 输出流,输入流设置为 nullin = null;out = null;return;}}}服务器发送给所有人代码:public static void sendAll(String s){if (connections != null){Enumeration e = connections.elements();while(e.hasMoreElements()) {try {PrintWriter pw = new PrintWriter( ( (Socket) e.nextElement() ).getOutputStream() );pw.println(s);pw.flush();} catch (IOException ex){}}}System.out.println(s);}服务器发送给指定人代码:public static boolean sendOne(String name,String msg){if (clients != null){Enumeration e = clients.elements();while(e.hasMoreElements() ) {ClientProc cp = (ClientProc)e.nextElement(); //枚举所有连接中的用户if ((cp.getName()).equals(name)){try{PrintWriter pw = new PrintWriter((cp.getSocket()).getOutputStream());pw.println(msg);pw.flush();return true; //找了返回且返回值为真}catch (IOException ioe){}}}}return false;//没有找到}服务器发送更新用户信息代码:private void updateList() {// 更新用户列表Vector cs = ChatServer.getClients();if (cs != null){for (Enumeration e = cs.elements();e.hasMoreElements() ;) {ClientProc cp = (ClientProc)e.nextElement();String exist_name = cp.getName();String exit_sex = cp.getSex();out.println("old"+"&"+exist_name+"&"+exit_sex); //在这里标记以便判断out.flush();}}}服务器处理接收到的信息:public void run(){while (name == null){try{String inmsg;inmsg = in.readLine(); // 1111&BoyChatServer.sendAll("new"+"&"+inmsg);//发送信息更新用户列表 new&1111&BoyString []userInfo;userInfo = inmsg.split("&");name = userInfo[0];sex = userInfo[1];//out.println("欢迎 "+name); //初次登陆//out.flush();}catch (IOException ee){}}while (true){try { //通过客户端发送代码到服务器来执行相应的功能,in.readLine()监视String line = in.readLine();String[] inmsg = line.split("&");if (line.equals("quit")){ //处理退出事件ChatServer.sendAll("withWho&"+"【系统消息】& "+"所有人&"++" 退出了聊天室");ChatServer.deleteConnection(s,this);return;}else if (line.equals("refurbish")){ //处理刷新用户列表请求this.updateList();}else if (line.startsWith("withWho")){if(inmsg[2].equals("所有人"))ChatServer.sendAll(line);else{if (ChatServer.sendOne(inmsg[2],line)) {//out.println(line);//out.flush();}else{out.println("withWho&"+"【系统消息】& "+inmsg[1]+"&"+inmsg[2]+"已经退出了聊天室");out.flush(); //}}}elseChatServer.sendAll(line); //发给所有的人}catch (IOException e){System.out.println("事件 "+e);try {s.close();}catch (IOException e2){}return;}}}(2).用户使用说明:在昵称里输入想要使用的名字,在主机地址写入运行服务器端程序的电脑的ip地址,默认为localhost,代表本机,在端口号中输入和服务器端程序约定好的端口号,默认为4331,如果不一致会导致运行失败,最后选择性别后点击“连接”按钮连接服务器,弹出聊天界面。

相关主题