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