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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:49:57+00:00 2026-05-26T14:49:57+00:00

I’m in the process of writing a Java client and server, and they’re both

  • 0

I’m in the process of writing a Java client and server, and they’re both talking to each other correctly. At the moment, however, the client must always send a message to the server before it can receive one – but I want the server to be able to send a message without the client asking for it.

In AS3, my primary language, I’d have added an event listener to the socket and handled the data that way – but I can’t seem to find out how I would do that in Java.

Currently my client has this code in it:

public String send(String message) {
    out.println(message);
    try {
        return in.readLine();
    } catch (IOException ex) {
        return "";
    }
}

Basically I call it, passing my message, and it returns giving me the server’s response.
Instead of doing this I want to send a message there, like below, and have the response picked up somewhere different (by an event listener).

public void send(String message) {
    out.println(message);
}

As I couldn’t figure out how an event listener would work, I did contemplate using a separate thread to run while(true){in.readLine;} but I want to avoid this if at all possible; I’m not too comfy with threads, and I want to keep it as simple as possible.

  • 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-26T14:49:57+00:00Added an answer on May 26, 2026 at 2:49 pm

    The simplest way to do this is to use threads. I would have at least one thread reading the socket which allows the server to send a message to the client any time.

    The problem with mixed responses and events is that your first block won’t work because the “response” could be an event.

    If you don’t want to use threads, you could have something like

    // simple but probably not a good idea.
    public String send(String message) {
        out.println(message);
        try {
            while(true) {
                String line = in.readLine();
                if (isEvent(line))
                   processEvent(line);
                else
                   return line;
        } catch (IOException ex) {
            return "";
        }
    }
    

    However, it will only read events when a request is sent and if this is not done regularly it can cause the stream to bank up all the way to the server causing it to block as well.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.