?? multsocketclient.java
字號:
// multSocketClient.java
import java.io.*;
import java.net.*;
class multSocketClient {
public static void main(String[] args) throws IOException {
//在端口12345上創(chuàng)建MulticastSocket對象,接收多播數(shù)據(jù)包
MulticastSocket ms = new MulticastSocket(12345);
InetAddress group = InetAddress.getByName("224.0.0.1");
ms.joinGroup(group);//加入多播組
for (int i = 0; i < 5; i++) {//限制循環(huán)次數(shù),只接收5次
byte[] buf1 = new byte[256];
//對象不需要地址信息,因?yàn)樘捉幼忠寻刂? DatagramPacket dgp = new DatagramPacket(buf1, buf1.length);
ms.receive(dgp);//接收數(shù)據(jù)包
byte[] buf2 = new byte[dgp.getLength()];
//將數(shù)據(jù)復(fù)制到字節(jié)數(shù)組中并輸出到屏幕
System.arraycopy(dgp.getData(), 0, buf2, 0, dgp.getLength());
System.out.println(new String(buf2));
}
ms.leaveGroup(group);//退出多播組
ms.close();//關(guān)閉套接字
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -