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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:21:46+00:00 2026-05-24T23:21:46+00:00

I have a server-client pair and I want to create a listener on the

  • 0

I have a server-client pair and I want to create a listener on the client end for new server responses. I am not sure how to do this, right now I can only interact in a direct synchronous way.

Here is the server:

public class TestServer {

public static void main(String[] args) throws Exception {

    TestServer myServer = new TestServer(); 
    myServer.run(); 

}

private void run() throws Exception {

    ServerSocket mySS = new ServerSocket(4443);     

    while(true) {

        Socket SS_accept = mySS.accept(); 

        BufferedReader myBR = new BufferedReader(new InputStreamReader(SS_accept.getInputStream()));

        String temp = myBR.readLine(); 
        System.out.println(temp); 


        if (temp!=null) {

            PrintStream serverPS = new PrintStream(SS_accept.getOutputStream()); 
            serverPS.println("Response received: " + temp); 

        }

    }
}

}

As you can see, it sends a response when it gets one. However in general I won’t be sure when other servers I use send responses, so I would like to create an asynchronous listener (or at least poll the server for a response every half-second or so).

Here is what I’m trying on the client end:

protected static String getServerResponse() throws IOException {

    String temp;
    try {
        BufferedReader clientBR = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); 
        temp = clientBR.readLine(); 
    } catch (Exception e) {
        temp = e.toString(); 
    }

    return temp; 
}

And just for reference, yes, sending over data from client to server works fine (it System.out’s the data correctly). However, when I call the above function to try and retrieve the server response, it just hangs my application, which is an Android application in case that’s relevant.

What I want from a function is just the ability to ask the server if it has data for me and get it, and if not, then don’t crash my damn app.

  • 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-24T23:21:47+00:00Added an answer on May 24, 2026 at 11:21 pm

    On the client side create a ConnectionManager class which will handle all the socket I/O. The ConnectionManager‘s connect() method will create and start a new thread which will listen for server responses. As soon as it will receive a response it will notify all the ConnectionManager‘s registered listeners. So in order to receive asynchronously the server responses you will have to register a listener in ConnectionManager using its register(SomeListener) method.

    Also, you can have a look at JBoss Netty which is an asynchronous event-driven network application framework. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

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

Sidebar

Related Questions

I have this line of code in my server/client pair: BufferedOutputStream out = new
I have a (client/server communication) setup right now like so: 1) Client asks user
i have a server - client application that runs on java 1.3; i want
I have this server/client TCP. The server is run off my desktop. And the
I have a written a simple Client-Server pair, sending an Object to the server.
I'm not exactly an expert at this, but I'm pretty sure I have covered
Typically you have a server and client pair or two peers that connect to
I'm writing a UDP client-server pair for a networks class, and I have hit
I have a server/client app on a Linux box. If the server is not
I have a server - client pair written in java and I would 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.