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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:00:20+00:00 2026-06-11T13:00:20+00:00

I have to write java codes implementing the TCP server & TCP client with

  • 0

I have to write java codes implementing the TCP server & TCP client with a 10% of transmission failure determined by a random number. Now in the receiver side it needs to wait for 2000 ms(2 sec.) & if no data received from the sender in this 2 sec. then it will stop waiting for data from the sender & send back a negative ACK to the sender.

BufferedReader inFromSender =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        long startTime = System.currentTimeMillis();
        while(( System.currentTimeMillis() - startTime) < 2000 )
        {
            clientSentence = inFromSender.readLine();
            System.out.println("Received: " + clientSentence);
        }
        System.out.println("Data Transmission failed");

But once the while loop begins & the receiver goes in to read from the sender, it never breaks.. even if the 2 sec. timeout is over. How can I abort/stop the receiver from waiting for sender data after 2 sec (timeout).

  • 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-11T13:00:21+00:00Added an answer on June 11, 2026 at 1:00 pm

    One way to go would be to have a Thread that would close the socket when the timeout is reached. This would make the read(...) call fail (probably an IOException) and therefore you would go out from the loop.

    Example:

    final Socket connectionSocket = new Socket("...", 8888); // To be changed
    BufferedReader inFromSender = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
    Thread t = new Thread() {
       public void run() {
          try {
              Thread.sleep(2000); // Wait 2 s
              connectionSocket.close();
              System.out.println("Data Transmission failed");
          } catch(InterruptedException e) {
             // Handle exception
          }
       }
    };
    t.start();
    while((clientSentence = inFromSender.readLine()) != null) {
       System.out.println("Received: " + clientSentence);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I have to write a client in Java against
I am trying to Write two Java programs implementing the FTP server with responses
I have a java server that has to communicate over a TCP socket to
I have a problem; I want to write a java code for sending an
I have to write a Java program that tells what coins to give out
I have been asked to write a java program on linux platform. According to
I have to write an XML file using java.The contents should be written from
How to write a java program which will tell me whether I have internet
I need to write a java JCE provider. I have been looking in the
I need to write a standalone Java application which will have a embedded HTTP

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.