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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:26:37+00:00 2026-06-03T23:26:37+00:00

This is a client part of programm. I have such class which works with

  • 0

This is a client part of programm.
I have such class which works with sockets and has methods to read and write to server
How can I make an event to catch messages from server?
Some actionListener which makes actions only when thereis a message in inputStream.
I’ve tried timer which calls Running.receiveLine(); but it works very bad.
The solution with while(true) also seems to be not good.

public class Running extends Thread{
private Socket s;
private PrintStream ps;
private BufferedReader br;

public Running(){
try{
    s = new Socket(InetAddress.getLocalHost(), 8072);
    ps = new PrintStream(s.getOutputStream());
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException ex) {
    System.out.println("11");
    ex.printStackTrace();
} catch (IOException ex) {
    System.out.println("00");
    ex.printStackTrace();
}
}

public String receiveLine(){
    String ret = "";
    try {
        ret = br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ret;
}

public void sendLine(String s){
    ps.println(s);
}

public void close(){
    try {
        s.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
  }
  • 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-03T23:26:38+00:00Added an answer on June 3, 2026 at 11:26 pm

    I think it’s not a problem to use method read() because it blocks flow until new portion of data is available. All standard Java input streams have a good implementation of this approach, based on monitors. So your thread won’t eat CPU and other resources during waiting.

    Also you can refer to non-blocking IO in Java NIO.

    Small addition.
    To use it with Swing you can use queue approach in fusion with SwingWorker.

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

Sidebar

Related Questions

This is a client-server programm. For each client server has a method, which checks
i have this client/server application, and the client application sometimes completely freezes when i
i have a java chat server & client, that works fine. I made a
I have a client-server program. I'm sending data like this: private void Sender(string s,TcpClient
I have a program with a Mono-based C# client and a Python server, which
I have a client / server application exactly like in this link. When I
You may skip this part: I am creating an application where the client needs
Hey all, I'm about to rip my hair out. I have this client that
I have this jsp client that consumes a webservice. The problem with the client
I have this piece of code using standard sockets: void set_fds(int sock1, int sock2,

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.