I’m building a simple client/server program to school.
The server recieves a message from the client and shows it in the console.
It uses Java and UDP Sockets.
Both client and server are working.
My problem is in the server.
After displaying the message in the console, it fill the rest of the line with trash (little squares to be more precise).
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 console, the squares are not recognized in text editors so, I will make a printscrean)

Am I missing the “string-endig” characters?
Thanks
As the JavaDoc of
DatagramPacket.getData()says:This means that the whole
byte[]you got is not necessarily valid – you must extract portion of it that is relevant. Try this on the client side:We are using
String(byte[] bytes, int offset, int length)constructor.