Garbage in java string

I am creating a simple client-server program for the school. The server receives a message from the client and displays it in the console.

It uses Java and UDP sockets. Both the client and the server work.

My problem is on the server. After displaying the message in the console, it fills the rest of the line with garbage (more precisely, small squares).

Client Source:

import java.io.*; import java.net.*; public class Main { public Main() { // TODO Auto-generated constructor stub } public static void main(String[] args) { int argc = args.length; if (argc!=2){ System.out.println("Syntax:"); System.out.println("java javaUDPclient ip/hostname port"); return; } String hostname = args[0]; int port = Integer.parseInt(args[1]); //create try{ System.out.println ("Binding to a local port"); // CREATE A DATAGRAM SOCKET, BOUND TO ANY AVAILABLE LOCAL PORT DatagramSocket socket = new DatagramSocket(); System.out.println ("Bound to local port " + socket.getLocalPort()); // CREATE A MESSAGE TO SEND USING A UDP PACKET String message = new String("Time"); // GET THE CONTENTS OF OUR MESSAGE AS AN ARRAY OF BYTES byte[] barray = message.getBytes(); // CREATE A DATAGRAM PACKET, CONTAINING OUR BYTE ARRAY DatagramPacket packet = new DatagramPacket( barray, barray.length ); System.out.println ("Looking up hostname " + hostname ); // LOOKUP THE SPECIFIED HOSTNAME, AND GET AN INETADDRESS InetAddress addr = InetAddress.getByName(hostname); System.out.println ("Hostname resolved as "+addr.getHostAddress()); // ADDRESS PACKET TO SENDER packet.setAddress(addr); // SET PORT NUMBER TO 2000 packet.setPort(port); // SEND THE PACKET - REMEMBER NO GUARANTEE OF DELIVERY socket.send(packet); socket.send(packet); System.out.println ("Packet sent!"); }catch (UnknownHostException e){ System.err.println ("Can't find host " + hostname); }catch (IOException e){ System.err.println ("Error - " + e); } } } 

Server Source:

 package progd.java.udp.time.server; import java.net.*; import java.io.*; public class UDPserver { public static void main(String[] args) { try{ System.out.println ("Binding to local port 6001"); // CREATE A DATAGRAM SOCKET, BOUND TO THE SPECIFIC PORT 6001 DatagramSocket socket = new DatagramSocket(6001); // CREATE A DATAGRAM PACKET WITH A MAXIMUM BUFFER OF 256 BYTES DatagramPacket packet = new DatagramPacket(new byte[256], 256); // RECEIVE A PACKET (BY DEFAULT, THIS IS A BLOCKING OPERATION) socket.receive(packet); String message = new String(packet.getData()); // DISPLAY PACKET INFORMATION InetAddress remote_addr = packet.getAddress(); System.out.println("Sent by: " + remote_addr.getHostAddress()); System.out.println ("Sent from port: " + packet.getPort()); System.out.println("Message:\n"+ message); socket.close(); } catch (IOException e){ System.err.println ("Error - " + e); } } } 

Client Console Output:

 Binding to a local port Bound to local port 58534 Looking up hostname localhost Hostname resolved as 127.0.0.1 Packet sent! 

Server console output: (if I copy paste from the console, the squares are not recognized in text editors, so I will do printscrean)

enter image description here

Am I missing the string-endig characters?

thanks

+4
source share
1 answer

As the JavaDoc DatagramPacket.getData() says:

Received data or sent data starts with offset in the buffer and lasts length long.

This means that all the byte[] that you received is not necessarily valid - you must extract some of this that matters. Try this on the client side:

 new String(packet.getData(), packet.getOffset(), packet.getLength()); 

We use the constructor String(byte[] bytes, int offset, int length) .

+7
source

Source: https://habr.com/ru/post/1438168/


All Articles