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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:19:18+00:00 2026-05-20T03:19:18+00:00

I have a function for sending data over Socket class in Java. When I

  • 0

I have a function for sending data over Socket class in Java. When I send data I need to receive some data. The problem is how to set a timeout for waiting data on 2sec (if I don’t receive data in 2sec I need to understand that happened a communication error and show a message).
This is my code, any help?

    public boolean SendMonitorMessage(
            final MonitorRequestRepeatMessageTCP message) {
        boolean result = true;
        System.out
                .println("****************** SEND MONITOR REQUEST REPEAT MESSAGE TCP **********************************");

        // new Thread() {
        // public void run() {
        int prevService=message.GetService();
        synchronized (socket) {
            try {
                System.out.println("IPADDRESS=" + ipAddress);
                System.out.println("PORT=" + port);
                System.out.println("Is reachable=" + Ping());
                message.PrintMessage(message.toBytes());

                OutputStream socketOutputStream = (OutputStream) socket
                        .getOutputStream();
                socketOutputStream.write(message.toBytes());

                InputStream socketInputStream = (InputStream) socket
                        .getInputStream();
                
                byte[] buffer = new byte[256];
                List<byte[]> received = new LinkedList<byte[]>();
                int numberReceived;
                byte[] tempBuffer;

                while ((numberReceived = socketInputStream.read(buffer)) != -1) {
                    tempBuffer = new byte[numberReceived];
                    ByteBuffer baferce = ByteBuffer.wrap(tempBuffer);
                    baferce.put(buffer, 0, numberReceived);
                    received.add(tempBuffer);
                }
                
                if (received.size()>0){
                    new MonitorResponseMessageTCP(received, message.GetMonitorVariablesArrayList(), prevService);
                }
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return true;
    }
  • 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-20T03:19:19+00:00Added an answer on May 20, 2026 at 3:19 am

    See Socket and ServerSocket. Both classes have a setSoTimeout method to specify the maximum time to wait when waiting for connections or waiting to receive data. When that time has elapsed, the socket throws a SocketTimeoutException that you can handle with your error message or however you want.

    You have to call setSoTimeout before performing the actions you want to have a timeout.

    Prior to

    while ((numberReceived = socketInputStream.read(buffer)) != -1) {
    

    You’ll need to call

    socket.setSoTimeout(2000);
    

    And then add a catch(SocketTimeoutException) section to the try/catch block you already have.

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

Sidebar

Related Questions

The following code waits for data over UDP. I have a test function that
I have a problem with sending some custom headers to with a jQuery ajax
I have a function where I need to do something to a string. I
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I am currently trying to send some data from and Android application to a
I have the following function sending an email twice (and I believe running if($result)
So i have this function in JS, sending a request to insert a new
I want to send a string data with jquery load function but its not
so I have some main function. 24 time a second it opens a boost
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035

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.