• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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!
0
hpchong7
Asked:
hpchong7
  • 3
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now