java局域网广播系统

17 篇文章 0 订阅
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

/**
 *
 * @author Administrator
 */
import java.net.*;
import java.awt.*; 
import java.awt.event.*;

public class Receive extends Frame implements Runnable,ActionListener
{ 
  int port;                                        
  InetAddress group=null;                          
  MulticastSocket socket=null;                     
  Button 开始接收,停止接收;   
  TextArea 显示正在接收内容,显示已接收的内容;  
  Thread thread;                                   
  boolean 停止=false;
  public Receive()
   { 
     super("定时接收信息");
     thread=new Thread(this);
     开始接收=new Button("开始接收");
     停止接收=new Button("停止接收");
     停止接收.addActionListener(this); 
     开始接收.addActionListener(this); 
     显示正在接收内容=new TextArea(10,10);
     显示正在接收内容.setForeground(Color.blue); 
     显示已接收的内容=new TextArea(10,10);
     Panel north=new Panel();
     north.add(开始接收);
     north.add(停止接收);
     add(north,BorderLayout.NORTH);
     Panel center=new Panel();
     center.setLayout(new GridLayout(1,2)); 
     center.add(显示正在接收内容);
     center.add(显示已接收的内容);
     add(center,BorderLayout.CENTER);
     validate();
     port=5000;                                       
     try{
         group=InetAddress.getByName("239.255.0.0");  
         socket=new MulticastSocket(port);            
         socket.joinGroup(group);           
                                            
       }
    catch(Exception e)
       {
       } 
   setBounds(100,50,360,380);   
   setVisible(true);
   addWindowListener(new WindowAdapter()
                     { public void windowClosing(WindowEvent e)
                       { System.exit(0);
      	               }
      	             });
                            
   }
  public void actionPerformed(ActionEvent e)
   {
    if(e.getSource()==开始接收)
      {
        开始接收.setBackground(Color.blue);
        停止接收.setBackground(Color.gray);
        if(!(thread.isAlive()))
           {
             thread=new Thread(this);
           }
        try
           {
             thread.start(); 
             停止=false;        
           }
        catch(Exception ee)
           {
           }
      }
    if(e.getSource()==停止接收)
      {
        开始接收.setBackground(Color.gray);
        停止接收.setBackground(Color.blue);
        thread.interrupt(); 
        停止=true; 
      }
   }

  public void run()
  {
   
    while(true)   
    {  
       byte data[]=new byte[8192];
       DatagramPacket packet=null;
       packet=new DatagramPacket(data,data.length,group,port);  
       try
           {  
             socket.receive(packet);
             String message=new String(packet.getData(),0,packet.getLength());
             显示正在接收内容.setText("正在接收的内容:\n"+message);
             显示已接收的内容.append(message+"\n");
           }
      catch(Exception e)
           {
           }
      if(停止==true)
           {
             break;
           } 
    } 
  }
 
 public static void main(String args[])
 { 
    new Receive();
 }
} 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

/**
 *
 * @author Administrator
 */
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
 
public class BroadCastWord extends Frame implements ActionListener
{   
   int port;                                        
   InetAddress group=null;                          
   MulticastSocket socket=null;                     
   Timer time=null;                                 
   FileDialog open=null;                            
   Button select,开始广播,停止广播;
   File file=null;                                  
   String FileDir=null,fileName=null;
   FileReader in=null;                              
   BufferedReader bufferIn=null;
   int   token=0;                                   
   TextArea 显示正在播放内容,显示已播放的内容;       
  public BroadCastWord()
  {
   super("单词广播系统");
   select=new Button("选择要广播的文件");
   开始广播=new Button("开始广播");
   开始广播.setEnabled(false);
   停止广播=new Button("停止广播");
   select.addActionListener(this); 
   开始广播.addActionListener(this); 
   停止广播.addActionListener(this); 
   time=new Timer(2000,this);                       
   open=new FileDialog(this,"选择要广播的文件",FileDialog.LOAD);  
   显示正在播放内容=new TextArea(10,10);
   显示正在播放内容.setForeground(Color.blue); 
   显示已播放的内容=new TextArea(10,10);
   Panel north=new Panel();
   north.add(select);
   north.add(开始广播);
   north.add(停止广播);
   add(north,BorderLayout.NORTH);
   Panel center=new Panel();
   center.setLayout(new GridLayout(1,2)); 
   center.add(显示正在播放内容);
   center.add(显示已播放的内容);
   add(center,BorderLayout.CENTER);
   validate();
   try 
      {
       port=5000;                                   
       group=InetAddress.getByName("239.255.0.0");  
       socket=new MulticastSocket(port);            
       socket.setTimeToLive(1);                     
       socket.joinGroup(group);                     
                                                   
      } 
  catch(Exception e)
       {
         System.out.println("Error: "+ e);          
       }
  setBounds(100,50,360,380);   
  setVisible(true);
  addWindowListener(new WindowAdapter()
                     { public void windowClosing(WindowEvent e)
                       { System.exit(0);
      	               }
      	             });
 }
 public void actionPerformed(ActionEvent e)
 {
   if(e.getSource()==select)
     {
      显示已播放的内容.setText(null);
      open.setVisible(true);
      fileName=open.getFile();
      FileDir=open.getDirectory(); 
      if(fileName!=null)
       { 
         time.stop();                                                
         file=new File(FileDir,fileName);
         try
            {
               file=new File(FileDir,fileName);
               in=new FileReader(file);                      
               bufferIn=new BufferedReader(in);
               开始广播.setEnabled(true);  
            }
         catch(IOException ee)
            {
            }
       }
     }
   else if(e.getSource()==开始广播)
     {
        time.start();
     }
   else if(e.getSource()==time)
     {
       String s=null;
        try
            {  
              if(token==-1)
                 {
                   file=new File(FileDir,fileName);
                   in=new FileReader(file);                      
                   bufferIn=new BufferedReader(in);
                 }
              s=bufferIn.readLine();
              if(s!=null)
                {
                  token=0;
                  显示正在播放内容.setText("正在广播的内容:\n"+s);
                  显示已播放的内容.append(s+"\n");
                  DatagramPacket packet=null;                               
                  byte data[]=s.getBytes(); 
                  packet=new DatagramPacket(data,data.length,group,port); 
                  socket.send(packet);                                      
                }
              else
                {
                  token=-1;
                }
            }
        catch(IOException ee)
            {
            }
     }
   else if(e.getSource()==停止广播)
     {
       time.stop();
     }
 }
                                         
 public static void main(String[] args) 
   {
      BroadCastWord broad=new BroadCastWord();
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值