简体   繁体   中英

UDP datagram only showing first character in Java

I'm trying to make an Echo server-client UDP application. The client is supposed to send over 20 numbers and the server will just send them back. However, from what I have, it shows only the first character from the String is being sent. I've asked my professor for help but he couldn't figure it out either. Some help would be nice. Thank you!

Client.java:

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

public class client {
    public static void main(String[] args) {
        DatagramSocket socket;
        try {
            socket = new DatagramSocket();
            InetAddress address = InetAddress.getByName("localhost");
            byte[] buf = new byte[256];
            for (Integer i = 1; i < 21; i++) {
                buf = i.toString().getBytes();
                DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
                socket.send(packet);
                packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                String received = new String(packet.getData(), 0, packet.getLength());
                System.out.println("Server: " + received);
            }
            socket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Server.java:

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

public class server {
    public static void main(String argsp[]) {
        DatagramSocket socket;
        try {
            socket = new DatagramSocket(4445);
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (true) {
                socket.receive(packet);
                String message = new String(packet.getData(), 0, packet.getLength());
                System.out.println("Client: " + message);
                buf = message.getBytes();
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client output:

Server: 1
Server: 2
Server: 3
Server: 4
Server: 5
Server: 6
Server: 7
Server: 8
Server: 9
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 1
Server: 2

Server output:

Client: 1
Client: 2
Client: 3
Client: 4
Client: 5
Client: 6
Client: 7
Client: 8
Client: 9
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 1
Client: 2

Every time you receive a datagram the DatagramPacket shrinks to the length. So, in your server, it shrinks to 1 after the first receive. You need to recreate it inside the receive lopo, or a least reset its length at the top of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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