使用java的三台服务器之间的UDP单播通信

Posted

技术标签:

【中文标题】使用java的三台服务器之间的UDP单播通信【英文标题】:UDP Unicast Communication between three servers using java 【发布时间】:2014-10-30 20:45:36 【问题描述】:

我想让三台服务器使用 java UDP 进行通信。如果在第一个服务器实例上调用所有三个服务器都存在的特定方法,那么其他两个服务器也应该使用 UDP 将这些方法的结果传达给第一个服务器。我应该使用多播吗?我尝试使用单播,但没有成功。

UDPDataExchange类

import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class UDPDataExchange 

    String              Hostname;
    static InetAddress  IPAddress;

    UDPDataExchange() 
        Hostname = new String("127.0.0.1");
        try 
            IPAddress = InetAddress.getByName(Hostname);
        
        catch (UnknownHostException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

    public void requestData(int portNumber, String data) 
        try 
            DatagramSocket clientSocket = new DatagramSocket();
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);
            byte[] sendData = new byte[1024];
            sendData = data.getBytes();
            System.out.print("Ready to send data ");
            DatagramPacket sendPacket = new DatagramPacket(sendData,
                    sendData.length, IPAddress, portNumber);
            clientSocket.send(sendPacket);
            clientSocket.setSoTimeout(10000);
            try 
                clientSocket.receive(receivePacket);
                String modifiedSentence = new String(receivePacket.getData());

                InetAddress returnIPAddress = receivePacket.getAddress();

                int port = receivePacket.getPort();

                System.out.println("From server at: " + returnIPAddress + ":"
                        + port);
                System.out.println("Message: " + modifiedSentence);

            
            catch (SocketTimeoutException ste) 
                System.out.println("Timeout Occurred: Packet assumed lost");
            
            //clientSocket.close();

        
        catch (SocketException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        

    

    public void responseData(int portNumber) 
            try 

                DatagramSocket serverSocket = new DatagramSocket(portNumber);
                byte[] receiveData = new byte[1024];
                byte[] sendData = new byte[1024];

                while (true) 

                    receiveData = new byte[1024];

                    DatagramPacket receivePacket = new DatagramPacket(receiveData,
                            receiveData.length);

                    System.out.println("Waiting for datagram packet");

                    serverSocket.receive(receivePacket);

                    String sentence = new String(receivePacket.getData());

                    InetAddress IPAddress = receivePacket.getAddress();

                    int port = receivePacket.getPort();

                    System.out.println("From: " + IPAddress + ":" + port);
                    System.out.println("Message: " + sentence);

                    String capitalizedSentence = sentence.toUpperCase();

                    sendData = capitalizedSentence.getBytes();

                    DatagramPacket sendPacket = new DatagramPacket(sendData,
                            sendData.length, IPAddress, port);

                    serverSocket.send(sendPacket);

                

            
            catch (SocketException ex) 
                System.out.println("UDP Port 9876 is occupied.");
                System.exit(1);
            
            catch (IOException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            

    



UDP测试

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

class UDPTest extends UDPDataExchange 

    public static void main(String args[]) throws Exception 
        int UDPClientConcordiaPortNumber = 9876;
        int UDPClientMcgillPortNumber=9890;
        int UDPClientDawsonPortNumber=9891;
        UDPTest McgillServer=new UDPTest();
        UDPTest DawsonServer=new UDPTest();
        McgillServer.responseData(UDPClientConcordiaPortNumber);
        DawsonServer.responseData(UDPClientDawsonPortNumber);

        UDPTest concordiaTest = new UDPTest();

        System.out.println("Attemping to connect to " + IPAddress
                + ") via UDP port" + UDPClientConcordiaPortNumber);

        String concordiaData = "Hello from concordia";
        System.out.println("Sending data  " + concordiaData.length()
                + " bytes to server.");

        concordiaTest.requestData(UDPClientConcordiaPortNumber, concordiaData);

    

在 UDPDataExchange 中,我定义了两种发送和接收数据的方法。在 UDPTest 类中,我试图创建两个服务器实例,即 mcgill 和 dawson 服务器,它们将侦听 concordia 的端口以发送数据。一旦他们收到它,他们就会将响应发送回 Concordia

【问题讨论】:

请发布代码以查看发生了什么。 单播应该可以工作。您可以发布错误或问题到底是什么?他们无法联系到对方还是什么? @ldmtwo : 他们无法在第一台服务器启动时访问其他服务器,此时执行被暂停 【参考方案1】:

试试这个,我不确定它是否正是你想要的。

UDP数据交换

public class UDPDataExchange 

String Hostname;
static InetAddress IPAddress;

UDPDataExchange() 
    Hostname = new String("127.0.0.1");
    try 
        IPAddress = InetAddress.getLocalHost();
     catch (final UnknownHostException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    


public void requestData(final int portNumber, final String data) 
    try 
        final DatagramSocket clientSocket = new DatagramSocket();
        final byte[] receiveData = new byte[1024];
        final DatagramPacket receivePacket = new DatagramPacket(receiveData,
                receiveData.length);
        byte[] sendData = new byte[1024];
        sendData = data.getBytes();
        System.out.print("Ready to send data ");
        final DatagramPacket sendPacket = new DatagramPacket(sendData,
                sendData.length, IPAddress, portNumber);
        clientSocket.send(sendPacket);
        clientSocket.setSoTimeout(10000);
        try 
            clientSocket.receive(receivePacket);
            final String modifiedSentence = new String(receivePacket.getData());

            final InetAddress returnIPAddress = receivePacket.getAddress();

            final int port = receivePacket.getPort();

            System.out.println("From server at: " + returnIPAddress + ":"
                    + port);
            System.out.println("Message: " + modifiedSentence);

         catch (final SocketTimeoutException ste) 
            System.out.println("Timeout Occurred: Packet assumed lost");
        
        // clientSocket.close();

     catch (final SocketException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
     catch (final IOException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    



public void responseData(final int portNumber) 
    try 

        final DatagramSocket serverSocket = new DatagramSocket(portNumber, IPAddress);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];

        while (true) 

            receiveData = new byte[1024];

            final DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);

            System.out.println("Waiting for datagram packet");

            serverSocket.receive(receivePacket);

            final String sentence = new String(receivePacket.getData());

            final InetAddress IPAddress = receivePacket.getAddress();

            final int port = receivePacket.getPort();

            System.out.println("From: " + IPAddress + ":" + port);
            System.out.println("Message: " + sentence);

            final String capitalizedSentence = sentence.toUpperCase();

            sendData = capitalizedSentence.getBytes();

            final DatagramPacket sendPacket = new DatagramPacket(sendData,
                    sendData.length, IPAddress, port);

            serverSocket.send(sendPacket);

        

     catch (final SocketException ex) 
        System.out.println("UDP Port 9876 is occupied.");
        System.exit(1);
     catch (final IOException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    




UDP测试

class UDPTest extends UDPDataExchange 

public static void main(final String args[]) throws Exception 
    final int UDPClientConcordiaPortNumber = 9876;
    final int UDPClientMcgillPortNumber = 9890;
    final int UDPClientDawsonPortNumber = 9891;
    final UDPTest McgillServer = new UDPTest();
    final UDPTest DawsonServer = new UDPTest();
    final Thread t1 = new Thread() 
        @Override
        public void run() 
            McgillServer.responseData(UDPClientMcgillPortNumber);
        
    ;
    t1.setDaemon(true);
    t1.start();
    final Thread t2 = new Thread() 
        @Override
        public void run() 
            DawsonServer.responseData(UDPClientDawsonPortNumber);
        
    ;
    t2.setDaemon(true);
    t2.start();

    final UDPTest concordiaTest = new UDPTest();

    System.out.println("Attemping to connect to " + IPAddress
            + ") via UDP port" + UDPClientConcordiaPortNumber);

    final String concordiaData = "Hello from concordia";
    System.out.println("Sending data  " + concordiaData.length()
            + " bytes to server.");

    concordiaTest.requestData(UDPClientMcgillPortNumber, concordiaData);
    concordiaTest.requestData(UDPClientDawsonPortNumber, concordiaData);


我将简要解释一下我所做的更改:

UDP 数据交换: 我将 IPAdress 更改为 InetAddress.getLocalHost()

UDP测试: 每个对 responseData 的调用都在一个单独的线程中运行,以避免阻塞主线程。 该线程是允许退出应用程序的守护进程。 requestData() 调用已更改为调用每个服务器。

希望有用。

【讨论】:

感谢您抽出时间帮助我,这正是我想要的。

以上是关于使用java的三台服务器之间的UDP单播通信的主要内容,如果未能解决你的问题,请参考以下文章

Geode 通信机制

Linux(程序设计):42---UDP网络编程(单播)

网络通信中组播和多播的联系,区别分别是啥?

Qt网络编程之搭建Udp通信单播组播广播

Java UDP通信小程序到UDP服务器

QT之UDP通信