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 8770073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:29:07+00:00 2026-06-13T17:29:07+00:00

Hi I made an echo client – server code using datagram. The code is

  • 0

Hi I made an echo client – server code using datagram. The code is running and the messages sent from the client side are being received at the server side, then they are echoed back to the client. The following scenario shows what problem I am having

1->Client: hi , server : hi ,
echoed back : hi

2->Client: hello , server : hello ,
echoed back : hello
Now when the client types a message shorter than the last one the following thing happens

3->Client: K , server : Kello ,
echoed back : K

I can’t understand why the server prints the rest of the characters of the previous message, here is the server side code:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;

    import javax.swing.SwingUtilities;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class Server extends JFrame {
int port;
// JTextField textField;
JButton resendPacket;
JTextArea chatWindow;
DatagramSocket socket;
private final int PACKET_SIZE = 124;
private final int port_Number = 6789;
DatagramPacket packet =  new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);

    InetAddress host;


public Server() {
    super("INSTANT ECHO - SERVER");
    resendPacket = new JButton("Echo");
    chatWindow = new JTextArea();

//  add(resendPacket, BorderLayout.SOUTH);
    add(new JScrollPane(chatWindow));
    setSize(300, 300);
    setVisible(true);
}

public void startRunning() {

    try {
        port = 6777;
        socket = new DatagramSocket(port);
        showMessage("Server is Ready... \n");
        /*resendPacket.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    socket.send(packet);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }); */
        while(true)
        {
            processPacket();
        }


    } catch (SocketException e) {
        // TODO: handle exception
        showMessage("\n Could not send packet!\n");
        e.printStackTrace();
    } 
}

private void processPacket() {




    // create a packet

    // receive a packet

    try {
        socket.receive(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        showMessage("\nCould not receive Packet");
        e.printStackTrace();
    }
    try {
        socket.send(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    // display the contents of the packet
    showMessage(new String(packet.getData()));



    // send the packet again to the sender

}

public void showMessage(final String text) {
    SwingUtilities.invokeLater(

    new Runnable() {

        public void run() {
            chatWindow.append("\n Packet Data : " + text);
        }
    });

}

}

And here is the client side code

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;

    public class Client extends JFrame {
int port = 6777;
JTextField textField;
String data = "hello" ;
// JButton sendPacket;
JTextArea chatWindow;
DatagramSocket socket;
DatagramPacket packet;
private final int PACKET_SIZE = 124;
InetAddress host;

public Client() {
    super("INSTANT ECHO - CLIENT");

    textField = new JTextField();
    try {
        host = InetAddress.getByName("127.0.0.1");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        showMessage("\n Could not find local host");
        e.printStackTrace();
    }

try {

        socket = new DatagramSocket();

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        showMessage("\n Could not find Socket");
        e.printStackTrace();
    }
    // textField.setEditable(false);
    textField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub
            data = event.getActionCommand();
            showMessage(event.getActionCommand());
            startRunning();
            textField.setText("");

        }
    });

    chatWindow = new JTextArea();
    add(textField, BorderLayout.SOUTH);


    add(new JScrollPane(chatWindow));
    setSize(300, 300);
    setVisible(true);

}

public void startRunning() {



    byte[] bytes_to_send = data.getBytes();

    DatagramPacket packet = new DatagramPacket(bytes_to_send,
            bytes_to_send.length, host, port);
    try {


        sendPacket(packet);
        setTime();
        receivePacket(packet);

        socket.receive(packet);
        showMessage("\n" + new String(packet.getData()));
    } catch (IOException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    } finally {
        //closeCrap();
    }

}

private void sendPacket(DatagramPacket packet) {
    try {
        textField.setEditable(true);

        socket.send(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        showMessage("\nUnable to send Packet !");
        e.printStackTrace();
    }
}

private void setTime() {
    try {
        socket.setSoTimeout(5);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        showMessage("\nRequest Time Out");
        e.printStackTrace();
    }
}

private void receivePacket(DatagramPacket packet) {
    packet = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);

    try {
        socket.receive(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        showMessage("\n Unable to receive packet\n");
        e.printStackTrace();
    }
    showMessage("\n Echoed Message --> "+new String(packet.getData()));
}

private void closeCrap() {
    socket.close();
}

private void showMessage(final String data) {
    SwingUtilities.invokeLater(

    new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            chatWindow.append("\n" + data);

        }
    });

}

}

Can anyone tell why the previous characters are getting printed?

  • 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-13T17:29:09+00:00Added an answer on June 13, 2026 at 5:29 pm

    You’re reusing the same packet over and over, so when it reads information from the network, it overwrites the existing packet buffer contents with new content. The client isn’t writing a null terminator, so none is being written into the buffer. As such, when you print the data with showMessage(new String(packet.getData())); the data you get back still has remnants of the old data in it if the message just received is not long enough to completely overwrite the old contents.

    It’s not echoing back the extra data because when you send the packet, the socket obeys packet.getLength(), which controls how many bytes are to be sent.

    You can either:

    • Change your showMessage invocation to:

      showMessage("\n Echoed Message --> " + 
                  new String(packet.getData(), 0, packet.getLength()));
      
    • use a different packet each time

    Any of those should fix it.

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

Sidebar

Related Questions

I'm connecting to local web server from android client and getting results back from
Hello i made a php socket server to get data from plc, plc is
i made a chat application, here is the refresh code setInterval(function() { $('#DisplauDiv').load('show-chat.php?session=<?php echo
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
I made an iphone app to capture image from camera and to set that
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
Made a new project, added main.cpp and wrote the code at this URL: http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp
i have made a module using hook function . its working but when i
I purchased modalbox plugin from envato market place made in javascript i want to
I have a makefile such as : A : B echo made A B

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.