2014-10-30 52 views
2

我想使三臺服務器使用java UDP進行通信。如果在第一個服務器實例上調用了所有三個服務器的特定方法,則其他兩個服務器也應該使用UDP將這些方法的結果傳遞給第一個服務器。我應該使用多播嗎?我試過使用單播,但它沒有工作。UDP單播使用java的三臺服務器之間的通信

UDPDataExchangeclass

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(); 
      } 

    } 


} 

UDPTest

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的端口以發送數據。一旦他們收到它,他們發送迴應給康科迪亞

+0

請發佈代碼,看看發生了什麼。 – rafalopez79 2014-10-30 21:30:20

+0

單播應該工作。你能發佈錯誤還是問題是什麼?他們不能達到對方或什麼? – ldmtwo 2014-10-30 23:30:47

+0

@ldmtwo:他們無法到達其他第一臺服務器啓動,並在那一點執行被暫停 – Aayush 2014-10-30 23:34:14

回答

2

試試這個,我不確定它是否正是你想要的。

UDPDataExchange

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(); 
    } 

} 

} 

UDPTest

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); 

} 

}

I'll解釋brielfy我改變了:

UDPDataExchange: 我改變了IPAdress到InetAddress.getLocalHost( )

UDPTest: 每個對responseData的調用都在單獨的線程中運行,以避免阻塞主線程。 該線程是守護進程,允許退出應用程序。 requestData()調用已更改爲調用每個服務器。

希望有用。

+0

感謝您抽出時間和幫助我這正是我正在尋找。 – Aayush 2014-10-31 17:34:05

相關問題