java - how to convert Date object to string/long and send it via client/server -


hi i'm having little trouble sending date object client server can compare them. i'm trying is: 1)send date object 2)compare times 3)set time both machines average time both

so far have done this:

server:

public class server implements runnable{     private int port;     private string name;     private date mydate = new date();     private date date;     public server(int port, string name){         this.port=port;         this.name=name;     }     public synchronized void run(){         while(true){         try{              method();          }         catch(exception e){             return;         }          }     }      public synchronized void method() throws exception{         long dates;         long average=0;         string[] values = new string[10];          dateformat df = new simpledateformat("eee mmm dd kk:mm:ss zzz yyyy");       serversocket server = new serversocket(port);        socket s=server.accept();        inputstream in= s.getinputstream();       outputstream out = s.getoutputstream();        printwriter w = new printwriter(out);       scanner r = new scanner(in);       for(int i=0; i<10; i++){       string msg = r.next();       values[i] = msg;      }       for(int j=0; j<values.length; j++){           dates = long.parselong(values[j]);           date = new date(dates);           average = average + date.gettime();       }       average = (average - mydate.gettime())/2;       mydate.settime(average);       system.out.println(name+": "+mydate.tostring()); } } 

client:

public class client implements runnable{     private int port;     private int id;     date time = new date();     date time2 = new date();     public client(int port,int id){         this.port=port;         this.id = id;     }      public synchronized void run(){          try{             thread.sleep(5000);              method();          }         catch(exception e){             return;         }      }      public synchronized void method() throws exception{         dateformat df = new simpledateformat("eee mmm dd kk:mm:ss zzz yyyy");         random ran = new random(500);         int d = ran.nextint(500)+500;         time.settime(time.gettime()+d);         string datestr;         socket s = new socket("localhost", port);          inputstream in= s.getinputstream();         outputstream out = s.getoutputstream();          printwriter w = new printwriter(out);         scanner r = new scanner(in);         for(int i=0; i<10; i++){         time.settime(time.gettime()+d);         datestr = df.format(time);         w.println(datestr);         w.flush();         }       } } 

but result 'nothing' white space. program compiles fine errors , kind of know problem can't figure out how parse values correctly can read properly. please me :s

in client send date string formated simpledateformat resulting in like: "sat mai 04 22:27:24 pst 2013"

on server side take string tries parse long can use future in date(long date) constructor.

the new date(long date) expect date unixtime. unixtime when call date.gettime(). unixtime milliseconds sins 01.01.1970 00:00:00:0000.

i expect server throw som kind of parse exception.

you have deside if want use simpledateformat format transmittion side , use simpledateformat parse on reciving side. or send unixtime.

i would've recommend sending unixtime. convert date object by:

long unixtime =  date.gettime()  

and back:

date date = new date(unixtime) 

or serverside dont need date object @ all, use call gettime() witch in turn gives unixtime.

server:

public class server implements runnable{     private int port;     private string name;     private date mydate = new date();     private date date;     public server(int port, string name){         this.port=port;         this.name=name;     }     public synchronized void run(){         while(true){         try{              method();          }         catch(exception e){             return;         }          }     }      public synchronized void method() throws exception{         long unixtime;         long average=0;         string[] values = new string[10];          serversocket server = new serversocket(port);        socket s=server.accept();        inputstream in= s.getinputstream();       outputstream out = s.getoutputstream();        printwriter w = new printwriter(out);       scanner r = new scanner(in);       for(int i=0; i<10; i++){       string msg = r.next();       values[i] = msg;      }       for(int j=0; j<values.length; j++){           unixtime= long.parselong(values[j]);           average = average + unixtime;       }       average = (average - mydate.gettime())/2;       mydate.settime(average);       system.out.println(name+": "+mydate.tostring()); } } 

client:

public class client implements runnable{     private int port;     private int id;     date time = new date();     date time2 = new date();     public client(int port,int id){         this.port=port;         this.id = id;     }      public synchronized void run(){          try{             thread.sleep(5000);              method();          }         catch(exception e){             return;         }      }      public synchronized void method() throws exception{         random ran = new random(500);         int d = ran.nextint(500)+500;         time.settime(time.gettime()+d);         socket s = new socket("localhost", port);          inputstream in= s.getinputstream();         outputstream out = s.getoutputstream();          printwriter w = new printwriter(out);         scanner r = new scanner(in);         for(int i=0; i<10; i++){         time.settime(time.gettime()+d);         w.println(time.gettime());         w.flush();         }       } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -