无为

无为则可为,无为则至深!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
/**
 *
 *  Broadcast.java
 *  一个基于UDP数据广播的局域网络会议程序
 * 
 *  作者:qyjohn@SMTH
 *
 */


import java.io.*;
import java.net.*;

public class Broadcast
{         
    DatagramSocket  Sender;
    MulticastSocket Receiver;
    InetAddress     BroadcastGroup;
    int             ServerPort, ClientPort;
    
    public Broadcast()
    {
        GetBroadcastPorts();
        try
        {
                BroadcastGroup = InetAddress.getByName("230.0.0.1");
                Sender         = new DatagramSocket(ServerPort);
                Receiver       = new MulticastSocket(ClientPort);
                Receiver.joinGroup(BroadcastGroup);
        } catch (Exception e) {}
        
    }
    
    public void SendData(String Msg)
    {
        byte[] b = new byte[1024];
        DatagramPacket packet;

        try
        {
            b = Msg.getBytes();
            packet = new DatagramPacket(b, b.length, BroadcastGroup, ClientPort);
            Sender.send(packet);
        } catch (Exception e) {}
    }
    
    
    
    public String ReceiveData()
    {
        byte[] b = new byte[1024];
        DatagramPacket packet = new DatagramPacket(b, 1024);
        String InMsg;
                
        try
        {
                Receiver.receive(packet);
                } catch (IOException e) {}
                
        b = packet.getData();
        InMsg = new String(b);
                return InMsg;
        
    }
    
    
    public void GetBroadcastPorts()
    {
        ServerPort = 1235;
        ClientPort = 1236;
    }
}


凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。

posted on 2005-12-14 13:22 草儿 阅读(148) 评论(0)  编辑  收藏 所属分类: Java编程经验谈

只有注册用户登录后才能发表评论。


网站导航: