📄 example1205_multicastclient.java
字号:
import java.net.*;
public class Example1205_MulticastClient implements Runnable
{
int port = 6789;
String host = "228.5.6.7";
InetAddress group = null;
MulticastSocket socket = null;
Thread thread;
public Example1205_MulticastClient()
{
try
{
group=InetAddress.getByName(host);
socket=new MulticastSocket(port);
socket.joinGroup(group);
thread = new Thread(this);
thread.start();
}
catch (Exception e)
{
System.out.println("Error:"+e);
}
}
public void run()
{
try
{
DatagramPacket packet = null;
byte data[] = new byte[1024];
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
System.out.println("received: " + new String(packet.getData(), 0, packet.getLength()) + " from " + packet.getSocketAddress());
socket.leaveGroup(group);
}
catch (Exception e)
{
System.out.println("Error:" + e);
}
}
public static void main(String[] args)
{
Example1205_MulticastClient mc = new Example1205_MulticastClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -