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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

简易聊天室一对多Word格式.docx

1、/Applet创建一个服务线程 public static void main(String args) ServerSocket socket=null; Vector m_threads=new Vector(); System.out.println(服务器已启动,正在等待客户的请求.); try /设置Server监听端口号为8000, 这个数字必须 /和程序ChatClient中的port参数一致。 socket=new ServerSocket(8000); catch(Exception e)服务接口建立失败! return; int nid=0; while(true) /监听

2、是否有新Chat Applet连接到Server, /线程运行到该语句会封锁,直到有新的连接产生。 Socket s=socket.accept(); /创建一个新的ServerThread. ServerThread st=new ServerThread(s,m_threads); /为该线程设置一个ID号。 st.setID(nid+); /将该线程加入到m_threads Vector中。 m_threads.addElement(st); /启动服务线程。 new Thread(st). start(); /通知所有Chat Applet有一个新的网友加入。 for(int i=0;

3、im_threads.size();i+) ServerThread st1=(ServerThread)m_threads.elementAt(i); st1.write(欢迎 +st.getID()+号朋友进入聊天室!接受号客户请求继续等待其他客户的请求.n服务器已关闭.* 监听线程,监听对应的Chat Applet是否有信息传来。 class ServerThread implements Runnable Vector m_threads; Socket m_socket=null; DataInputStream m_in=null; DataOutputStream m_out=n

4、ull; int m_nid; /初始化该线程。 public ServerThread(Socket s,Vector threads) m_socket=s; m_threads=threads; m_in=new DataInputStream(m_socket.getInputStream(); m_out=new DataOutputStream(m_socket.getOutputStream(); public void run() /线程的执行体。等待进程正在运行 /监听对应的Applet是否传来消息 /程序陷入到m_in.readUTF()中,直到有信息传来才返回。 Stri

5、ng s=m_in.readUTF(); if (s=null) break; /如果Chat Applet传来的信息为leave, /则通知所有其他的的Chat Applet自己退出了。 if (s.trim().equals () for (int i=0; ServerThread st=(ServerThread)m_threads.elementAt(i); st.write(*各位朋友, +getID()+号朋友离开聊天室+!* else /向所有Chat Applet广播该信息。朋友说+s); e.printStackTrace(); /从m_threads Vector中删除该

6、线程,表示该线程已经离开聊天室。 m_threads.removeElement(this); m_socket.close(); catch (Exception e) /将msg送回对应的Applet public void write (String msg) synchronized(m_out) m_out.writeUTF(msg); catch(IOException e) public int getID() /获得该线程的ID. return m_nid; public void setID(int nid) / /设置线程的ID. m_nid=nid;客户端用的Applet

7、代码如下:import java.awt.*;import java.awt.event.*;import java.applet.*;/import com.borland.jbcl.layout.*;import javax.swing.*;public class ChatClientApplet extends Applet implements Runnable private boolean isStandalone = false; JLabel jLabel1 = new JLabel(); JTextField jTextField1 = new JTextField();

8、JLabel jLabel2 = new JLabel(); JButton jButton1 = new JButton(); DataInputStream m_in; /消息输入流 DataOutputStream m_out; /消息输出流 JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel3 = new JLabel(); /Get a parameter value public String getParameter(String k

9、ey, String def) return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); /Construct the applet public ChatClientApplet() /Initialize the applet public void init() m_in=null; m_out=null; try URL url=getCodeBase(); /获取applet 的URL 值。 /获取服务器IP地址 InetAdd

10、ress inetaddr=InetAddress.getByName(url.getHost(); Socket m_socket; /屏幕显示服务器IP地址、通讯协议服务器:+inetaddr+ +url.getHost()+url.getProtocol(); m_socket=new Socket(inetaddr,8000); /创建与服务器IP地址连接的套接口 /在套接口上建立输入流 m_in=new DataInputStream(m_socket.getInputStream(); /在套接口上建立输出流 catch (Exception e)Error:+e); try jb

11、Init(); catch(Exception e) /Component initialization private void jbInit() throws Exception jLabel1.setFont(new java.awt.Font(DialogInput, 1, 20); jLabel1.setForeground(Color.red); jLabel1.setToolTipText(作者:杨林森 Email:yanglinsen jLabel1.setText(一对多聊天程序Java Applet客户端 jLabel1.setBounds(new Rectangle(81

12、, 22, 358, 38); this.setBackground(UIManager.getColor(ComboBox.selectionBackground); this.setLayout(null); jLabel2.setFont(new java.awt.Font(Dialog, 1, 16); jLabel2.setForeground(Color.blue); jLabel2.setText(输入发送消息: jLabel2.setBounds(new Rectangle(35, 78, 124, 37); jButton1.setBackground(UIManager.g

13、etColor(OptionPane.questionDialog.titlePane.background jButton1.setBounds(new Rectangle(413, 77, 70, 39); jButton1.setFont(new java.awt.Font( jButton1.setForeground(Color.blue); jButton1.setToolTipText(点击发送按钮即可发送文本框中输入的消息 jButton1.setText(发送 jButton1.addActionListener(new ChatClientApplet_jButton1_a

14、ctionAdapter(this); jTextField1.setBackground(new Color(182, 231, 223); jTextField1.setFont(new java.awt.Font(, 0, 17); jTextField1.setToolTipText(此文本框用来输入发送的消息 jTextField1.setBounds(new Rectangle(153, 80, 248, 35); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); j

15、ScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane1.getViewport().setBackground(UIManager.getColor(MenuItem.acceleratorForeground jScrollPane1.setBounds(new Rectangle(54, 130, 410, 231); jTextArea1.setBackground(UIManager.getColor(Desktop.background jTextArea1

16、.setFont(new java.awt.Font(, 0, 18); jTextArea1.setForeground(Color.black); jLabel3.setFont(new java.awt.Font(, 0, 16); jLabel3.setForeground(Color.blue); jLabel3.setText(如想离开聊天室,可在发送框中输入字符串leave并发送 jLabel3.setBounds(new Rectangle(62, 367, 394, 24); this.add(jLabel1, null); this.add(jLabel2, null);

17、this.add(jTextField1, null); this.add(jButton1, null); this.add(jScrollPane1, null); this.add(jLabel3, null); jScrollPane1.getViewport().add(jTextArea1, null); /启动监听线程 new Thread(this).start(); /Get Applet information public String getAppletInfo() return Applet Information; /Get parameter info publi

18、c String getParameterInfo() return null; public void run() /监听服务者发来的消息,线程将阻塞在该语句中,直到消息到来。 /读一个UTF格式字符串。 if(s!=null) /将消息显示在信息显示窗口中。 jTextArea1.append(s+n jTextArea1.append(Network problem or Sever down.n jTextArea1.setVisible(false); public void stop() m_out.writeUTF( /如想离开聊天室,可在发送框中输入字符串leave void jButton1_actionPerformed(ActionEvent e) String b = jTextField1.getText(); jTextField1.setText( /将用户输入的消息发送给 Chat Server m_out.writeUTF(b); /向服务者发送一UTF格式字符串。 catch(IOException g)

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

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