由于某些原因,服务器响应为空?

时间:2019-04-09 21:45:49

标签: java networking udp

有一项作业来创建客户端代码,以通过UDP发送分成两个变量的字符串。服务器通过将两个拆分变量发送回前端来接收并确认。 由于某种原因,我在运行它时的响应为空,不确定为什么。 有人可以帮忙吗?

当我尝试在另一个代码上使用整数时,它可以工作,但是当我尝试执行字符串操作时,什么也没回来

客户代码:

import java.io.*;
import java.net.*;
import java.util.Scanner;
public class UDPClient
{
    public static void main (String[] args) throws Exception
    {
        DatagramSocket client = new DatagramSocket();
        InetAddress IPAd = InetAddress.getByName("localhost");
        byte [] sendData1 = new byte[1024];
        byte [] sendData2 = new byte[1024];
        byte [] receiveData = new byte [1024];
        String input = "";
        Scanner in = new Scanner (System.in);
        System.out.println("Enter word: ");
        input = in.nextLine();
        String [] splitWord = input.split("");
        int length = splitWord.length;
        int evenSplit = 0;
        int oddSplit = 0;
        String firstHalf = "";
        String secondHalf  = "";

        if (length%2==0)
        {
            evenSplit = length / 2;
            for (int i=0; i<evenSplit; i++)
                firstHalf += splitWord[i];

            for (int i=evenSplit; i<length;i++)
                secondHalf += splitWord[i];
            }
        else{
            oddSplit = (length+1)/2;
            for (int i=0; i<oddSplit;i++)
                firstHalf += splitWord[i];
            for (int i=oddSplit; i<length;i++)
            secondHalf += splitWord[i];
        }

        sendData1 = firstHalf.getBytes();
        sendData2 = secondHalf.getBytes();
        DatagramPacket sendPacket1 = new DatagramPacket (sendData1, 
sendData1.length, IPAd, 2000);
        DatagramPacket sendPacket2 = new DatagramPacket (sendData2, 
sendData2.length, IPAd, 2000);
        client.send(sendPacket1);
        client.send(sendPacket2);
        DatagramPacket receivePacket = new DatagramPacket (receiveData, 
receiveData.length);
        String serverReply = new String (receivePacket.getData());
        System.out.println ("From server: "  + serverReply);
        client.close();
    }

}

服务器代码:

import java.io.*;
import java.net.*;
public class UDPServer
{
    public static void main (String [] args) throws Exception
    {
        DatagramSocket server = new DatagramSocket (2000);
        byte [] receiveData1 = new byte[1024];
        byte [] receiveData2 = new byte[1024];
        byte [] sendData = new byte [1024];

        while (true)
        {
            DatagramPacket receivePacket1 = new DatagramPacket 
(receiveData1, receiveData1.length);
            server.receive(receivePacket1);
            DatagramPacket receivePacket2 = new DatagramPacket 
(receiveData2, receiveData2.length);
            server.receive(receivePacket2);
            String firstHalfWord = new String (receivePacket1.getData());
            String secondHalfWord = new String (receivePacket2.getData());
            InetAddress IPAd = receivePacket1.getAddress();
            int port = receivePacket1.getPort();
            String response = secondHalfWord + firstHalfWord;
            sendData = response.getBytes();
            DatagramPacket sendPacket = new DatagramPacket (sendData, 
sendData.length, IPAd, port);
            server.send(sendPacket);



        }


    }
}

如果我打招呼,是用lohel回复(hel + lo回到最前面) 它只是返回“来自服务器:”而没有任何字符串响应

1 个答案:

答案 0 :(得分:2)

String serverReply = new String(receivePacket.getData());行之前,您需要说client.receive(receivePacket);