日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java多播_Java多播发送数据,未接收

發布時間:2024/10/14 java 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多播_Java多播发送数据,未接收 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在用

Java編寫一個類,用于大大簡化多播過程.但是,我遇到了兩個大問題:

>該類發送數據(我可以使用我的網絡監視器Wireshark進行驗證),但同一組中的任何其他人都不會收到數據.

>在某些機器上,傳輸過程中超出了發送數據包TTL(同樣,根據Wireshark).

有人可以幫幫我嗎?我一直在努力尋找幾小時的答案,看來我的代碼遵循了從多播主機連接,加入,發送和接收數據的所有基本過程.

以下是該課程相關部分的片段:

Multicaster類:

public class Multicaster {

public int port = 5540;

protected String IPAddress;

private MulticastSocket msConn;

private InetAddress netAddr;

public Multicaster(String IPAddress) {

this.IPAddress = IPAddress;

}

public String recieveData() {

byte[] buf = new byte[1000];

DatagramPacket pack = new DatagramPacket(buf, buf.length);

try {

this.msConn.receive(pack);

new Message(pack);

String out = new String(pack.getData());

return out.substring(0, pack.getLength());

} catch (IOException e) {

return new String("");

}

}

public void joinGroup() {

try {

this.msConn.joinGroup(this.netAddr);

} catch (IOException e) {

//This error shouldn't occur since it would caught by the connect() method during initial connection to the host

}

}

public void connect() throws MulticasterInitException {

//Try to create a multicast connection on the given IP address and port

try {

try {

//Create a multicast connection on a given port, throws UnknownHostException

this.msConn = new MulticastSocket(this.port);

//If all goes well, then create a connection to a given IP address using the above port number, throws IOException and SecurityException

this.netAddr = InetAddress.getByName(this.IPAddress);

}

}

/**

* Here all of the possible exceptions that are thrown above

* are caught and handled here. This works just fine.

*/

}

public void sendData(String data) throws MulticasterSendException {

DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), this.netAddr, this.port);

try {

this.msConn.send(packet);

} catch (IOException e) {

throw new MulticasterSendException("Java could not communicate with the server. Please check your network connections.", e);

}

}

}

發送數據的示例用法:

Multicaster multicast = new Multicaster("239.0.0.0");

try {

multicast.connect();

} catch (MulticasterInitException e) {

//Handle exception...

}

multicast.joinGroup();

try {

multicast.sendData("Hi");

} catch (MulticasterSendException e) {

//Handle exception...

}

用于接收數據的示例用法:

Multicaster multicast = new Multicaster("239.0.0.0");

try {

multicast.connect();

} catch (MulticasterInitException e) {

//Handle exception...

}

multicast.joinGroup();

System.out.print(multicast.recieveData());

總結

以上是生活随笔為你收集整理的java多播_Java多播发送数据,未接收的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。