Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在java中无法从UDP服务器接收正确的消息_Java_Sockets_Udp - Fatal编程技术网

Can';在java中无法从UDP服务器接收正确的消息

Can';在java中无法从UDP服务器接收正确的消息,java,sockets,udp,Java,Sockets,Udp,我正在尝试使用udp客户端向udp服务器发送消息,并将消息发送回客户端,其中包含有关接收消息的各种元数据。 我有两门课: import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostExceptio

我正在尝试使用udp客户端向udp服务器发送消息,并将消息发送回客户端,其中包含有关接收消息的各种元数据。 我有两门课:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpDateClient {
    public static void main(String[] args) throws IOException {
        String host = "localhost";
        if (args.length > 0)
            host = args[0];
        // get a datagram socket on any available port
        try {
            DatagramSocket socket = new DatagramSocket();
            // send request
            byte[] buf = new byte[2048];
            buf="hi it's Max".getBytes();
            InetAddress address = InetAddress.getByName(host);
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
            socket.send(packet);
            // get response
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            // display response
            buf = packet.getData();
            int len = packet.getLength();
            String received = (new String(buf)).substring(0, len);
            System.out.println("From server: " + received);
            socket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我在客户端得到的只是服务器发送的消息的前11个字节:

客户端:

来自服务器:客户端是我

服务器端:

服务器就绪

收到来自客户的数据包:嗨,我是Max

要从服务器发送的buf:接收到客户端消息:hi it's Max

收到日期:2015年8月14日星期五16:00:20 IDT

基本长度=2116

如您所见,客户端从服务器接收到的消息在11个字符后被剪切


我做错了什么?

问题在于行
buf=“hi it's Max.”getBytes()。
这里您将缓冲区长度设置为11。
您的客户端代码应该如下所示

            DatagramSocket socket = new DatagramSocket();
            // send request
            byte[] buf = "hi it's Max".getBytes();
            InetAddress address = InetAddress.getByName(host);
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
            socket.send(packet);
            // get response
            buf = new byte[2048];
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            // display response
            buf = packet.getData();
            int len = packet.getLength();
            String received = (new String(buf)).substring(0, len);
            System.out.println("From server: " + received);
            socket.close();
在此之后,
buf.长度将是11而不是2048

在你回来的路上

packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
将只接收11个字节,因为
buf.length
为11。将第一个块更改为

byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);