/**
*
* 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(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。