眾所周知,現在的網絡通訊通常使用TCP或是UDP協議,而TCP常常因為資源占用太大而在媒體數據傳輸時棄之不用,UDP成為了解決方案之一。在JXTA中,也有類似UDP協議的實現,通過構造JxtaMulticastSocket類來實現。該類與Socket類類似,不過構造方式有了很大的變化,后者通過IP和端口,而該類則通過管道廣告,這便具有了JXTA技術的特點,因為JXTA是采用虛擬管道通信,而管道資源的表現形式是廣告。以下是一段代碼,用來發送媒體數據的:
class SendMessageThread extends Thread {
public void run() {
System.out.println("SendMessageThread is run.");
int sendCount = 0;
File file = null;
try {
file = new File("E:\\TDdownload\\Movie\\23409484.wmv");
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buf = new byte[4096];
int i = 0;
try {
JxtaMulticastSocket tmpSocket = new JxtaMulticastSocket(pg,
pipeAdv);
System.out.println(pipeAdv);
while ((i = bis.read(buf)) != -1) {
sendCount++;
System.out.println(buf);
DatagramPacket dp = new DatagramPacket(buf, buf.length);
tmpSocket.send(dp);
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/