Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:43:43+00:00 2026-06-12T14:43:43+00:00

I’m building a simple client/server program to school. The server recieves a message from

  • 0

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)

enter image description here

Am I missing the “string-endig” characters?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:43:45+00:00Added an answer on June 12, 2026 at 2:43 pm

    As the JavaDoc of DatagramPacket.getData() says:

    The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

    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:

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

    We are using String(byte[] bytes, int offset, int length) constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.