ImageVerifierCode 换一换
格式:DOCX , 页数:40 ,大小:24.78KB ,
资源ID:3983335      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3983335.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(聊天室代码.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

聊天室代码.docx

1、聊天室代码import java.applet.*; import java.awt.*; import java.io.*; import .*; import java.awt.event.*; public class ChatClient extends Applet protected boolean loggedIn;/登入状态 protected Frame cp;/聊天室框架 protected static int PORTNUM=7777; /缺省端口号7777 protected int port;/实际端口号 protected Socket sock; protect

2、ed BufferedReader is;/用于从sock读取数据的BufferedReader protected PrintWriter pw;/用于向sock写入数据的PrintWriter protected TextField tf;/用于输入的TextField protected TextArea ta;/用于显示对话的TextArea protected Button lib;/登入按钮 protected Button lob;/登出的按钮 final static String TITLE =Chatroom applet; protected String paintMe

3、ssage;/发表的消息 public ChatParameter Chat; public void init() paintMessage=正在生成聊天窗口; repaint(); cp=new Frame(TITLE); cp.setLayout(new BorderLayout(); String portNum=getParameter(port);/呢个参数勿太明 port=PORTNUM; if (portNum!=null) /书上是portNum=null,十分有问题 port=Integer.parseInt(portNum); /CGI ta=new TextArea(1

4、4,80); ta.setEditable(false);/read only attribute ta.setFont(new Font(Monospaced,Font.PLAIN,11); cp.add(BorderLayout.NORTH,ta); Panel p=new Panel(); Button b; /for login button p.add(lib=new Button(Login); lib.setEnabled(true); lib.requestFocus(); lib.addActionListener(new ActionListener() public vo

5、id actionPerformed(ActionEvent e) login(); lib.setEnabled(false); lob.setEnabled(true); tf.requestFocus();/将键盘输入锁定再右边的文本框中 ); /for logout button p.add(lob=new Button (Logout); lob.setEnabled(false); lob.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) logout(); lib.s

6、etEnabled(true); lob.setEnabled(false); lib.requestFocus(); ); p.add(new Label (输入消息:); tf=new TextField(40); tf.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(loggedIn) /pw.println(Chat.CMD_BCAST+tf.getText();/Chat.CMD.是咩野来? int j=tf.getText().indexOf(:); if(j0

7、) pw.println(Chat.CMD_MESG+tf.getText(); else pw.println(Chat.CMD_BCAST+tf.getText(); tf.setText();/勿使用flush()? ); p.add(tf); cp.add(BorderLayout.SOUTH,p); cp.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) /如果执行了setVisible或者dispose,关闭窗口 ChatClient.this.cp.setVisible(f

8、alse); ChatClient.this.cp.dispose(); logout(); ); cp.pack();/勿明白有咩用? /将Frame cp放在中间 Dimension us=cp.getSize(), them=Toolkit.getDefaultToolkit().getScreenSize(); int newX=(them.width-us.width)/2; int newY=(them.height-us.height)/2; cp.setLocation(newX,newY); cp.setVisible(true); paintMessage=Window s

9、hould now be visible; repaint(); /登录聊天室 public void login() if(loggedIn) return; try sock=new Socket(getCodeBase().getHost(),port); is=new BufferedReader(new InputStreamReader(sock.getInputStream(); pw=new PrintWriter(sock.getOutputStream(),true); catch(IOException e) showStatus(Cant get socket: +e)

10、; cp.add(new Label(Cant get socket: +e); return; /构造并且启动读入器,从服务器读取数据,输出到文本框中 /这里,长成一个线程来避免锁住资源(lockups) new Thread (new Runnable() public void run() String line; try while(loggedIn &(line=is.readLine()!=null) ta.appendText(line+n); catch(IOException e) showStatus(我的天啊,掉线了也!); return; ).start(); /假定登

11、录(其实只是打印相关信息,并没有真正登录) / pw.println(Chat.CMD_LOGIN+AppletUser); pw.println(Chat.CMD_LOGIN+AppletUser); loggedIn =true; /模仿退出的代码 public void logout() if(!loggedIn) return; loggedIn=false; try if(sock!=null) sock.close(); catch(IOException ign) / 异常处理哦 /没有设置stop的方法,即使从浏览器跳到另外一个网页的时候 /聊天程序还可以继续运行 public

12、 void paint(Graphics g) Dimension d=getSize(); int h=d.height; int w=d.width; g.fillRect(0,0,w,2); g.setColor(Color.black); g.drawString(paintMessage,10,(h/2)-5); 聊天室服务器端 import .*; import java.io.*; import java.util.*; public class ChatServer /聊天室管理员ID protected final static String CHATMASTER_ID=Ch

13、atMaster; /系统信息的分隔符 protected final static String SEP=: ; /服务器的Socket protected ServerSocket servSock; /当前客户端列表 protected ArrayList clients; /调试标记 protected boolean DEBUG=false; public ChatParameter Chat; /主方法构造一个ChatServer,没有返回值 public static void main(String argv) System.out.println(Chat server0.1

14、 starting); ChatServer w=new ChatServer(); w.runServer(); System.out.println(*ERROR* Chat server0.1 quitting); /构造和运行一个聊天服务 ChatServer() Chat=new ChatParameter(); clients=new ArrayList(); try servSock=new ServerSocket(7777);/实有问题拉,不过可能是他自己定义既一个class. System.out.println(Chat Server0.1 listening on po

15、rt:+7777); catch(Exception e) log(IO Exception in ChatServer.); System.exit(0); public void runServer() try while(true) Socket us=servSock.accept(); String hostName=us.getInetAddress().getHostName(); System.out.println(Accpeted from +hostName); /一个处理的线程 ChatHandler cl=new ChatHandler(us,hostName); s

16、ynchronized(clients) clients.add(cl); cl.start(); if(clients.size()=1) cl.send(CHATMASTER_ID,Welcome!You are the first one here); else cl.send(CHATMASTER_ID,Welcome!You are the latest of+ clients.size()+ users.); catch(Exception e) log(IO Exception in runServer:+e); System.exit(0); protected void lo

17、g(String s) System.out.println(s); /处理会话的内部的类 protected class ChatHandler extends Thread /客户端scoket protected Socket clientSock; /读取socket的BufferedReader protected BufferedReader is ; /在socket 上发送信息行的PrintWriter protected PrintWriter pw; /客户端出主机 protected String clientIP; /句柄 protected String login;

18、 public ChatHandler (Socket sock,String clnt)throws IOException clientSock=sock; clientIP=clnt; is=new BufferedReader( new InputStreamReader(sock.getInputStream(); pw=new PrintWriter (sock.getOutputStream(),true); /每一个ChatHandler是一个线程,下面的是他的run()方法 /用于处理会话 public void run() String line; try while(li

19、ne=is.readLine()!=null) char c=line.charAt(0);/我顶你老母啊 ,果只Chat.CMD咩xx冇定义 扑啊/! line=line.substring(1); switch(c) /case Chat.CMD_LOGIN: case l: if(!Chat.isValidLoginName(line) send(CHATMASTER_ID,LOGIN+line+invalid); log(LOGIN INVALID from:+clientIP); continue; login=line; broadcast(CHATMASTER_ID,login+

20、 joins us,for a total of+ clients.size()+ users); break; / case Chat.CMD_MESG: case m: if(login=null) send(CHATMASTER_ID,please login first); continue; int where =line.indexOf(Chat.SEPARATOR); String recip=line.substring(0,where); String mesg=line.substring (where+1); log(MESG: +login+-+recip+: +mes

21、g); ChatHandler cl=lookup(recip); if(cl=null) psend(CHATMASTER_ID,recip+not logged in.); else cl.psend(login,mesg); break; /case Chat.CMD_QUIT: case q: broadcast(CHATMASTER_ID,Goodbye to +login+clientIP); close(); return;/ChatHandler结束 / case Chat.CMD_BCAST: case b: if(login!=null) broadcast(login,l

22、ine); else log(B); else if(clients.size()=1) ChatHandler last=(ChatHandler)clients.get(0); last.send(CHATMASTER_ID,Hey,you are talking to yourself again); else broadcast(CHATMASTER_ID,There are now+clients.size()+ users); protected void close() if(clientSock=null) log(close when not open); return; t

23、ry clientSock.close(); clientSock=null; catch(IOException e) log(Failure during close to +clientIP); /发送一条消息给用户 public void send(String sender,String mesg) pw.println(sender+SEP+*+mesg); /发送私有的消息 protected void psend(String sender ,String msg) send(,msg); /发送一条消息给所有的用户 public void broadcast (String

24、sender,String mesg) System.out.println(Broadcasting+sender+SEP+mesg); for(int i=0;iclients.size();i+) ChatHandler sib=(ChatHandler)clients.get(i); if(DEBUG) System.out.println(Sending to+sib); sib.send(sender,mesg); if(DEBUG) System.out.println(Done broadcast); protected ChatHandler lookup(String ni

25、ck) synchronized(clients) for(int i=0;iclients.size();i+) ChatHandler cl=(ChatHandler)clients.get(i); if(cl.login.equals(nick) return cl; return null; /将ChatHandler对象转换成一个字符串 public String toString() return ChatHandler+login+; public class ChatParameter public static final char CMD_BCAST=b; public s

26、tatic final char CMD_LOGIN=l; public static final char CMD_MESG=m; public static final char CMD_QUIT=q; public static final char SEPARATOR=:;/? public static final int PORTNUM=7777; public boolean isValidLoginName(String line) if (line.equals(CHATMASTER_ID) return false; return true; public void mai

27、n(String argv) !import java.awt.*; import .*; import java.awt.event.*; import java.io.*; import java.util.Hashtable; public class ChatArea 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 privateChatLi

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1