Java UDP sockets – data left behind on the server side

I'm using UDP sockets to implement a very basic server - client model in Java, and I encountered a very strange problem

What I want to do is let the user (client) send a message to the server, and then the server will print it

I have an example, but I missed something because I have the following questions:

If the client sends the message "a" to the server, it will receive the message correctly If the client sends the message "BBB" to the server, it will receive the message correctly If the client sends the message "C" to the server, the server will print "CBB" as the received message

It seems that the server cleans up some kind of buffer when it receives new messages

This is the code I'm using:

The server

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;



public class UDPServer {
public static void main(String args[]) throws Exception {
    byte[] receive_data = new byte[256];
    int recv_port;

    DatagramSocket server_socket = new DatagramSocket(5000);

    System.out.println("Server - Initialized server. Waiting for client on port 5000");

    while (true) {
        // System.out.println("Server - Listening for connections...");
        DatagramPacket receive_packet = new DatagramPacket(receive_data,receive_data.length);

        server_socket.receive(receive_packet);

        String data = new String(receive_packet.getData());

        InetAddress IPAddress = receive_packet.getAddress();

        recv_port = receive_packet.getPort();

        if (data.equals("q") || data.equals("Q")) {
            System.out.println("Server - Exiting !");
            break;
        } else {
            System.out.println("Server - Client from IP " + IPAddress + " @ port " + recv_port + " said : " + data + " (length: " + receive_packet.getLength() + ")");
        }
    }
}
}

customer

public class UDPClient {
public static void main(String args[]) throws Exception {
    byte[] send_data = new byte[256];

    BufferedReader infromuser = new BufferedReader(new InputStreamReader(system.in));

    DatagramSocket client_socket = new DatagramSocket();

    InetAddress IPAddress = InetAddress.getByName("localhost");

    System.out.println("Client - Initialized the client...");

    while (true) {
        System.out.print("Client - Type Something (q or Q to quit): ");

        String data = infromuser.readLine();

        if (data.equals("q") || data.equals("Q")) {
            System.out.println("Client - Exited !");
            DatagramPacket send_packet = new DatagramPacket(send_data,send_data.length,IPAddress,5000);
            System.out.println("Client - Sending data : <" + data + ">");
            client_socket.send(send_packet);
            break;
        } else {
            send_data = data.getBytes();
            DatagramPacket send_packet = new DatagramPacket(send_data,5000);
            System.out.println("Client - Sending data : <" + data + ">");
            client_socket.send(send_packet);
        }
    }

    client_socket.close();
}
}

I think mistakes are trivial, but my skills in network programming are limited, so I don't know what they are

To make it clear, I run the server and client on the same machine (MAC) on different terminals in case it will affect the situation anyway

Any help would be appreciated

edit

... I came back to answer my own question The problem is that I don't define the amount of data that the server socket should read So when I change

String data = new String(receive_packet.getData());

with

String data = new String(receive_packet.getData(),receive_packet.getLength());

Deo gratias.

For future reference only, and people who may encounter the same problem:)

Solution

On the first call, this is receive_ What data looks like:

--------------
|"a"|    |    |
 --------------

On your second call:

--------------
|"b"|"b"| "b" |             notice that the "a" in data_receive was overwritten
 --------------

In the third call, you only send one letter, so the only part of the overwritten array is the first element:

--------------
|"c"|"b"| "b" | 
 --------------

This happens because there is still a receive between messages sent to the server_ A simple way to the data in the data array is to initialize the new array inside the receiving loop In this way, you will have a new array waiting for you every time you receive a message

while (true) 
{
    byte[] receive_data = new byte[256];
    ......
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>