要有如下的要求:①注册功能:主要实现申请 “J-QQ”系统账号的功能。在本系统中,要实现即时交流就必须要拥有合法的账号才能进行。一个新的用户在提交自己的一些描述信息的前提下(如:用户的姓名、昵称、性别等信息),由服务器为其分配一个唯一的“J-QQ”账号。②客户登录功能:主要实现从客户端登录“J-QQ”系统。其工作过程是:客户在登录时需要给出客户的账号和口令,并将账号和口令送服务端进行身份验证,当验证通过时服务器将该用户的好友信息发回客户端。③添加好友功能:主要实现添加好友功能。在本系统中客户只能与好友聊天,所以在与某位用户聊天之前必须先将该好友添加到好友列中。其主要工作过程是:首先输入好友的账号,然后由客户端提交到服务器,再由服务器询问该客户是否同意将他加为好友,当得到许可后就完成了好友的添加。④删除好友功能:主要实现从好友列表中删除好友的功能。当你不愿意再与某个好友聊天,可以将该好友从好友列表中删除。其工作过程是:首先选择一个待删除的好友,然后向服务器端提出删除好友的请求,当服务器许可即完成好友的删除操作。⑤私聊主要实现好友间单独聊天的功能。其主要过程是:用户首先从好友列表中选择一个好友,然后打开私聊窗口,通过该聊天窗口来实现与好友之间的交流。⑥群聊主要实现与所有好友群聊的功能。其主要过程是:首先打群聊窗口,用户输入群聊信息并由客户端转交到服务器中,服务器则根据该用户的好友列表群发到所有好友的客户端。⑦好友上下线提示主要实现好友上下线提示,也就是说当好友上线时会自动通知其所有已上线的好友,当其下线时也需要自动通知其所有在线的好友。其主要工作过程如下:当好友上线时,服务器会自动取出当前用户的好友列表,并根据该列表对其好友分别进行通知。当用户下线时就会向服务器传送下线命令,再由服务器将好友下线命令转发给其好友。⑧用户管理主要实现用户信息修改的功能。其主要过程是:用户通过客户端程序中的用户信息修改窗口来实现用户个人信息的修改,当信息修改确定后就将该用户的信息传送至服务器,由服务器完成用户信息的更新操作。注意:用户信息一旦修改成功,其在线好友只能重新登录后,才会显示更新后的个人信息。 本人是java 的初学者 请一定要有注释说明 希望是用java基础和核心技术的内容来写(图形界面设计 线程 网络协议 等等 )万分感谢了
客户端: package chatroom; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import javax.swing.*; public class client extends JFrame implements ActionListener,Runnable{ JPanel conn,text,send; JLabel name,sendto; JComboBox list; JButton con,snd,clear; JTextArea talk; JTextField who,say; JScrollPane jsp; Socket client; InputStream is; OutputStream os; PrintStream ps; BufferedReader br; String receive,yousay; Thread th; DataInputStream dis; Double tof; client() { super("聊天室客户端"); this.setSize(800,600); this.setResizable(false); conn=new JPanel(); text=new JPanel(); send=new JPanel(); this.getContentPane().add(conn); conn.setBounds(0, 0, this.getWidth(),50); name=new JLabel("姓名:"); who=new JTextField(); con=new JButton("连接"); conn.setLayout(null); conn.add(name); name.setBounds(30, 10, 50, 25); conn.add(who); who.setBounds(80, 10, 150, 25); conn.add(con); con.setBounds(250,10, 60, 25); this.getContentPane().add(text); text.setBounds(0,50,this.getWidth(),450); text.setLayout(new BorderLayout()); jsp=new JScrollPane(); talk=new JTextArea(); jsp.getViewport().setView(talk); text.add(jsp,"Center"); talk.setLineWrap(true); this.getContentPane().add(send); send.setLayout(null); send.setBounds(0, 480, this.getWidth(), 150); sendto=new JLabel("发送到:"); snd=new JButton("发送"); list=new JComboBox(); say=new JTextField(); clear=new JButton("清空"); send.add(sendto); sendto.setBounds(30, 525, 50, 25); send.add(list); list.setBounds(100, 525, 75, 25); send.add(say); say.setEditable(true); say.setBounds(200, 525, 300, 25); send.add(snd); snd.setBounds(520, 525, 100, 25); send.add(clear); clear.setBounds(650, 525, 100, 25); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); con.addActionListener(this); snd.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("连接")) { try { client=new Socket(InetAddress.getLocalHost(),55555); talk.append("连接成功...\n"); con.setText("断开"); is=client.getInputStream(); os=client.getOutputStream(); th=new Thread(this); String id=who.getText(); byte b[]=id.getBytes(); DataOutputStream dos=new DataOutputStream(os); int len=b.length; dos.writeInt(len); dos.write(b); th.start(); }catch(Exception e){talk.append("连接失败\n"+e.toString()+"0000");} } else if(ae.getSource()==snd) { if(list.getSelectedItem().toString()=="所有人") { yousay="@open"+say.getText(); byte b[]=yousay.getBytes(); DataOutputStream dos=new DataOutputStream(os); try{ int len=b.length; dos.writeInt(len); dos.write(b); }catch(Exception e) { System.out.print(e.toString()+"1111"); } } else { yousay="@whisper"+say.getText(); byte b[]=yousay.getBytes(); byte w[]=list.getSelectedItem().toString().getBytes(); DataOutputStream dos=new DataOutputStream(os); try{ int len=b.length; int wlen=w.length; dos.writeInt(len); //内容 dos.write(b); dos.writeInt(wlen); //发送对象 dos.write(w); }catch(Exception e) { System.out.print(e.toString()+"AAAA"); } } } else if(ae.getActionCommand().equals("断开")) { try { client.close(); talk.append("连接已断开\n"); con.setText("连接"); }catch(Exception e){System.out.println(e.toString()+"2222");} } } public void run() { while(true) { try { dis=new DataInputStream(is); tof=dis.readDouble(); if(tof==1.1) { dis=new DataInputStream(is); int number; list.removeAllItems(); list.addItem("所有人"); number=dis.readInt(); for(int i=0;i<number;i++) { int len=dis.readInt(); byte b[]=new byte[len]; dis.read(b); String name=new String(b); list.addItem(name); } } else if(tof==2.2) { try { dis=new DataInputStream(is); int len=dis.readInt(); byte b[]=new byte[len]; dis.read(b); receive=new String(b); talk.append(receive+"\n"); }catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE);break;} } }catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE);break;} } } public static void main(String[] args) { client cl=new client(); } } 服务端: package chatroom; import java.awt.*; import javax.swing.*; import java.net.*; import java.util.Vector; import java.io.*; public class server extends JFrame implements Runnable{ JPanel jp1; JTextArea jta; JScrollPane jsp; Socket socket=null; ServerSocket server; InputStream is; OutputStream os; static int i=0,login; int no; static String s=""; Thread sr; Thread th[]=new Thread[20]; static Vector ol=new Vector(); static public byte w[]=null; static String from=""; //人名 static String to=""; static String fromtext=""; //内容 static String totext=""; server() { super("聊天室服务端"); this.getContentPane().setLayout(new GridLayout(1,1)); this.setSize(400,300); jp1=new JPanel(); jta=new JTextArea(); jsp=new JScrollPane(); this.getContentPane().add(jp1); jp1.setLayout(new GridLayout(1,1)); jsp.getViewport().setView(jta); jp1.add(jsp); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { server sr=new server(); sr.sr=new Thread(sr); sr.sr.start(); sendtoall sta=new sendtoall(); returnfrom rf=new returnfrom(sr); returnto rt=new returnto(sr); sta.start(); rf.start(); rt.start(); } public void run() { try { server=new ServerSocket(55555); while(true) { socket=server.accept(); is=socket.getInputStream(); DataInputStream dis=new DataInputStream(is); int len=dis.readInt(); byte b[]=new byte[len]; dis.read(b); String id=new String(b); record v=new record(id,socket); ol.addElement(v); for (int i=0;i<ol.size();i++) { if (ol.elementAt(i).equals(v)) { no=i; } } login=1; s=id+"已来到聊天室!"; th[no]=new Thread(new receive(this,no)); th[no].start(); } }catch(Exception e){System.out.println(e.toString()+"aaaa");} } } class receive implements Runnable { InputStream is; OutputStream os; server sr; Socket sk; int i; String name; String ip; receive(server sr,int i) { this.sr=sr; this.i=i; sk=((record)sr.ol.elementAt(i)).ip; name=((record)sr.ol.elementAt(i)).name; ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString(); } public void run() { while(true) { try { is=sk.getInputStream(); DataInputStream dis=new DataInputStream(is); int len=dis.readInt(); byte b[]=new byte[len]; dis.read(b); String abc=new String(b); sr.jta.append(abc); if(abc.substring(0,5).equals("@open")) { server.s=name+"["+ip.substring(1, ip.length())+"]"+"说:"+abc.substring(5,abc.length()); sr.jta.append(server.s+"\n"); } else if(abc.substring(0,8).equals("@whisper")) { int wlen=dis.readInt(); sr.w=new byte[wlen]; dis.read(sr.w); server.to=new String(sr.w); server.from=((record)sr.ol.elementAt(i)).name; String ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString(); // server.s=server.from+"对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length()); server.fromtext="你对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length()); server.totext=server.from+"["+ip.substring(1, ip.length())+"]"+"对你悄悄地说:"+abc.substring(8,abc.length()); sr.jta.append(server.s+"\n"); } }catch(Exception e) { try { DataOutputStream dos=new DataOutputStream(os); server.ol.removeElementAt(i); server.s=name+"已离开聊天室."; server.login=1; break; }catch(Exception f){} } } } } class sendtoall extends Thread { int len,number; byte b[]; server sr; Socket st; OutputStream os; DataOutputStream dos; public void run() { while(true) { try { if(server.login==1) { number=0; number=server.ol.size(); dos=new DataOutputStream(os); for(int i=0;i<server.ol.size();i++) { st=((record)sr.ol.elementAt(i)).ip; os=st.getOutputStream(); dos=new DataOutputStream(os); dos.writeDouble(1.1); dos.writeInt(number); for (int j=0;j<number;j++) { String name=((record)sr.ol.elementAt(j)).name; byte b[]=name.getBytes(); int len=b.length; dos.writeInt(len); dos.write(b); } } server.login=0; } else if(!server.s.equals("")&&sr.ol.size()>0) { b=server.s.getBytes(); //String类型中 汉字占一个字节,但是byte类型中,汉字占两个字节 len=b.length; //len=server.s.length(); //b=new byte[len]; //b=server.s.getBytes(); for(int i=0;i<server.ol.size();i++) { st=((record)sr.ol.elementAt(i)).ip; os=st.getOutputStream(); DataOutputStream dos=new DataOutputStream(os); dos.writeDouble(2.2); dos.writeInt(len); dos.write(b); } server.s=""; } }catch(Exception e){System.out.println(e.toString()+"sdasd");} } } } class returnfrom extends Thread { int len,wlen; byte b[]; byte w[]; server sr; Socket st; OutputStream os; DataOutputStream dos; //String from="",to=""; returnfrom(server sr) { this.sr=sr; } public void run() { while(true) { if(!server.fromtext.equals("")) { b=server.fromtext.getBytes(); len=b.length; sr.jta.append(sr.fromtext); try { for(int i=0;i<server.ol.size();i++) { if(((record)sr.ol.elementAt(i)).name.equals(server.from)) { st=((record)sr.ol.elementAt(i)).ip; os=st.getOutputStream(); DataOutputStream dos=new DataOutputStream(os); dos.writeDouble(2.2); dos.writeInt(len); dos.write(b); } } }catch(Exception e){System.out.println(e.toString()+"wwww");} server.fromtext=""; server.from=""; } } } } class returnto extends Thread { int len,wlen; byte b[]; byte w[]; server sr; Socket st; OutputStream os; DataOutputStream dos; //String from="",to=""; returnto (server sr) { this.sr=sr; } public void run() { while(true) { if(!server.totext.equals("")) { w=server.totext.getBytes(); wlen=w.length; try { for(int i=0;i<server.ol.size();i++) { if(((record)sr.ol.elementAt(i)).name.equals(server.to)) { st=((record)sr.ol.elementAt(i)).ip; os=st.getOutputStream(); DataOutputStream dos=new DataOutputStream(os); dos.writeDouble(2.2); dos.writeInt(wlen); dos.write(w); } } }catch(Exception e){System.out.println(e.toString()+"wwww");} server.totext=""; server.to=""; } } } } class record { String name; Socket ip; record(String id,Socket socket) { name=id; ip=socket; } } 以前学习java时写的,是个在线聊天室只有群聊,私聊,上下线提示,搞不好里面还有错误- -其他功能自己看着加吧,你要的功能实在是很好很强大.