简体   繁体   English

DataGram Client-Server Java-消息传输问题

[英]DataGram Client-Server Java - message transfer issue

Hy, I have to do a client-server DataGram communication and I had a little problem. 嗨,我必须进行客户端-服务器DataGram通信,但遇到了一些问题。 The send-recive methods are working good, but if I try to send the secound time from client, the server will not recive entire message again: send-recive方法运行良好,但是如果我尝试从客户端发送秒数,服务器将不会再次接收整个消息:

Server Started and listening to the port 10000
Recive from client: Send me a datagram
Send to client: I am Server!

Recive from client: Send me a da      -> HERE is the PROBLEM
Send to client: I am Server!

In TCP transfer I kwnow that I have to flush the buffer, but in DataGram what I have to do? 在TCP传输中,我知道必须刷新缓冲区,但是在DataGram中我必须做什么?

DGSServerT: DGSServerT:

public class DGSServerT {
    public static void main (String [] args) throws IOException {
        final int port = 10000;

        // Create a datagram socket bound to port 10000. Datagram packets sent from client programs arrive at this port.
        DatagramSocket s = new DatagramSocket (port);
        System.out.println("Server Started and listening to the port " + port);

        // Create a byte array to hold data contents of datagram packet.
        byte [] data = new byte [100];

        // Create a DatagramPacket object that encapsulates a reference to the byte array and destination address information. The
        // DatagramPacket object is not initialized to an address because it obtains that address from the client program.
        DatagramPacket dgp = new DatagramPacket (data, data.length);

        // Enter an infinite loop. Press Ctrl+C to terminate program.
        while (true) {
            // Receive a datagram packet from the client program.
            s.receive (dgp);
            // Display contents of datagram packet.
            System.out.println ("Recive from client: " + new String (data));

            InetAddress address = dgp.getAddress();
            int clPort = dgp.getPort();
            data = new String ("I am Server!").getBytes ();
            dgp = new DatagramPacket (data, data.length, address, clPort);

            // Echo datagram packet back to client program.
            s.send (dgp);
            System.out.println ("Send to client: " + new String (data));
        }
    }
}

DGSClientT: DGSClientT:

public class DGSClientT {
    public static void main (String [] args) {
        String host = "localhost";

        // If user specifies a command-line argument, that argument represents the host name.
        if (args.length == 1) {
            host = args [0];
        }
        DatagramSocket s = null;

        try {
            s = new DatagramSocket();

            // Create a byte array that will hold the data portion of a datagram packet's message
            byte [] buffer = new String ("Send me a datagram").getBytes ();

            InetAddress ia = InetAddress.getByName (host);
            DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia, 10000);

            // Send the datagram packet over the socket.
            s.send (dgp);
            System.out.println("Send to server: " + new String (dgp.getData ()));

            // Create a byte array to hold the response from the server program
            byte [] buffer2 = new byte [100];

            // Create a DatagramPacket object that specifies a buffer to hold the server program's response, the IP address of
            // the server program's computer, and port number 10000.
            DatagramPacket dgp2 = new DatagramPacket (buffer2, buffer.length, ia, 10000);

            // Receive a datagram packet over the socket.
            s.receive (dgp2);

            // Print the data returned from the server program and stored in the datagram packet.
            System.out.println ("Recive from Server: " + new String (dgp2.getData ())); 
        } catch (IOException e) {
            System.out.println (e.toString ());
        } finally {
            if (s != null) {
                s.close ();
            }
        }
    }
}

You have to move the first line: 您必须移动第一行:

DatagramPacket dgp = new DatagramPacket (data, data.length);

into the "while" loop. 进入“ while”循环。 The "dgp" is constructed with a buffer length of 12 bytes at the end of the while loop while send the server answer. “ dgp”的构造是在发送服务器应答时,在while循环结束时缓冲区长度为12个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM