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

  • Home
  • SEARCH
  • 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 6710359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:00:45+00:00 2026-05-26T08:00:45+00:00

The server acts like an echo server. The clients sends 10 packets to server

  • 0

The server acts like an echo server. The clients sends 10 packets to server (1 sec of gap)

When Client receives packets from the server, sometimes the packets are lost.

So the client has to wait for up to one second for the packet to arrive. If the packet does not arrive in 1 second then the client should continue sending the other packets.

How would i use .setSoTimeout to achieve this?

Code:

import java.io.*;
import java.net.*;
import java.util.*;
/*
* Client to process ping requests over UDP.
*/
public class PingClient
{
    private static final int AVERAGE_DELAY = 100; // milliseconds
    public static void main(String[] args) throws Exception
    {
// Get command line argument.
        int port = Integer.parseInt(args[1]);//specified as argument
// Create random number generator for use in simulating
// packet loss and network delay.
        System.out.println("Port "+port);
// Create a datagram socket for receiving and sending UDP packets
// through the port specified on the command line.
        DatagramSocket socket = new DatagramSocket(1234);

    int i=0;
        for(i=0;i<10;i++)
    {
    byte[] buf = new byte[1024] ;
    Calendar cal=Calendar.getInstance();
    String ping="Ping "+ i +" "+cal.getTimeInMillis()+"\r\n";
    buf=ping.getBytes("UTF-8");
    InetAddress address = InetAddress.getByName(args[0]);
    System.out.println("Name "+args[1]);
    DatagramPacket packet = new DatagramPacket(buf, buf.length, 
                                       address, port);
    packet.setData(buf);
    socket.send(packet);
    Thread.sleep( 10* AVERAGE_DELAY);//1 sec

    DatagramPacket server_response = new DatagramPacket(new byte[1024], 1024);
    // Block until the host receives a UDP packet.

        socket.setSoTimeout(1000); //I don't know how to use this
        socket.receive(server_response);

    // Print the recieved data.

        printData(server_response);

}   
}

private static void printData(DatagramPacket request) throws Exception
    {
// Obtain references to the packet's array of bytes.
    byte[] buf = request.getData();
// Wrap the bytes in a byte array input stream,
// so that you can read the data as a stream of bytes.
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);
// Wrap the byte array output stream in an input stream reader,
// so you can read the data as a stream of characters.
    InputStreamReader isr = new InputStreamReader(bais);
// Wrap the input stream reader in a bufferred reader,
// so you can read the character data a line at a time.
// (A line is a sequence of chars terminated by any combination of \r and \n.)
    BufferedReader br = new BufferedReader(isr);
// The message data is contained in a single line, so read this line.
    String line = br.readLine();
// Print host address and data received from it.
    System.out.println(
        "Received from " +
        request.getAddress().getHostAddress() +
        ": " +
        new String(line) );
    }

}

  • 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-05-26T08:00:45+00:00Added an answer on May 26, 2026 at 8:00 am

    The javadoc for setSoTimeout says:

    With this option set to a non-zero timeout, a call to receive() for
    this DatagramSocket will block for only this amount of time. If the
    timeout expires, a java.net.SocketTimeoutException is raised, though
    the DatagramSocket is still valid.

    So, if you want to send packets if no response has been received after 1 second, you just have to use

    socket.setSoTimeout(1000L);
    boolean continueSending = true;
    int counter = 0;
    while (continueSending && counter < 10) {
        // send to server omitted
        counter++;
        try {
            socket.receive(packet);
            continueSending = false; // a packet has been received : stop sending
        }
        catch (SocketTimeoutException e) {
            // no response received after 1 second. continue sending
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible for exchange server to send SOAP request (acts like a client)
Apache acts like has reached to the maximum client value and I can't see
From JSP file accessed another JSP file which acts like java class, this used
HI I'm writing a program that acts as a server and has the ability
I am writing a program in C that acts like a proxy server in
I have two perl scripts; one acts like the client (and queries); the other
Server has created a socket and bound to a port and started a thread
SQL Server 2000 Standard, Windows 2003 My coworker removed 'BUILTIN\Administrators' group from SQL Server
I got the following scenario: I am writing an app, which acts like a
Ok this is the scenario... I have a table in Oracle that acts like

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.