2014-02-09 104 views
0

我已經編寫了以下兩個用於查找兩個數字的GCD的代碼。 (通過UDP服務器)在Java中尋找GCD,Socket編程

GCD_UDPClient.java

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

class GCD_UDPClient 
{ 
    public static void main(String args[]) throws Exception 
    { 
     BufferedReader InFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     DatagramSocket ClientSocket = new DatagramSocket(); 
     InetAddress IPAddress = InetAddress.getByName("localhost"); 
     byte[] SendData = new byte[1024]; 
     byte[] ReceiveData = new byte[1024]; 

     System.out.print("First Number: "); 
     String input1 = InFromUser.readLine(); 
     System.out.print("Second Number: "); 
     String input2 = InFromUser.readLine(); 
     String Input = input1 + ' ' +input2; 

     SendData = Input.getBytes(); 
     DatagramPacket SendPacket = new DatagramPacket(SendData, SendData.length, IPAddress, 9836); 
     ClientSocket.send(SendPacket); 
     DatagramPacket ReceivePacket = new DatagramPacket(ReceiveData, ReceiveData.length); 
     ClientSocket.receive(ReceivePacket); 
     String ModifiedInput = new String(ReceivePacket.getData()); 
     System.out.println("GCD From Server: " +ModifiedInput); 
     ClientSocket.close(); 
    } 
} 

GCD_UDPServer.java

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

@SuppressWarnings("unused") 
class GCD_UDPServer 
{ 
    @SuppressWarnings("resource") 
    public static void main(String args[]) throws Exception 
    { 
     DatagramSocket ServerSocket = new DatagramSocket(9836); 
     byte[] ReceiveData = new byte[1024]; 
     byte[] SendData = new byte[1024]; 
     while(true) 
     { 
      DatagramPacket ReceivePacket = new DatagramPacket(ReceiveData, ReceiveData.length); 
      ServerSocket.receive(ReceivePacket); 
      String input = new String(ReceivePacket.getData()); 
      InetAddress IPAddress = ReceivePacket.getAddress(); 
      int port = ReceivePacket.getPort(); 

      int ar[] = new int[2],i=0; 
      for (String Number: input.split(" ", 2)) 
      { 
       ar[i] = Integer.parseInt(Number); 
       i=i+1; 
      } 
      String Answer = Integer.toString(calculategcd(ar[0],ar[1])); 

      SendData = Answer.getBytes(); 
      DatagramPacket SendPacket = new DatagramPacket(SendData, SendData.length, IPAddress, port); 
      ServerSocket.send(SendPacket); 
     } 
    } 

    public static int calculategcd(int a, int b) 
    { 
     if(b%a == 0) 
      return a; 
     else 
      return calculategcd(b%a,a); 
    } 
} 

ClientSocket.receive(ReceivePacket);似乎沒有正常工作,任何線索,爲什麼?完整的代碼只是爲了清楚。由上述代碼給出

輸出:

First Number: 5 
Second Number: 25 
[waits indefinitely] 

輸出所需:

First Number: 5 
Second Number: 25 
GCD From Server: 5 
+1

任何錯誤訊息?怎麼了?你期望發生什麼? –

+0

@GuntramBlohm >>編輯上述代碼給出的輸出:'和'輸出必需:'。 –

+1

您是否在您的代碼中檢查了錯誤 >>> ar [i] = Integer.parseInt(Number); –

回答

1

您正在向服務器發送1024個字節,並且您將數據放入字符串「5 25」中。當你收到你分割的數據時,你將有「5」和「25」,其後是緩衝區中的其他字節。要麼使用Number.trim()來丟棄這些額外的字節,要麼發送一個較小的數據包(如果可能的話)。

+0

好的解釋! –

1

我有一個測試中,GCD_UDPServer.java有像

Exception in thread "main" java.lang.NumberFormatException: For input string: "12" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:492) 
    at java.lang.Integer.parseInt(Integer.java:527) 
    at GCD_UDPServer.main(GCD_UDPServer.java:24) 

和一個簡單的例外改線24

ar[i] = Integer.parseInt(Number); 

ar[i] = Integer.parseInt(Number.trim()); 

和它工作正常。