




免费预览已结束,剩余7页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ChatClient.javaimport java.awt.*;import java.io.*;import .*;import java.applet.*;import java.util.Hashtable;public class ChatClient extends Applet implements Runnable Socket socket=null; DataInputStream in=null;/读取服务器端发来的消息 DataOutputStream out=null;/向服务器端发送的消息 InputInfo 用户名提交界面=null; UserChat 聊天界面=null; Hashtable listTable;/用于存放在线用户的用户名的散列表 Label 提示条; Panel north,center; Thread thread; public void init() setSize(1000,800); int width=getSize().width; int height=getSize().height; listTable=new Hashtable(); setLayout(new BorderLayout(); 用户名提交界面=new InputInfo(listTable); int h=用户名提交界面.getSize().height; 聊天界面=new UserChat(,listTable,width,height-(h+5); 聊天界面.setVisible(false); 提示条=new Label(正在连接到服务器.,Label.CENTER); 提示条.setForeground(Color.red); north=new Panel(new FlowLayout(FlowLayout.LEFT); center=new Panel(); north.add(用户名提交界面); north.add(提示条); center.add(聊天界面); add(north,BorderLayout.NORTH); add(center,BorderLayout.CENTER); validate(); public void start() if(socket!=null&in!=null&out!=null) try socket.close(); in.close(); out.close(); 聊天界面.setVisible(false); catch(Exception ee) /客户端与服务器端简历连接 try socket=new Socket(this.getCodeBase().getHost(),6666); in=new DataInputStream(socket.getInputStream(); out=new DataOutputStream(socket.getOutputStream(); catch(IOException ee) 提示条.setText(连接失败); /客户端成功连接服务器端 if(socket!=null) InetAddress address=socket.getInetAddress(); 提示条.setText(连接:+address+成功); 用户名提交界面.setSocketConnection(socket,in,out); north.validate(); if(thread=null) thread=new Thread(this); thread.start(); public void stop() try socket.close(); thread=null; catch(IOException e) this.showStatus(e.toString(); public void run() while(thread!=null) if(用户名提交界面.getchatornot()=true) 聊天界面.setVisible(true); 聊天界面.setName(用户名提交界面.getName(); 聊天界面.setSocketConnection(socket,in,out); 提示条.setText(祝聊天快乐!); center.validate(); break; try Thread.sleep(100); catch(Exception e) ChatMain.javaimport java.io.*;import .*;import java.util.*;public class ChatMain public static void main(String args) ServerSocket server=null; Socket you=null; Hashtable peopleList; peopleList=new Hashtable(); while(true) try /服务器端在端口6666处监听来自客户端的信息 server=new ServerSocket(6666); catch(IOException e1) System.out.println(正在监听); try /当服务器端接收到客户端的消息后,取得客户端的IP地址。 you=server.accept(); InetAddress address=you.getLocalAddress(); System.out.println(用户名的IP:+address); catch(IOException e) /为每一个客户端简历一个线程,用来记录客户端用户的信息 if(you!=null) Server_thread peopleThread=new Server_thread(you,peopleList); peopleThread.start(); else continue; InputInfo.javaimport java.awt.*;import .*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class InputInfo extends Panel implements ActionListener,Runnable TextField nameFile=null; String name=null; Checkbox male=null,female=null; CheckboxGroup group=null; Button 进入聊天室=null,退出聊天室=null; Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread thread=null; boolean chatornot=false; Hashtable listTable; public InputInfo(Hashtable listTable) this.listTable=listTable; nameFile=new TextField(10); group=new CheckboxGroup(); male=new Checkbox(男,true,group); female=new Checkbox(女,false,group); 进入聊天室=new Button(进入); 退出聊天室=new Button(退出); 进入聊天室.addActionListener(this); 退出聊天室.addActionListener(this); thread=new Thread(this); add(new Label(用户名:); add(nameFile); add(male); add(female); add(进入聊天室); add(退出聊天室); 退出聊天室.setEnabled(false); public void setchatornot(boolean b) chatornot=b; public boolean getchatornot() return chatornot; public String getName() return name; public void setName(String s) name=s; public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out) this.socket=socket; this.in=in; this.out=out; try thread.start(); catch(Exception e) nameFile.setText(+e); public Socket getSocket() return socket; /通过ActionEvent的getSource()方法判断哪个按钮被按下 /并根据用户不用的命令做出相应的处理 public void actionPerformed(ActionEvent e) if(e.getSource()=进入聊天室) 退出聊天室.setEnabled(true); if(chatornot=true) nameFile.setText(您正在聊天:+name); else this.setName(nameFile.getText(); String sex=group.getSelectedCheckbox().getLabel(); if(socket!=null&name!=null) try out.writeUTF(姓名:+name+性别:+sex); catch(IOException ee) nameFile.setText(没有连通服务器+ee); if(e.getSource()=退出聊天室) try out.writeUTF(用户离开:); catch(IOException ee) /读取服务器端发来的消息,根据消息来判断用户是否能聊天或显示聊天者信息 public void run() String message=null; while(true) if(in!=null) try message=in.readUTF(); catch(IOException e) nameFile.setText(和服务器断开+e); if(message.startsWith(可以聊天:) chatornot=true; break; else if(message.startsWith(聊天者:) String people=message.substring(message.indexOf(:)+1); listTable.put(people, people); else if(message.startsWith(不可以聊天:) chatornot=false; nameFile.setText(该用户名已被占用); Server_thread.javaimport java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.IOException;import .Socket;import java.util.Enumeration;import java.util.Hashtable;public class Server_thread extends Thread String name=null,sex=null; Socket socket=null; File file=null; DataOutputStream out=null; DataInputStream in=null; Hashtable peopleList=null; Server_thread(Socket t,Hashtable list) peopleList=list; socket=t; try in=new DataInputStream(socket.getInputStream(); out=new DataOutputStream(socket.getOutputStream(); catch(IOException e) public void run() while(true) String s=null; try s=in.readUTF(); /读取客户端发送的消息,该消息中包含用户提交的个人信息及聊天内容等 /服务器端根据该消息判断用户名时候重复 if(s.startsWith(姓名:) name=s.substring(s.indexOf(:)+1, s.indexOf(性别); sex=s.substring(s.lastIndexOf(:)+1); boolean boo=peopleList.containsKey(name); /如果用户名不重复,则用户可以进入聊天室 if(boo=false) peopleList.put(name, this); out.writeUTF(可以聊天:); Enumeration enums=peopleList.elements(); while(enums.hasMoreElements() Server_thread th=(Server_thread)enums.nextElement(); th.out.writeUTF(聊天者:+name+性别+sex); if(th!=this) out.writeUTF(聊天者:++性别+th.sex); /如果用户名重复,则用户名不可以进入聊天室 else out.writeUTF(不可以聊天:); /用户所发送的聊天内容为公共聊天内容 else if(s.startsWith(公共聊天内容:) String message=s.substring(s.indexOf(:)+1); Enumeration enums=peopleList.elements(); while(enums.hasMoreElements() (Server_thread)enums.nextElement().out.writeUTF(聊天内容:+message); else if(s.startsWith(用户离开:) Enumeration enums=peopleList.elements(); while(enums.hasMoreElements() try Server_thread th=(Server_thread)enums.nextElement(); if(th!=this&th.isAlive() th.out.writeUTF(用户离线:+name); catch(IOException eee) peopleList.remove(name); socket.close(); System.out.println(name+用户离开了); break; /用户所发送的聊天内容为私人聊天内容 else if(s.startsWith(私人聊天内容) String 悄悄话=s.substring(s.indexOf(:)+1, s.indexOf(#); String toPeople=s.substring(s.indexOf(#)+1); Server_thread toThread=(Server_thread)peopleList.get(toPeople); if(toThread!=null) toThread.out.writeUTF(私人聊天内容:+悄悄话); else out.writeUTF(私人聊天内容:+toPeople+已经离线); catch(IOException ee) Enumeration enums=peopleList.elements(); while(enums.hasMoreElements() try Server_thread th=(Server_thread)enums.nextElement(); if(th!=this&th.isAlive() th.out.writeUTF(用户离线:+name); catch(IOException eee) peopleList.remove(name); try socket.close(); catch(IOException eee) System.out.println(name+用户离开了); break; UserChat.javaimport java.awt.*;import .*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class UserChat extends Panel implements ActionListener,Runnable Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread threadMessage=null; TextArea 谈话显示区,私聊显示区=null; TextField 送出信息=null; Button 确定,刷新谈话区,刷新私聊区; Label 提示条=null; String name=null; Hashtable listTable; List listComponent=null; Choice privateChatList; int width,height; public UserChat(String name,Hashtable listTable,int width,int height) setLayout(null); setBackground(Color.orange); this.width=width; this.height=height; setSize(width+350,height); this.listTable=listTable; =name; threadMessage=new Thread(this); 谈话显示区=new TextArea(20,20); 私聊显示区=new TextArea(20,20); 确定=new Button(发送); 刷新谈话区=new Button(刷新谈话区); 刷新私聊区=new Button(刷新私聊区); 提示条=new Label(双击可私聊,Label.CENTER); 送出信息=new TextField(28); 确定.addActionListener(this); 送出信息.addActionListener(this); 刷新谈话区.addActionListener(this); 刷新私聊区.addActionListener(this); listComponent=new List(); listComponent.addActionListener(this); privateChatList=new Choice(); privateChatList.add(大家(*); privateChatList.select(0); add(谈话显示区); 谈话显示区.setBounds(10, 10, (width-120)/2, (height-120); add(私聊显示区); 私聊显示区.setBounds(10+(width-120)/2, 10, (width-120)/2, (height-120); add(listComponent); listComponent.setBounds(10+(width-120), 10, 100, (height-160); add(提示条); 提示条.setBounds(10+(width-120), 10+(height-160), 100, 40); Panel pSouth=new Panel(); pSouth.add(送出信息); pSouth.add(确定); pSouth.add(privateChatList); pSouth.add(刷新谈话区); pSouth.add(刷新私聊区); add(pSouth); pSouth.setBounds(10,20+(height-120),width-20,60); public void setName(String s) name=s; /和服务器端建立连接 public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out) this.socket=socket; this.in=in; this.out=out; try threadMessage.start(); catch(Exception e) /通过ActionEventd的getSourse()方法判断哪个按钮被按下 /并根据用户不同的命令作出相应的处理 public void actionPerformed(ActionEvent e) if(e.getSource()=确定|e.getSource()=送出信息) String message=; String people=privateChatList.getSelectedItem(); people=people.substring(5, people.indexOf(); message=送出信息.getText(); /如果用户单击发送按钮并且发送的内容为公共聊天,则在谈话区显示聊天内容 /如果用户聊天为私聊,则在私聊区显示聊天内容 if(message.length()0) try if(people.equals(大家) out.writeUTF(公共聊天内容:+name+说:+message); else out.writeUTF(私人聊天内容:+name+悄悄地说:+message); catch(IOException event) else if(e.getSource()=listComponent) privateChatList.insert(listComponent.getSelectedItem(),
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 媒体传播企业会计核算及财务风险防控合同
- 离婚协议中子女抚养及教育费用支付协议范本
- 离婚协议书模板(夫妻共同债务清偿)
- 离婚协议签订后共同财产分割及债权债务处理协议
- 形体礼仪培训内容
- 老干部工作课件
- 邮储银行2025怀化市秋招面试典型题目及参考答案
- 邮储银行2025衢州市秋招笔试EPI能力测试题专练及答案
- 邮储银行2025定西市秋招笔试热点题型专练及答案
- 建设银行2025济源市秋招笔试EPI能力测试题专练及答案
- 攻读工程博士专业学位研究计划书【模板】
- NBT 10643-2021 风电场用静止无功发生器技术要求与试验方法-PDF解密
- 初中英语单词表(For-Junior)2182个 带音标
- 人教鄂教版六年级上册科学全册教案
- 财务工作内部培训课件
- 铁路防雷及接地工程技术规范(TB 10180-2016)
- 网络安全意识培训
- 建筑艺术赏析(职业通用)全套教学课件
- 无人机理论知识无人机理论基础
- 医院检验科质量手册
- 农业科技在2024年的发展与前景展望
评论
0/150
提交评论