![](/img/trans.png)
[英]Multicast socket server not able to receive from a particular multicast on android mobile
[英]Making a Multicast Socket accessible from another Thread
现在,我有一台向多播组发送UDP消息的设备。 我编写了一个小型Java程序,可以通过加入该组并查看正确的端口来检测这些数据包。 我使用MulticastSocket.receive(packet);
命令。 为此,我将继续使用GUI编写程序。 我希望用户能够指定一个时间量,并且让我的程序在该时间量内查找数据包。 我已经做了大量研究,发现这样做的最好方法是在端口阻塞时切断接收命令。 为此,我让程序打开另一个线程,并在我的主线程睡眠指定时间的同时使用新线程监视UDP数据包。 它正在检测数据包就好了。 但是,我似乎无法从主线程访问它以关闭端口。 这是我的代码:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class MulticastClient_v2 extends Thread
{
public volatile MulticastSocket socket;
public void run()
{
try {
//Declare the port and IGMP Group IP
MulticastSocket socket2 = new MulticastSocket(5000);
InetAddress address = InetAddress.getByName("224.0.1.2");
//Display connection information
System.out.println("Joining 224.0.1.2 on port 5000");
//Join the Multicast Group
socket.joinGroup(address);
//Declaring a DatagramPacket
DatagramPacket packet;
//Starting an infinite loop
//while (true)
//{
System.out.println("Waiting on packets..");
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length); //Declaring an internal DatagramPacket
socket.receive(packet); //Receiving a Packet
String received = new String(packet.getData(), 0, packet.getLength());
InetAddress senderAddress = packet.getAddress(); //Get the InetAddress object
String forFun = senderAddress.getHostAddress(); //Extract the IP address of sender in text format
if (received.indexOf("Product1") >= 0) //Searching the raw data for "Product1"
{
//If found, display the IP and device type
System.out.println("Product1 found at " + senderAddress);
}
if (received.indexOf("Product2") >= 0) //Searching the raw data for "Product2"
{
//If found, display the IP and device type
System.out.println("Product2 found at " + senderAddress);
}
//}
}
catch(IOException ex)
{
System.out.println (ex.toString());
}
}
public static void main(String[] args)
{
MulticastClient_v2 thread = new MulticastClient_v2();
thread.start();
try {
Thread.sleep( 3000 );
thread.socket2.close();
}
catch(InterruptedException in)
{
System.out.println("Interrupted Exception!");
}
System.out.println("Done.");
}
}
因此,当我尝试编译时,出现以下错误:
MulticastClient_v2.java:63: error: cannot find symbol
thread.socket2.close();
^
symbol: variable socket2
在我看来,main方法无法在其他方法中使用socket2。 我的问题是如何让我看到它? 我对
public volatile MulticastSocket socket;
并且main方法可以访问它,但是当我在run方法中时,我无法连接到某个端口。 我唯一可以找到的方法就是bind()。 但是bind()需要IP和端口,而当我第一次声明一个多播套接字时,我可以像这样声明端口:
MulticastSocket socket2 = new MulticastSocket(5000);
任何帮助将不胜感激! 我已经被卡住了一段时间了。
编辑:我有一些建议。 首先,我应该在类级别进行声明和初始化,这给了我以下IO错误:
MulticastClient_v2.java:8: error: unreported exception IOException; must be caught
or declared to be thrown
public volatile MulticastSocket socket = new MulticastSocket(5000);
^
因此,接下来,我尝试将其放在类级别的try..catch块中,得到以下信息:
MulticastClient_v2.java:8: error: illegal start of type
try{
^
因此,我想我真正需要做的是在类级别初始化Multicast Port,然后将其放入方法内部的try块中,如JTMon所建议的那样。 但是我想不办法在初始化过程中不选择端口就选择端口。
编辑2:我仍然有困难。 如果我在类级别尝试像这样初始化它:
public volatile MulticastSocket socket;
以后如何在run()方法中编辑其端口?
socket2
是一个局部变量,因此它的作用域只是在其中定义的try
块。 使用MulticastClient_v2
实例,您只能访问该类的字段。
socket2是否在run方法中声明为局部变量? 无法从该方法之外的任何位置访问它。 尝试先在类级别上声明它,然后看看会发生什么。
我将尝试在此处放置一些代码来解释我的确切含义。 在类级别声明变量socket2。
MulticastClient_v2类应具有以下类型的公共构造函数:
public MulticastClient_v2(int portNumber){
try{
socket2 = new MulticastSocket(portNumber);
}catch(IOException e){
//Do something with exception here
}
}
如果端口号不变,则可以对其进行硬编码,但是这种方式更加灵活。 现在,在run方法中,您可以使用初始化的套接字,并且仍然可以从类外部访问它。 尽管为了记录,我希望您通过线程上的另一个方法进行访问,但这可能是对另一个线程的讨论;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.