基于java的网络程序设计报告(附带源码).docx_第1页
基于java的网络程序设计报告(附带源码).docx_第2页
基于java的网络程序设计报告(附带源码).docx_第3页
基于java的网络程序设计报告(附带源码).docx_第4页
基于java的网络程序设计报告(附带源码).docx_第5页
已阅读5页,还剩138页未读 继续免费阅读

下载本文档

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

文档简介

软件工程设计报告一. 项目名称:基于JAVA实现的聊天室二. 项目功能:1. 新用户的注册2. 用户登录和退出3. 聊天服务4. 在线用户列表5. 文件传输服务三. 项目整体设计框图说明:客户机服务器建立服务器套接字接受请求,建立对等客户机套接字接受消息队列信息,并做处理关闭套接字,保存相关信息建立客户机套接字提出请求,与服务器建立连接发送并接受消息队列信息,并做处理关闭套接字,保存相关信息四. 项目分模块设计说明和流程图:1. 此个项目主要分3个模块:服务器,客户端,文件传输系统2. 模块主要工能是:客服端发送和接受信息,服务器转发信息,文件传输系统主要是处理文件传送和接受的工作(文件传输是发起端建立文件传输服务器,并把相关信息通过服务器转发给接收方,让接受方与文件传输服务器建立连接,完成文件传输过程)。3. 此个项目采用JAVA完成,主要的类文件如下:Chatsever:聊天室的服务器Chatroom:聊天室的客户端File_send:文件传输界面,是文件传送和接受的前导都是组成关系Server:文件传送Client:文件接受Java_list:处理用户列表界面问题MyCellRenderer:处理用户头像问题4. 程序处理流程图:启动服务器读取用户帐号和密码信息信息表建立服务器套接字ServerSocket(2950)侦听客户端连接请求Accept()建立线程n和套接字s实现连接接受客户端信息m主线程m为注册信息,对比信息表给客户端回应判断m是注册信息还是登录信息m为登录信息,对比信息表,并给予客户端回应登录成功登录失败继续接收信息,判断信息类型信息是文件传输请求,转发请求信息是普通聊天消息,判断转发对象,并且转发消息到指定队列消息是用户注销退出消息关闭套接字s,终止线程n服务器启动过程关闭各种客服端套接字,终止连接关闭服务器套接字释放各种资源保存读取用户帐号和密码信息信息表服务器终止过程启动客服端,与服务器建立连接发送注册信息发送登录信息登录注册界面接收服务器信息接收服务器信息注册成功注册失败登录成功登录失败聊天界面提示用户建立接受信息线程主线程接收信息根据消息决定处理方式接收用户处理信息要发送普通聊天消息要提出文件传送请求发送信息文件传输主界面传输文件过程结束客户端启动运行过程关闭客服端套接字,终止连接释放各种资源结束客户端终止过程发送用户注销退出消息文件传输主界面文件传送界面文件接收界面选择传送文件选择存放文件位置和文件名发送消息:包括文件类型,名字和大小接收并提取消息:包括文件类型,名字和大小连续读取文件到字节流发送字节流发送完毕流没有结束连续接收字节流将字节流写进文件接收完毕流没有结束关闭文件流和套接字建立套接字建立连接释放各种资源结束文件传输系统5. 程序运行示范截图以及使用说明:(1).服务器配置文件server.ini,主要记录已注册用户的帐号和密码信息,每2行是一组帐号和密码:(2).服务器启动窗口,等待客服机的连接:(3).服务器启动窗口,已有客服机的连接:(4).客户机配置文件chat.ini,其中第一项是服务器的地址,第二项是用户上次记录的帐号,第三项是密码:(5).客服机启动,登录界面:(6)。客服机启动,注册界面:(7).登录失败提示界面:(8).注册失败提示界面:(9).注册成功提示界面:(10).登录成功进入主界面,最上面是表明当前用户的帐号(昵称)和当前在线人数,最左边是当前在线人员列表,可以选择对谁发消息或者传送文件;最下面3个按键功能分别如上面所写的:(11).对所有人发送消息:(12).对所有人发送消息之后,自己界面变化:(13).对所有人发送消息之后,别人收到信息的界面变化:(14).对某个人发送消息的界面变化:(15).别人收到信息之后的界面变化:(16).对某人(xujin)提出文件传输请求:(17).选择传送文件窗口:(18).文件等待传输窗口:(19).xujin的界面提示用户(徐进,ip为)有文件需要接收的提示窗口:(20).点击接收,弹出如下提示窗口:(21).点击确定,弹出文件存储位置选择窗口:(22).点击保存,弹出文件接收窗口,显示当前接收量的百分比:(23).文件传送窗口:(24).文件传送完毕窗口:五. 关键数据结构及算法:(1).作为客户机与服务器通信的标准,采用“$0”,“$1”,“$2”,“$3”.为头的数据加工方式,即每条信息都是以此格式为标准的,所不管是服务器或者客服端接收的到的字节流信息(具体文件传输过程除外),都要把其转化为字符串,然后取前2个字符,根据其决定该条信息的含义,并决定后续工作(2).在账户和密码匹配部分,服务器没有采用数据库的方式存储,是把帐号(xujin)和密码(123456)以“rnxujinrn123456”的方式存进文件,多个信息也是以此种方式存储,这样,服务器一次读取文件中所有的字符串,在用户登录或者注册时只需在这个字符串中是否存在其子串(方式“rn帐号rn密码”),决定是否成功,当注册成功时只需要在那个字符串后面加上那个子串作为新的字符串,当服务器结束时再写进文件保存。这样的算法存在一个问题就是,当一个用户的帐号是某个其他用户的密码,而他的密码又是该个用户信息的存储位置的下一个用户的帐号,就会出现错误,不过这是小概率的事件!(3).其他算法都是在流程图中,数据结构在源代码中。(源代码见后页)Chatsever.javaimport java.io.*;import .*;import java.awt.*;import java.awt.event.*;import javax.swing.JButton;import javax.swing.JFrame;public class Chatsever extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;static Chatsever frm=new Chatsever();static JButton btn1=new JButton(关闭服务器);static OutputStream out;static ServerSocket svs;static InputStream in;static byte data=new byte1024;static int n=0;static TextArea txa1=new TextArea(!,6,10,TextArea.SCROLLBARS_NONE);static ServerThread server=new ServerThread100;public static void main(String args) throws Exceptionchar ch=new char10240;FileReader fr=new FileReader(server.ini);int num=fr.read(ch);if(num0)ServerThread.message=new String(ch,0,num);ch=null;fr.close();ServerThread.txa1=txa1;txa1.setFont(new Font(楷体,Font.BOLD,32);txa1.setBounds(10, 10, 265, 120);txa1.setEditable(false);frm.setLayout(null);frm.add(txa1);btn1.setBounds(92, 130, 100, 30);btn1.addActionListener(frm);frm.add(btn1);frm.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)trysvs.close();for(int i=0;i100;i+)if(serveri!=null)serveri.in.close();serveri.out.close();serveri.s.close(); catch (Exception e1)e1.printStackTrace();tryFileWriter fw=new FileWriter(server.ini);fw.write(ServerThread.message);fw.close(); catch (Exception e1)e1.printStackTrace();System.exit(0););frm.setLocation(400, 200);frm.setSize(300,200);frm.setVisible(true);svs=new ServerSocket(2950);txa1.setText(等待客户机的连接。);Thread t;while(true)if(servern=null)servern=new ServerThread(); servern.s=svs.accept();servern.i=n;servern.server=servern;t=new Thread(servern);t.start();else n=(n+1)%100;public void actionPerformed(ActionEvent e)trysvs.close();for(int i=0;i100;i+)if(serveri!=null)serveri.in.close();serveri.out.close();serveri.s.close(); catch (Exception e1)e1.printStackTrace();tryFileWriter fw=new FileWriter(server.ini);fw.write(ServerThread.message);fw.close(); catch (Exception e1)e1.printStackTrace();System.exit(0); class ServerThread implements Runnablestatic String st=new String100;static String name=new String100;static boolean state=new boolean100; static String message;static TextArea txa1;static int j=0;ServerThread server;String pop,pou;Socket s;int i;int n;byte data=new byte1024;InputStream in;OutputStream out;public void run()tryout=s.getOutputStream();in=s.getInputStream();while(true)n=in.read(data);if(n-1)/txa1.setText(message);ServerThread.j+;Chatsever.txa1.setText(当前在线+ServerThread.j+人);out.write($9+ServerThread.j).getBytes();break;elseout.write($8.getBytes();else if(pop.startsWith($4)pop=pop.substring(2);if(message.indexOf(pop)-1)out.write($7.getBytes();elseout.write($6.getBytes();message=message+rn+pop;if(pop.equals($2)tryin.close(); catch (IOException e)e.printStackTrace();tryout.close(); catch (IOException e)e.printStackTrace();trys.close(); catch (IOException e)e.printStackTrace();sti=null;namei=null;server=null;return;ServerTi=pop.substring(0,pop.indexOf(rn);WriteThread.writes(i, Chatsever.server);while(true)n=in.read(data);if(n=0)continue;pop=new String(data,0,n);if(pop.startsWith($3)ServerThread.statei=false;break;else if(pop.startsWith($4)pop=pop.substring(2,pop.indexOf(rn);pou=new String(data,0,n);for(int j=0;j100;j+)if(pop.equals(ServerTj)&i!=j)ServerThread.stj=$1+namei+s.getInetAddress();tryChatsever.serverj.out.write(ServerThread.stj.getBytes(); catch (IOException e)e.printStackTrace();else if(pop.startsWith($a)pop=pop.substring(2);for(int j=0;j100;j+)if(pop.equals(ServerTj)&i!=j)ServerThread.stj=$b+namei;tryChatsever.serverj.out.write(ServerThread.stj.getBytes(); catch (IOException e)e.printStackTrace();elseWriteThread.write(pop,data,n,i,Chatsever.server,pou);ServerTi=null;WriteThread.writes(i, Chatsever.server);tryin.close(); catch (IOException e)e.printStackTrace();tryout.close(); catch (IOException e)e.printStackTrace();trys.close(); catch (IOException e)e.printStackTrace();ServerThread.statei=false;ServerThread.sti=null;ServerThread.j-;Chatsever.txa1.setText(当前在线+ServerThread.j+人);server=null;return; catch (IOException e)e.printStackTrace(); class WriteThread public synchronized static void write(String pop,byte data,int n,int i,ServerThread server,String pou)pop=pop.substring(0,pop.indexOf(rn);if(pop.equals(所有人)pop=new String(data,0,n);pop=pop.substring(pop.indexOf(rn)+2);for(int j=0;j100;j+)if(ServerTj!=null&i!=j)ServerThread.stj=ServerTi+对所有人说:+pop;tryserverj.out.write(ServerThread.stj.getBytes(); catch (IOException e)e.printStackTrace();ServerThread.sti=你对所有人说:+pop;tryserveri.out.write(ServerThread.sti.getBytes(); catch (IOException e)e.printStackTrace();elsepou=new String(data,0,n);for(int j=0;j100;j+)if(pop.equals(ServerTj)&i!=j)ServerThread.stj=ServerTi+对你说:+pou.substring(pou.indexOf(rn)+2);tryserverj.out.write(ServerThread.stj.getBytes(); catch (IOException e)e.printStackTrace();ServerThread.sti=你对+pop+:+pou.substring(pou.indexOf(rn)+2);tryserveri.out.write(ServerThread.sti.getBytes(); catch (IOException e)e.printStackTrace();public synchronized static void writes(int i,ServerThread server)String pop=$0;for(int j=0;j100;j+)if(ServerTj!=null)pop=pop+ServerTj+rn;for(int j=0;j100;j+)if(ServerTj!=null)ServerThread.stj=pop;tryserverj.out.write(ServerThread.stj.getBytes(); catch (IOException e)e.printStackTrace(); Chatroom.javaimport java.awt.*;import java.awt.event.*;import java.io.*;import .Socket;import javax.swing.*;SuppressWarnings(serial)public class Chatroom extends JFrame implements ActionListener,KeyListenerstatic Java_list te=new Java_list();static boolean state=false;static InputStream in;static OutputStream out;static Socket s;static Chatroom frm=new Chatroom();static Label lab=new Label(帐号);static Label lab1=new Label(密码);static Label lab3=new Label();static Label lab4=new Label(按回车可以直接发送信息!);static Dialog dlg=new Dialog(frm);static Dialog dlg1=new Dialog(frm);static Dialog dlg2=new Dialog(frm);static JButton btn1=new JButton(登录);static JButton btn2=new JButton(取消);static JButton btn3=new JButton(注册帐号);static JButton btn4=new JButton(发送消息);static JButton btn5=new JButton(传送文件);static JButton btn6=new JButton(注销退出);static JButton btn7=new JButton(刷新);static JButton btn8=new JButton(接受);static JButton btn9=new JButton(拒绝);static ImageIcon general=new ImageIcon(捕获.PNG);static JLabel lab2=new JLabel(general);static JCheckBox ckb1=new JCheckBox(记住密码,true);static TextField txa1=new TextField(,10);static TextField txa2=new TextField(,10);static JTextArea txa3=new JTextArea(,6,10);static JTextArea txa4=new JTextArea();static JScrollPane listScroller;static String st=new String4;static String name=new String100;static String names;static String ip;static Thread t1;public static void main(String args) throws Exception name0=所有人;for(int i=1;i0)tryout.write(te.list.getSelectedValue()+rn+st).getBytes(); catch (IOException e1)e1.printStackTrace();txa4.setText();else if(btn=btn3)if(btn1.getText().equals(登录)btn1.setText(注册);elsebtn1.setText(登录);else if(btn=btn2)tryout.write($3.getBytes(); catch (IOException e1)e1.printStackTrace();System.exit(0);else if(btn=btn1)if(txa1.getText().equals()|txa2.getText().equals()lab3.setText(帐号或密码不能为空 !);dlg1.setVisible(true);return;if(ckb1.isSelected()st1=txa1.getText();st2=txa2.getText();tryFileWriter fw=new FileWriter(chat.ini);BufferedWriter bfw=new BufferedWriter(fw);for(int i=0;i3;i+)bfw.write(sti);bfw.newLine();bfw.flush();bfw.close(); catch (IO

温馨提示

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

评论

0/150

提交评论