Sending strings and File from Android Client to C# server -


i trying send files bunch of strings android client c# server. strings, among other things, contain details regards file being sent eg: file size, file name, etc. issue having file bytes not received original file cannot reconstructed @ destination though can retrieve strings.

my c# server code

                        while (true)         {             socket socket = listener.acceptsocket();             setstatus(socket.remoteendpoint + " connected");              try             {                 // open stream                 networkstream stream = new networkstream(socket);                 system.io.streamreader sr = new streamreader(stream);                  //get string data                 //********************************************                 teststr = sr.readline();                 setstatus(teststr);                   filesize = sr.readline();                 long filesizelong = convert.toint64(filesize);                 int length = (int)filesizelong;                 setstatus("file size: " + length + " bytes");                 //********************************************                   //read bytes buffer                 byte[] buffer = new byte[length];                  int toread = (int)length;                 int read = 0;                 while (toread > 0)                 {                     int nochars = stream.read(buffer, read, toread);                     read += nochars;                     toread -= nochars;                 }                  setstatus("file recieved. total bytes: " + convert.tostring(buffer.length));                 setstatus("saving file");                 string recievedpath = "c:\\test\\";                 binarywriter bwrite = new binarywriter(file.open(recievedpath + "test.png", filemode.create));                 bwrite.write(buffer);                 setstatus("file saved to: " + recievedpath);                 bwrite.flush();                 bwrite.close();                 stream.flush();                 stream.close();             }             catch (exception e)             {                 setstatus(e.message);             }              setstatus("disconnected");             socket.close();         } 

my android client code

                    file file = new file(configstr[2]); //create file instance             try {               client = new socket(configstr[0], integer.valueof(configstr[1]));               //read file              fileinputstream = new fileinputstream(file);               outputstream = client.getoutputstream();               //output database details stream              //*****************************************               //holds string before conversion bytes              string text = "";               text = "test\n";              outputstream.write(text.getbytes());               text = string.valueof(file.length()) + "\n";              outputstream.write(text.getbytes());              //*****************************************               outputstream.flush();               bytearrayoutputstream bos = new bytearrayoutputstream();              byte[] b = new byte[1024];              int bytesread = 0;              while ((bytesread = fileinputstream.read(b)) != -1) {                 bos.write(b, 0, bytesread);              }              byte[] bytes = bos.tobytearray();              outputstream.write(bytes);               outputstream.flush();               return true;             } catch (unknownhostexception e) {              e.printstacktrace();              return false;             } catch (ioexception e) {              e.printstacktrace();              return false;             } 

note: code works if send 1 string, eg: if send file size.

any better way of getting work trying send many strings through stream binary data file.

bufferedwriter flush() @ end of every write() solved issue me.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -