java - How to send custom object using serialization? -


i'm new java. need send on socket file content, double number. idea wrap these 2 fields custom object. since object file not serializable, how send/recv custom object? in advance. ps: sending party decide if needs send file according value of "double" number. example:

class myobject{     double number;     file file; } 

when sending, logic this:

myobject = new myobject();  if(my.number > 0){     my.file = open_file(aaa.jpg); }else{     //not opening file }  send(my); 

here quick example application sends data on sockets.

public class app {      private static final atomicinteger port = new atomicinteger(-1);     private static final atomicboolean read = new atomicboolean(true);      private static final class socketreader implements runnable {          @override         public void run() {             try {                 final serversocket serversocket = new serversocket(0);                 system.out.println("connected on " + serversocket);                 synchronized (port) {                     port.set(serversocket.getlocalport());                     port.notifyall();                 }                 while (read.get()) {                     try (final socket socket = serversocket.accept()) {                         final datainputstream dis = new datainputstream(socket.getinputstream());                         system.out.println(dis.readdouble());                                                 byte[] buffer = new byte[1024];                         int numread;                         while ((numread = dis.read(buffer)) != -1) {                             system.out.print(new string(buffer, 0, numread, "utf-8"));                         }                     }                  }             } catch (exception ex) {                 throw new runtimeexception(ex);             }          }     }      private static final class socketwriter implements runnable {          private final double d;         private final file file;          public socketwriter(double d, file file) {             this.d = d;             this.file = file;         }          @override         public void run() {             try {                 while (port.get() < 1) {                     synchronized (port) {                         port.wait();                     }                 }                 try (final socket clientsocket = new socket("localhost", port.get());                         final dataoutputstream dos = new dataoutputstream(clientsocket.getoutputstream())) {                     system.out.println(clientsocket);                     dos.writedouble(d);                     try (final fileinputstream fis = new fileinputstream(file)) {                         byte[] buffer = new byte[1024];                         int bytesread;                         while ((bytesread = fis.read(buffer)) != -1) {                             dos.write(buffer, 0, bytesread);                         }                     }                     dos.flush();                 }             } catch (exception ex) {                 throw new runtimeexception(ex);             }         }     }      public static void main(string[] args) throws interruptedexception {         final double d = 2.35;         final file f = new file(new file(system.getproperty("user.home"), "downloads"), "readme.txt");         final executorservice executorservice = executors.newsinglethreadexecutor();         final future<?> future = executorservice.submit(new socketreader());         new socketwriter(d, f).run();         read.set(false);         executorservice.shutdownnow();         try {             future.get();         } catch (executionexception ex) {             throw new runtimeexception(ex);         }     } } 

first send double serialised, sends contents of random file in downloads directory.

another thread, meanwhile, echos console.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -