用Java清除数据报缓冲区 [英] Clear Datagram Buffer in Java

查看:202
本文介绍了用Java清除数据报缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从互联网上获得了这个简单的udp客户端/服务器代码,并且它有效。但是,当我输入比我之前输入的东西短的东西时,我会剩下剩下的字符。例如,如果我先输入:

So I got this simple udp client/server code from the internet, and it works. However, when I enter something that is shorter than the thing I entered before it, I get the remaining characters left over. For example, if I first enter:

kitty

然后输入:

cat

第二个印刷品读作:

catty

我一直在寻找其他有类似问题的人,他们中的大多数似乎都被解决了清除字节数组。但是,如果我尝试实现他们的答案,它不能解决我的问题。我需要做什么,并且(可能更重要的是)代码应该去哪里?以下是代码:

I've been looking at other people with similar problems and most of them seem to be solved by clearing the byte array. However, if I try to implement their answers, it doesn't fix my problem. What do I need to do, and (maybe more importantly) where in the code should it go? Here is the code:

客户端:

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

class 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];
       String sentence = inFromUser.readLine();
       sendData = sentence.getBytes();
       DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 20700);
       clientSocket.send(sendPacket);
       DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
       clientSocket.receive(receivePacket);
       String modifiedSentence = new String(receivePacket.getData());
       System.out.println("FROM SERVER:" + modifiedSentence);
       clientSocket.close();
    }
 }

服务器:

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

class UDPServer
 {
    public static void main(String args[]) throws Exception
       {
          DatagramSocket serverSocket = new DatagramSocket(21200);
          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(), 0, receivePacket.getLength());
                   System.out.println("RECEIVED: " + sentence);
                   InetAddress IPAddress = receivePacket.getAddress();
                   int port = receivePacket.getPort();
                   String capitalizedSentence = sentence.toUpperCase();
                   sendData = capitalizedSentence.getBytes();
                   DatagramPacket sendPacket =  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                   serverSocket.send(sendPacket);
                }
       }
 }


推荐答案

您不需要清除字节数组,但是您需要在接收后通过 getLength()方法;例如:

You don't need to clear the byte array, but you do need to take some notice of the length of the DatagramPacket after the receive, via the getLength() method; for example:

new String(packet.getData(), packet.getOffset(), packet.getLength());

您在服务器中正确执行此操作,但在客户端中没有。

You're doing that correctly in the server, but not in the client.

您还需要在调用 receive()之前重置长度。否则 DatagramPacket 将继续缩小到目前为止收到的最小数据包的长度。

You also need to reset the length before calling receive(). Otherwise the DatagramPacket will keep shrinking to the length of the smallest packet received so far.

这篇关于用Java清除数据报缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆