Java UDP packet size

In Java, a single char is 16 bits, 2 bytes. This means that if I want to send a UDP packet to the server, I have to find the string length (for example) and multiply by 2?

String string = "My awesome string!";
byte[] buff = new byte[ string.length*2 ];
buff = string.getBytes();
...
packet = new DatagramPacket(buff, buff.length, address, port);
socket.send(packet);

What about the UDP packet limit? 65k package. For example, if I want to send data files to a server, I have to send 65 / 2k data! I divide 65 by 2, and what will be the limit? 65/2 or 65 kb?

For instance:

byte[] buff = new byte[ 65000 ]

//file and bufferreader handle
while( ( line = bufferedReader.readLine() ) != null ){
   buff = line.getBytes();
   packet = new DatagramPacket(buff, buff.length, address, port);
   socket.send(packet);
}

I read somewhere that I can transfer more than 65 thousand data, because the IPv4 protocol automatically splits the packet into pieces, than the receiver combines them. It's true?

Why am I getting empty space in my buffer? I wrote a client and server application, and I use a buffer of size 250. If I send a word, for example, "Test", whose length is 8 bytes, I get a very long space after the word "Test":

byte[] buff = new byte[250];
packet = new DatagramPacket(buff, buff.length);
socket.receive(packet);
System.out.println("GET: " + buff);

:

GET: ............................................................................

( )

+5
2

, UDP- , () 2?

. String.getBytes() :

byte[] buff = string.getBytes();

buff.length . , , String .

String.getBytes(), , String . :

byte[] buff = null;
try {
   buff = string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

- , 65 . , IPv4 , . ?

TCP. TCP, UDP, .

+8

- , 65 .

UDP . IPv4 65507 , - 534 .

IPv4 , . ?

, TCP, UDP

TCP.

+4

All Articles