MulticastSocket.receive永远不会返回

时间:2013-01-31 22:43:19

标签: java multicast

我正在尝试使用java开发多播应用程序。这是我的代码:

这是Lamport.Java文件:

package dsass2;
import java.net.*;
import java.io.*;
public class Lamport
{
static int Current_Time;
static int Time_Interval;
static String Multicast_Group;
static int port;
static MulticastSocket s;

static public int SendMsg(byte[] sending)
{
    DatagramPacket pack;
    try{
        pack = new DatagramPacket(sending, sending.length,InetAddress.getByName(Multicast_Group), port);
    }
    catch(UnknownHostException uhe){
        System.out.println("UnknownHostException Occured in SendMsg! "+uhe);
        return 0;
    }
    try{
        s.send(pack);
    }
    catch(IOException io){
        System.out.println("IOException Occured in SendMsg! "+io);
    }
    catch(NullPointerException npe){
       System.out.println("You Must Initialize Lamport Logical Clock First! ");
       return 0;
    }
    return 1;
}

static public int RecvMsg(byte[] recived)
{
   DatagramPacket pack = new DatagramPacket(recived, recived.length);
   try{

       s.receive(pack);
   }
   catch(IOException io){
       System.out.println("IOException Ocurred in RecvMsg! " + io);
       return 0;
   }
   catch(NullPointerException npe){
       System.out.println("You Must Initialize Lamport Logical Clock First! ");
       return 0;
   }
   return 1;
}

static public int Initialize(String mcastip, int port, int timeinterval)
{
    try{
    s=new MulticastSocket(port);
    }
    catch(IOException io)
    {
        System.out.println("IOException Ocurred in Initialize! " + io);
    }
    try{
        s.joinGroup(InetAddress.getByName(mcastip));
    }
    catch(UnknownHostException uhe){
         System.out.println("UnknownHostException Ocurred in Initialize! " + uhe);
    }
    catch(IOException io){
        System.out.println("IOException Ocurred in Initialize! " + io);
    }
    Multicast_Group=mcastip;
    Time_Interval=timeinterval;
    Thread Logical_Clock = new Thread(Clock_Incrementer);
    Logical_Clock.start();
    return 1;//it means no error occured.
}

static public int GetCurrentClock()
{
    return Current_Time;
}

Lamport () //Cunstructor
{
    Current_Time=0;
}

static Runnable Clock_Incrementer=new Runnable()
{
    public void run()
    {
        while(true){
            Current_Time+=Time_Interval;
              try{
                  Thread.sleep(1000);
              }
              catch(InterruptedException ie){
                  System.out.println("Logical Clock thread interrupted! " + ie);
              }
        }
    }
};
}

这是我的main.java文件:

package dsass2;
import java.util.Scanner;

public class Main {
public static void main(String[] args){
    Scanner user_input=new Scanner(System.in);

    int voroodi=0;

    byte[] message=new byte[1024];

    while(voroodi!=5){

        System.out.println("What do you want to do?\n1. Initialize Lamport Logical Clock\n2. Send a message\n3. Recieve a message\n4. Get Current Clock\n5.Exit From Program");

        voroodi=Integer.parseInt(user_input.next());

        switch(voroodi){
            case 1:
                System.out.println("Please Enter Multicast Group IP:");
                String group=user_input.next();
                System.out.println("Please Enter Port Number:");
                int port=Integer.parseInt(user_input.next());
                System.out.println("Please Enter Time interval:");
                Lamport.Initialize(group, port, Integer.parseInt(user_input.next()));
                break;
            case 2:
                System.out.println("Please Enter Your Message:");
                String Msg=user_input.next();
                Lamport.SendMsg(Msg.getBytes());
                break;
            case 3:
                System.out.println("Please Wait for a Message...");
                if(Lamport.RecvMsg(message)==1)
                {
                    System.out.println("New Message Recieved:");
                    System.out.println(message.toString());
                }
                break;
            case 4:
                System.out.printf("Current Time is: %d\n", Lamport.GetCurrentClock());
                break;
            case 5:
                System.exit(0);
                break;
            default:
                System.exit(0);
                break;
        }
    }
    System.exit(0);
}
}

初始化后,当我尝试从一台计算机(ip:192.168.1.2)向Lan中的另一台计算机(IP:192.168.1.3)发送数据包时,消息发送成功,但是“s.receive(pack );” RecvMsg函数中的命令不返回,并永久阻止程序。 我在IP 192.168.1.3的计算机上使用过wireshark,并且我可以看到数据包已经转移到目标计算机了!,所以可能没有防火墙或者......问题。 有人可以帮忙吗?

0 个答案:

没有答案