在Java中查找GCD,套接字编程

时间:2014-02-09 14:41:42

标签: java sockets udp

我写了以下两个代码来查找两个数字的GCD。 (通过UDP服务器)

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

2 个答案:

答案 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());

它运作正常。

答案 1 :(得分:1)

您正在向服务器发送1024个字节,并将数据放入“5 25”之类的字符串中。当您收到数据时,将其拆分,您将获得“5”,“25”后跟缓冲区中的其他字节。要么使用Number.trim()丢弃那些额外的字节,要么发送一个较小的数据包(如果可能的话)。