android - send and receive string and file over socket with streams -


i have app communicates on local network through socket other devices. in want transfer file , text. problem not know how both file , text , manage them in receive !

this text reciever :

 try {             outputstream = new dataoutputstream(socket.getoutputstream());             outputstream_external = outputstream;             inputstream = new bufferedreader(new inputstreamreader(socket.getinputstream()));             log("success set streams");         }         catch (ioexception e1) {             log("error: connection not stable, exit");             shutdown();         }         while (true) {             string message = null;             try {                 message = inputstream.readline();                 if (message == null) {                     return;                 }                 g.log(message);                 jsonobject object = new jsonobject(message);                 string command = object.getstring(command.command);                 g.log(message); 

this text sender:

public void sendcommand(string command) {         g.log("send command + " + command);         command = command.replace("\n", " ") + "\n";         if (outputstream == null) {             return;         }         final string commend = command;         thread thread = new thread(new runnable() {              @override             public void run() {                 // todo auto-generated method stub                 try {                     outputstream.write(commend.getbytes());                 }                 catch (ioexception e) {                     e.printstacktrace();                     g.log("sendcommand catch");                 }             }         });         thread.start();      } 

how can receive text , file ?

that's why there many application level network protocols, http, ftp, smtp.

in case, need 2 types of messages, 1 string, file. each message should comply predefined format. example,

[4 bytes message type] + [4 bytes message length] + [message content]

you construct message on sender side, , parse message on receiver side.

however, in cases, don't have reinvent wheel. search on internet find if there existing protocol suitable you.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -