UDP Java,不要返回正确的答案

时间:2017-11-30 18:35:07

标签: java udp

我的UDP Java程序没有从Cases返回正确的句子。 程序应该为4个不同的问题返回4个不同的答案。 但只有我收到的答案是默认答案“答案未找到" 我认为当服务器尝试将“句子”传递给案例方法时存在一些问题 我在代码中遗漏了什么吗?

SERVER类

package pac1;

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

public class Server {

    public static void main(String args[]) throws Exception{
        System.out.println("UDP Server: Started");
        DatagramSocket serverSocket =new DatagramSocket(9878);
        byte[]  receiveData = new byte[1024];
        byte[]  sendData = new byte[1024];
        while (true){
            DatagramPacket  receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String(receivePacket.getData());
            System.out.println("UDP Server Received: "+sentence);
            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();
            String out;
            out=getAnswer(sentence);

            System.out.println(sentence);
            sendData= out.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData,
            sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
            System.out.println("UDP Server Sending to Client: "+out);
            }

        }
    private static String getAnswer(String sentence) {
        String out;
        switch(sentence) {  
        case "A" : out =  "65";
        break;
        case "B" : out =  "66";
        break;
        case "C" : out =  "67";
        break;
        case "D" : out =  "68";
        break;

        default: out = "Answer not found";
        break;

        } 
        return out;
    }
    }

CLIENT类

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

public class Client {
    public static void main(String args[]) throws Exception{
        System.out.println("UDP Client: Started");

         String url = "rmi:///";
            System.out.println("Enter Question: ");
            Scanner myinput = new Scanner(System.in);
            String sentence = myinput.nextLine();

        DatagramSocket clientSocket =new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[]  sendData = new byte[1024];
        byte[]  receiveData = new byte[1024];
        sendData =sentence.getBytes();
        DatagramPacket  sendPacket = new DatagramPacket(sendData,
                sendData.length, IPAddress, 9878);
        System.out.println("UDP Client: Sending "+ sentence);
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData,
                receiveData.length);
        clientSocket.receive(receivePacket);
        String modifiedSentence = new String(receivePacket.getData());
        System.out.println("UDP Client: Received From Server; "+ modifiedSentence);
        clientSocket.close();
    }


    }

1 个答案:

答案 0 :(得分:0)

您遇到此问题的原因可能是您在服务器类中创建字符串的方式。如果初始化数据包以接收1024字节的数据,并且发送的字节数少于1024字节,则数组的其余部分将填充零。这会导致您的字符串在末尾有空格。这个问题的最简单的解决方案可能是调用:

sentence.trim();

将收到的数据转换为服务器端的字符串后。