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

📄 appframe.java

📁 用JAVA写的支持IPV6的P2P多线程聊天程序
💻 JAVA
字号:
// AppFrame.java
import java.io.*;
import java.net.InetAddress;

//主程序框架,如GUI的 实现等
public class AppFrame  {

  protected String username;
  protected MulticastCom mc;

  //Init Mulitcast commnunication
  public void InitMulticastCom ( String username, InetAddress group, int port, 
                                int ttl) throws IOException {
    this.username=username;
    mc = new MulticastCom(group, port, ttl, this);
  }

  // Invoked by the MulticastCom receive thread when a message has arrived
  public int MessageReceived(byte data[], int offset, int len) { 
	String message = new String(data, 0, len);
    System.out.println("Data Received :   "+ message);
    return len; 
  }
  
  // 发送测试
  public void SendTest() { 
    
     BufferedReader systemInBr = new BufferedReader(new InputStreamReader(System.in));
     try { 
	    String line;
	    while (true) {
	       System.out.println("input a line : ");
	       line = systemInBr.readLine();
	       line = "<"+username +"> :" + line;
	       byte[] data = line.getBytes();
           mc.sendMessage(data,0,data.length);
    	}
     } catch (IOException ioe) {
    	System.err.println("IOException"+ioe.getMessage());
     }
     return; 
  }
  

  public static void main(String[] args) 
  {

    if ((args.length != 3) && (args.length != 4)) {
      System.err.println("Usage: AppFrame " 
                         + "<username> <group> <port> { <ttl> }");
      System.err.println("       - default time-to-live value is 1");
      System.exit(1);
    } 

    String username = args[0];
    InetAddress group = null;
    int port = -1;
    int ttl = 1;

    try {
      group = InetAddress.getByName(args[1]);
    } catch (Throwable e) {
      System.err.println("Invalid multicast group address: " 
                         + e.getMessage());
      System.exit(1);
    } 

    if (!group.isMulticastAddress()) {
      System.err.println("Group argument '" + args[1] 
                         + "' is not a multicast address");
      System.exit(1);
    } 

    try {
      port = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
      System.err.println("Invalid port number argument: " + args[2]);
      System.exit(1);
    } 

    if (args.length >= 4) {
      try {
        ttl = Integer.parseInt(args[3]);
      } catch (NumberFormatException e) {
        System.err.println("Invalid TTL number argument: " + args[3]);
        System.exit(1);
      } 
    } 

   try {
   	
   	   AppFrame frame = new AppFrame();
       frame.InitMulticastCom(username, group, port, ttl);

       frame.SendTest();
       
    } catch (Throwable e) {
         System.err.println("Error starting frame: " + e.getClass().getName() 
                         + ": " + e.getMessage());
         System.exit(1);
    }    

  
  } 

}

⌨️ 快捷键说明

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