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?
Thnaks!
LVL 2
hpchong7Asked:
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
0
Ravindra76Commented:

Hi hpchong7,

  Q1.
>>

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().

Q2.

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



0
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.addMouseMotionListener(this);
      cv.addMouseListener(this);
           cv.setBackground(new Color(255,255,255));
        getContentPane().add(cv);
        g=cv.getGraphics();
       try {soc = new Socket("127.0.0.1",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)
         {  
               g.drawLine(a,b,c,d);
               //validate();
               //repaint();
               return;
         }         
   public void mousePressed(MouseEvent e)
   {
      oldx = e.getX();
      oldy = e.getY();
      System.out.println("press!");
   }
   
   public void mouseDragged(MouseEvent e)
   {
      int mx, my;
      mx = e.getX();
      my = e.getY();
        s="";
      g.drawLine(mx, my, oldx, oldy);
     if(mx<0){mx=0;}
        if(my<0){my=0;}
        if(oldx<0){oldx=0;}
        if(oldy<0){oldy=0;}
        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);
      s=w.concat(tlen).concat(",").concat(ox).concat(",").concat(oy).concat(",").concat(ex).concat(",").concat(ey).concat("");
      o.println(s);
      //o.flush();
      System.out.println(s);
                         
      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;
        this.start();
    }

   //public Runnable work = new Runnable() {
      public void run() {
        int pos0,pos1,pos2,pos3,pos4,colen=0;
            pos0=0;
            pos1=0;
            pos2=0;
            pos3=0;
            pos4=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="";}
                                                
                        System.out.println(s);
                      
                        pos0=s.indexOf(',',2);
                        System.out.println("pos0:"+pos0);
                        String temp=s.substring(2,pos0);
                        colen=Integer.parseInt(temp);
                        colen=colen+temp.length()+1+2;
                        System.out.println(colen);
                        
                        pos1=s.indexOf(',',pos0+1);
                        System.out.println("pos1:"+pos1);
                      temp=s.substring(pos0+1,pos1);
                        beginx[count]=Integer.parseInt(temp);
                        System.out.println(beginx[count]);
                        
                        count++;            
                      
                        pos2=s.indexOf(',',pos1+1);
                        System.out.println("pos2:"+pos2);
                        temp=s.substring(pos1+1,pos2);
                        beginx[count]=Integer.parseInt(temp);
                        System.out.println(beginx[count]);
                  
                        count++;
                        
                        pos3=s.indexOf(',',pos2+1);
                        System.out.println("pos3:"+pos3);
                        temp=s.substring(pos2+1,pos3);
                        beginx[count]=Integer.parseInt(temp);
                        System.out.println(beginx[count]);
                  
                        count++;
                        
                        temp=s.substring(pos3+1,colen);
                        beginx[count]=Integer.parseInt(temp);
                        System.out.println(beginx[count]);
                        count++;
                        System.out.println(count);
                        pos0=pos1=pos2=pos3=0;
                        if (count>=65536)
                        {count=0;}
                        
            WhiteBallaa.re(beginx[count-4],beginx[count-3],beginx[count-2],beginx[count-1]);
                        
                  }      
            }
      catch (IOException e) {}
     }        
        
}
0
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!
0
vladi21Commented:
u can submit answer
0

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
Java

From novice to tech pro — start learning today.