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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:06:38+00:00 2026-06-04T03:06:38+00:00

I faced a new problem. I’m writing a messenger in Java+Swing and I chose

  • 0

I faced a new problem.
I’m writing a messenger in Java+Swing and I chose that way to make a client’s conversation with server:

I have class Running(all the code is for client)
(sever is OK – I checked)

public class Running {
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();
    }
}

}

And in main i make one variable

  run = new Running();

Then I send this variable everywhere and it seemed to work.
It provides readline or writeline throwgh sockets.
I could register a user, log in or add contact.

I can open a MessageFrame to send messages to my contact.
But when I close it, my programm stopps reacting properly to server’s messages.
It reacts only to 30-70% of the messages.
I checked – server is OK.
So the problem is in Running or MessageFrameListener

public class MessageFrameListener{
private MessageFrame mf;
private User us;
private Contact cn;
private Timer timer;
private Running run;

public MessageFrameListener(MessageFrame m_f, User u_s, Contact c_n, Running r_n){
    run = r_n;
    mf = m_f;
    us = u_s;
    cn = c_n;
    m_f.addButtonListener(new SButtonListener());
    m_f.addWinListener(new FrameListener());
    timer = new Timer(500,new timerListener());
    timer.start();
}


public class timerListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
                SwingWorker<String,Void> sw = new SwingWorker<String,Void>(){
                    public String doInBackground() {
                        String ret = "";
                        ret = run.receiveLine();
                        return ret;
                    }
                    public void done() {
                        String[] results = null;

                            try {
                                results = get().split(" ");
                            } catch (InterruptedException
                                    | ExecutionException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        if("m".equals(results[0])){
                            if("-1".equals(results[2]))
                                mf.addLine2("Error");
                            else{
                                mf.addLine2(results[3]);
                            }
                        }


                    }
                };
                sw.execute();
    }
}

public class SButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
            String insert = mf.getInput();
            if(!insert.equals("")){
                String infoString = "m "+us.getName()+" "+cn.getName()+" "+insert;
                run.sendLine(infoString);
                mf.addLine(insert);
                mf.refreshInput();
            }
    }
}


public class FrameListener implements WindowListener{

    @Override
    public void windowClosing(WindowEvent e) {
        timer.stop();
        timer = null;
        mf.close();
    }

New information!
I started debugging. My client gets messages from server and gets to this line

   mf.addLine2(results[3]);

But then nothing happens! Swing doesn’t add new line.

The code of addLine2:

  public void addLine2(String line){
    //dialogArea.append(line+"\n");

    try { 
        if(line != ""){
            String formLine = us.getName()+" ("+now()+")\n";
            doc.insertString(doc.getLength(), formLine+line+"\n",st2);
        }

    }   
    catch (BadLocationException e){
        e.printStackTrace();
    }

}

Of course line!=””;
May be it is some Swing problem with threads? And Swing can’t deal with my requests?

Vasily.

  • 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-04T03:06:40+00:00Added an answer on June 4, 2026 at 3:06 am

    Hey what he is saying is right , that string is going to with /n but it is trying to fetch .

    in that just try with  **While** condition , that till true get the string .
    

    Here is the sample ,

    try {
            while(true) {
    
                DataInputStream is = new                      DataInputStream(sock.getInputStream());
              System.out.println("" +is.readLine());
    
                line =is.readLine();
    
            } // end of while
        } catch(Exception ex) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to java and here I faced a problem. I have a
I am a new in ms sql server and faced with the problem: i
I'm new in web services. I have faced some problem. At the server side
I'm faced a new problem something with memory allocation and leak here is my
I'm new in developing iOS app. So I've recently faced a problem with localization
Hi i am almost new in programing. I am faced with an error that
Today we faced a quite simple problem that were made even simpler by the
Hello I faced the same problem I have 1 broadcast receiver from that I
I am new to regex and recently faced this problem Appending and Prepending '#'
Today I faced the following problem: I registered a new user on my Joobla

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.