无法从java

时间:2015-08-14 13:13:53

标签: 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.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();
        }
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;

public class UdpDateServer {
    private DatagramSocket socket = null;
    private boolean moreClients = true;

    public static void main(String[] args) {
        UdpDateServer server = new UdpDateServer();
        server.start();
    }

    public UdpDateServer() {
        try {
            socket = new DatagramSocket(4445);
            System.out.println("server ready...");
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void start() {
        DatagramPacket packet;
        while (moreClients) {
            try {
                byte[] buf = new byte[2048];
                // receive request
                packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                System.out.println("recieved packet from client: "+new String(packet.getData()));
                // prepare response
                String responseString = "Client's message received: "+new String(packet.getData())+"\n"
                        + "Received on:" +new Date().toString();

                buf = responseString.getBytes();
                System.out.println("buf to be sent from server: "+(new String(buf)));
                System.out.println("buf.length="+buf.length);
                // send the response to "address" and "port"
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
                moreClients = false;
            }
        }
        socket.close();
    }
}

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

客户方:

  

来自服务器:客户&#39>

服务器端:

  

服务器就绪......

     

收到来自客户的信息:hi's Max

     

从服务器发送的buf:客户收到的消息:嗨,它的最大

     

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

     

buf.length = 2116

正如您所看到的,客户端收到的来自服务器的消息将在11个字符后被删除。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

问题是客户端代码的行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();

答案 1 :(得分:1)

byte[] buf = new byte[2048];
buf="hi it's Max".getBytes();

此后buf.length将是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);