众所周知,现在的网络通讯通常使用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);
}
文章来源于领测软件测试网 https://www.ltesting.net/