flush and printwriter

in my client whiteboard program:
PrintWriter o = new PrintWriter(soc.getOutputStream(),true);
//inside a function(mousedrag) in which ouput the mouse //coordinate continuously to my server:

o.println(s) //where s is a string of 2-pair-coordinates defined //previously with length <15

o.flush() //Q1:Is this line necessary?What's the difference/relationship betweeen o.println() and o.flush()?

Q2:When the server receive s,it sometimes appear double or even trible the lenght of s defined by my client program(4 to 6 pair of coordinates,but sometime it is 2 pair)Why?How can I resolve?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hpchong7Author Commented:
Adjusted points to 16

Hi hpchong7,


o.flush() //Q1:Is this line necessary?What's the
                      difference/relationship betweeen o.println() and o.flush()?

Now oo.flush() is unnecessary because youhave written "true" in the constructor

PrintWriter o = new PrintWriter(soc.getOutputStream(),true);

Relationship between flush() and println():

When you are using println(), it actually not priniting tp printwriter.

The data is stored in Buffer.

When you write flush(), the data are actually printed to PrintWriter object.

In construactor you mentioned true means for every print statement , it will automatically flushes. i.e  prints.

If you did not specify true in constructor, for every statement you have to use flush().


From your piece of code, it is difficult to judge what you are doing with s.

I think you are appending the values to s or somehing is happening.

If you post remaining code about what you are doing with s, it can be easily solved

hpchong7Author Commented:
then I try to inclue my program:

import java.applet.Applet;
import java.awt.*;
import java.applet.*;
import java.lang.Integer;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
import javax.swing.SwingUtilities;

public class WhiteBallaa extends JApplet implements MouseMotionListener, MouseListener
   static Canvas cv;
   int oldx,oldy;
   StreamListener listener;      
   Socket soc;
   BufferedReader i;
   PrintWriter    o;
   String s=new String("");
   Integer        ax=new Integer(0);
   Integer        ay=new Integer(0);
   Integer        bx=new Integer(0);
   Integer        by=new Integer(0);
   Integer            total=new Integer(0);
   static Graphics g;
   public WhiteBallaa()
   public void init()
     cv = new Canvas();
      cv.setSize(this.getWidth(), this.getHeight());
           cv.setBackground(new Color(255,255,255));
       try {soc = new Socket("",8000);
            o = new PrintWriter(soc.getOutputStream(),true);
            i = new BufferedReader(new InputStreamReader(soc.getInputStream()));}
       catch (IOException ex) {}

         listener=new StreamListener(i);
   public static void re(int a,int b,int c,int d)
   public void mousePressed(MouseEvent e)
      oldx = e.getX();
      oldy = e.getY();
   public void mouseDragged(MouseEvent e)
      int mx, my;
      mx = e.getX();
      my = e.getY();
      g.drawLine(mx, my, oldx, oldy);
        String w="W ";
      String ox = ax.toString(oldx);
      String oy = ay.toString(oldy);
      String ex = bx.toString(mx);
      String ey = by.toString(my);
      int totallen=ox.length()+oy.length()+ex.length()+ey.length()+3;
      String tlen=total.toString(totallen);
      oldx = mx;
      oldy = my;
   public void mouseMoved(MouseEvent e)
   public void mouseReleased(MouseEvent e)
   public void mouseEntered(MouseEvent e)
   public void mouseExited(MouseEvent e)
   public void mouseClicked(MouseEvent e)
class StreamListener extends Thread {
        int[] beginx=new int[65536];
      int count=0;
      //static WhiteBalla white=new WhiteBalla();
      BufferedReader in;  
    //Runnable work;
      public StreamListener(BufferedReader in) {
        this.in = in;

   //public Runnable work = new Runnable() {
      public void run() {
        int pos0,pos1,pos2,pos3,pos4,colen=0;
            String line,s;
           char[] buf=new char[256];  
            try {
            for(;;) {
                   int abc=in.read(buf);

                if (abc>0)      
      {s=new String(buf);}
      else      {s="";}
                        String temp=s.substring(2,pos0);
                        if (count>=65536)
      catch (IOException e) {}
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

hpchong7Author Commented:
Thank you very very much!
u can submit answer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.