⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatapplet.java

📁 基于Socket的java多用户视频聊天程序
💻 JAVA
字号:
package chatapplet;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.net.*;import java.io.*;import java.util.*;/** * Title:        客户端Applet小程序 * Description:  利用Socket建立聊天室客户端Applet小程序。 * Copyright:    Copyright (c) 2002 * Company:      中国农业大学计算机系 * @author 彭波 * @version 1.0 */	public class chatApplet extends Applet {	  Panel panel1 = new Panel();					// 放置输入姓名和连接的两个按钮	  BorderLayout borderLayout1 = new BorderLayout();	  Panel panel2 = new Panel();					// 放置聊天信息显示和聊天人员列表	  Panel panel3 = new Panel();					// 放置发送信息区域	  FlowLayout flowLayout1 = new FlowLayout();	  FlowLayout flowLayout2 = new FlowLayout();	  Label label1 = new Label();	  TextField name_txt = new TextField(15);	  Button button1 = new Button();	  Button button2 = new Button();	  TextArea chat_txt = new TextArea(15,30);	  Label label2 = new Label();	  Button button3 = new Button();	  TextField msg_txt = new TextField(20);	  java.awt.List list1 = new java.awt.List(13);	  Socket soc=null;							// 定义连接套接字	  PrintStream ps=null;							// 定义打印输出流	  Listen listen=null;							// 定义一个客户端线程监听	  public void init(){							// 初始化图形界面		resize(475,350);		this.setLayout(borderLayout1);		panel2.setLayout(flowLayout1);		panel3.setLayout(flowLayout2);		label1.setText("姓名:");		button1.setLabel("连接");		button2.setLabel("断开连接");		chat_txt.setEditable(false);		panel2.setBackground(Color.cyan);		panel1.setBackground(Color.cyan);		label2.setText("聊天信息:");		button3.setLabel("发送");		msg_txt.setText("请输入聊天信息");		panel3.setBackground(Color.cyan);		this.add(panel1, BorderLayout.NORTH);		panel1.add(label1, null);		panel1.add(name_txt, null);		panel1.add(button1, null);		panel1.add(button2, null);		this.add(panel2, BorderLayout.CENTER);		panel2.add(chat_txt, null);		panel2.add(list1, null);		this.add(panel3,  BorderLayout.SOUTH);		panel3.add(label2, null);		panel3.add(msg_txt, null);		panel3.add(button3, null);	  }	  public boolean action(Event evt,Object obj) {		// 事件处理代码	    if(evt.target instanceof Button){		 String label=(String) obj;		 if(label.equals("连接")){					// 点击连接		   if(soc==null){			try{			  soc=new Socket(InetAddress.getLocalHost(),8080);  // 使用端口8080实例化本地套接字			  System.out.println(soc);					   // 打印实例化结果			  ps=new PrintStream(soc.getOutputStream());	   // 将ps指向soc的输出流			  StringBuffer info=new StringBuffer("INFO: ");	   // 定义字符缓冲存储发送信息			  // 其中INFO为关键字让服务器识别连接信息			  String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString();			  // 将name和ip用":"分开			  ps.println(info.append(userinfo));			  ps.flush();			  listen=new Listen(this,name_txt.getText(),soc);		// 实例化客户端线程			  listen.start();								// 启动线程			}			catch(IOException e){			  System.out.println("Error:"+e);			  disconnect();			}		   }		// end of if	      }		// end of if	      else if(label.equals("断开连接")){					// 点击断开连接按钮		   disconnect();	      }	      else if(label.equals("发送")){						// 点击发送按钮		   if(soc!=null) {		     StringBuffer msg=new StringBuffer("MSG: "); // 定义并实例化字符缓冲存储发送聊天信息		     try {			  String msgtxt=new String(msg_txt.getText());		     }		     catch(Exception e){}		     ps.println(msg.append(msg_txt.getText()));			// 打印输出流发送聊天信息		     ps.flush();		   }	      }	    }         return true;	  }		// end of method action	  public void disconnect(){								// 客户端断开连接方法	     if(soc!=null){		  try	{			listen.suspend();			ps.println("QUIT");				// 发送QUIT信息通知服务器断开此次通讯			ps.flush();			soc.close();									// 关闭套接字		  }		  catch(IOException e) {		     System.out.println("Error:"+e);		  }		  finally { }		}		// end of if	  }	  class Listen extends Thread{				// 定义客户端线程类用于监听服务器传来的信息	    String name=null;						// 存储客户端连接后的name信息	    DataInputStream dis=null;				// 实现客户端接收服务器数据的输入流	    PrintStream ps=null;					// 实现从客户端发送数据到服务器的打印输出流	    Socket socket=null;					// 存储客户端的socket信息	    chatApplet parent=null;					// 存储当前运行的chatApplet实例	    public Listen(chatApplet p,String n,Socket s) {	 // Listen类的构造器		  parent=p;		  name=n;		  socket=s;		  try{							// 实例化两个数据流		    dis=new DataInputStream(s.getInputStream());		    ps=new PrintStream(s.getOutputStream());		  }		  catch(IOException e) {		    System.out.println("Error:"+e);		    parent.disconnect();		  }	    }		// end of Listen constractor	    public void run(){						// 定义线程run()方法		  String msg=null;		  while(true)	{		    try{msg=dis.readLine();}			// 读取从服务器传来的信息		    catch(IOException e) {			 System.out.println("Error:"+e);			 parent.disconnect();		    }		    if (msg==null) {					// 从服务器传来的信息为空则断开此次连接		      parent.listen=null;		      parent.soc=null;		      parent.list1.clear();		      return;		    }		    StringTokenizer st=new StringTokenizer(msg,":");	 // 读取分段字符		    String keyword=st.nextToken();			 // 读取信息头(关键字)识别何种信息		    if(keyword.equals("PEOPLE")){			 // PEOPLE为服务器发送的客户连接信息		       parent.list1.clear();			   while(st.hasMoreTokens()){			// 遍历st取得目前所连接的客户			     String str=st.nextToken();				 parent.list1.addItem(str);			   }		    }		    else if(keyword.equals("MSG")){			// MSG为服务器传送的聊天信息,用于显示		      String usr=st.nextToken();			  parent.chat_txt.appendText(usr);			  parent.chat_txt.appendText(st.nextToken("\0"));			  parent.chat_txt.appendText("\n\n");		    }		    else if(keyword.equals("QUIT")){			// QUIT为服务器关闭的信息,切断连接		       System.out.println("Quit");		       try {			    parent.listen.stop();		         parent.listen=null;		         parent.soc.close();			    parent.soc=null;                }			  catch(IOException e) {             	    System.out.println("Error:"+e);             	  }			  parent.list1.clear();			  return;		    }	     }	   }			// end of run method	  }      		// end of Listen inner class	}			// end of chatApplet class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -