Java UDP多播

时间:2014-03-02 10:02:30

标签: java sockets multicast

我有一个简单的单播客户端/服务器程序,与客户端一起询问服务器和服务器应答的内容。 现在我想做一个变体,但是添加Multicast。在此阶段,客户可以加入群组并收到如下确认:

hostAddr is /192.168.56.1 and PORT NUMBER IS: 54767

在服务器端,我期待来自客户端的软件包,但我从来没有得到它。

服务器代码:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.util.HashMap;

public class MulticastServer
{
public static MulticastSocket socket;

final static InetSocketAddress d = new InetSocketAddress("239.1.1.3", 4444);

public static void runNotifier(String m_addr, int m_port)
{
    //final InetSocketAddress d = new InetSocketAddress(m_addr, m_port);
    (new Thread()
    {
        public void run()
        {
            while(true)
            {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                byte[] hello = ("hello").getBytes();
                DatagramPacket h = new DatagramPacket(hello, hello.length, d.getAddress(), d.getPort());
                //System.out.println("ADDR: " + d.getAddress() + " PORT: " + d.getPort());
                try {
                    //System.out.println("SENDING");
                    socket.send(h);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    ).start();

}

public static void initSocket(int port) throws IOException
{
    socket = new MulticastSocket(); //port
    socket.setTimeToLive(1);
}

public static void main(String args[]) throws Exception
{
    initSocket(4444);
    runNotifier("239.1.1.3", 4444);
    HashMap<String, String> cache = new HashMap<String, String>();
    DatagramSocket serverSocket = new DatagramSocket(4444);  

    byte[] receiveData = new byte[1024]; //buffers           
    byte[] sendData = new byte[1024];  
    System.out.println("BEFORE SERVER WHILE");
    while(true)
    {   
        System.out.println("INSIDE SERVER WHILE");
       DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);                   
       serverSocket.receive(receivePacket);
       System.out.println("RECEIVED FROM CLIENT");
       //recebe string do cliente
       String sentence = new String(receivePacket.getData(),0, receivePacket.getLength());
       /*
       String[] parts = sentence.split(" "); 

       if (sentence.contains(" ")) {                       
           cache.put(parts[1], parts[2]);
        } else {
           System.out.println("SERVER can't do anything with this."); 
        }

       //String name = cache.get(parts[1]);
       */
       System.out.println("CLIENT SAID: " + sentence);
       //System.out.println("THE NAME IS: " + name);

       InetAddress IPAddress = receivePacket.getAddress();                   
       int port = receivePacket.getPort();                   
       String capitalizedSentence = sentence.toUpperCase();                   
       sendData = capitalizedSentence.getBytes();                  
       DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);                   
       serverSocket.send(sendPacket);
       receiveData = null;
       }
}
}

客户代码

import java.io.*;
import java.net.*;
import java.util.*;

public class MulticastClient {

public static  MulticastSocket socket = null;

public static void main(String[] args) throws IOException
{

    socket = new MulticastSocket(4444); //multicast port
    InetAddress address = InetAddress.getByName("239.1.1.3"); //multicast address
    socket.joinGroup(address);
    System.out.println("JOINED GROUP");
    //recebe o hello
    byte[] a = new byte[1024];
    DatagramPacket p = new DatagramPacket(a, a.length);
    socket.receive(p);
    System.out.println("RECEIVED PACKAGE");
    InetAddress hostAddr = p.getAddress();
    int portNumber = p.getPort();
    System.out.println("hostAddr is " + hostAddr + " and PORT NUMBER IS: " + portNumber);
    //

    HashMap<String, String> cache = new HashMap<String, String>();
    //DatagramSocket serverSocket = new DatagramSocket(p.getPort());            

    byte[] receiveData = new byte[1024]; //buffers           
    byte[] sendData = new byte[1024];    

   /* DatagramPacket packet;
    byte[] buf = new byte[256];
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);*/

    //escrita consola
    BufferedReader inFromUser =  new BufferedReader(new InputStreamReader(System.in)); 
    String sentence = inFromUser.readLine();      
    sendData = sentence.getBytes(); 

    DatagramSocket clientSocket = new DatagramSocket();       

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, hostAddr, portNumber);      
    clientSocket.send(sendPacket);       
    System.out.println("SENT TO SERVER");
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);       
    clientSocket.receive(receivePacket);       

    String modifiedSentence = new String(receivePacket.getData());       
    System.out.println("SERVER: " + modifiedSentence);       
    clientSocket.close();        

socket.leaveGroup(address);
socket.close();
}

}

到目前为止,我最好的猜测是在服务器的第53行

DatagramSocket serverSocket = new DatagramSocket(4444); 

我使用的端口号不正确。如果客户端收到端口54767,则它将发送到该端口。但即使假设这是问题,我也不知道如何修复它,因为每次运行它时,客户端都会获得一个不同的端口。

我遇到的另一个问题是,由于错误

,我似乎无法一次运行两个客户端
Exception in thread "main" java.net.SocketException: Unrecognized Windows Sockets error: 0: Cannot bind

在客户端的第12行

socket = new MulticastSocket(4444); //multicast port

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

1-您可以在服务器端创建MulticastSocket以接收消息 2-我认为你不需要DatagramSocket与gorup中的其他人通信,因为MulticastSocket适用于客户端和服务器。与第12行相关的绑定错误。