c# - Sending large strings through TCP - Windows Phone -
i developed simple tcp client on windows phone, shown here on msdn
this working expected.
now, want send large base64 strings though client (for transferring images). but, when try send base64 strings client, receive portion of string @ server, due i'm not able generate entire image @ server.
the server side code receiving strings is: (edited)
ipaddress ipad = ipaddress.any; console.write("port no. (leave blank port 8001): "); string port; port = console.readline(); if (port == "") port = "8001"; /* initializes listener */ tcplistener mylist = new tcplistener(ipad, int.parse(port)); /* start listeneting @ specified port */ mylist.start(); console.writeline("\nthe server running @ port " + port); console.writeline("the local end point :" + mylist.localendpoint); console.writeline("\nwaiting connection....."); socket s = mylist.acceptsocket(); console.writeline("\nconnection accepted " + s.remoteendpoint); byte[] b = new byte[5 * 1024 * 1024]; // big size byte array, correct? string message = string.empty; int k = s.receive(b); console.writeline("\nrecieved..."); (int = 0; < k; i++) { message += convert.tochar(b[i]); console.write(convert.tochar(b[i])); } system.io.file.writealltext(@"message.txt", message); // write file asciiencoding asen = new asciiencoding(); s.send(asen.getbytes("the string recieved server.")); console.writeline("\n\nsent acknowledgement"); /* clean */ s.close(); mylist.stop();
i'm stuck here. please me.
i think problem client , not server. please assist me.
the class i've used in client can found @ msdn article referred above.
ps: i've tried increase values of timeout_milliseconds , max_buffer_size in class. did not help.
update:
here's client side code (look here on msdn reference):
// make sure can perform action valid data if (validateremotehost() && validateinput()) { // instantiate socketclient socketclient client = new socketclient(); // attempt connect echo server log(string.format("connecting server '{0}' on port {1} (echo) ...", txtremotehost.text, echo_port), true); string result = client.connect(txtremotehost.text, echo_port); log(result, false); byte[] bytearray = null; // attempt send our message echoed echo server log(string.format("sending '{0}' server ...", txtinput.text), true); if (checkbox1.ischecked == true) // checkbox image selection { // part send images using (memorystream ms = new memorystream()) { writeablebitmap wbitmp = new writeablebitmap((bitmapimage)image1.source); wbitmp.savejpeg(ms, (int)wbitmp.pixelwidth, (int)wbitmp.pixelheight, 0, 10); bytearray = ms.toarray(); string str = convert.tobase64string(bytearray); result = client.send(str); system.diagnostics.debug.writeline("\n\nmessge sent:\n\n" + str + "\n\n"); } } else { result = client.send(txtinput.text); } log(result, false); // receive response server log("requesting receive ...", true); result = client.receive(); log(result, false); // close socket connection explicitly client.close(); }
while ((recbytes = netstream.read(recdata, 0, recdata.length)) > 0) { fs.write(recdata, 0, recbytes); totalrecbytes += recbytes; }
edit: section of code has been reduced down only
int k = s.receive(b);
is bad: assumes data sends in 1 go, isn't how networking works.
two options:
- at start of string, include how long should.
- at end of string, have end symbol (a null perhaps) won't anywhere else in message
then while loop should keep going until either entire length found or end symbol found.
(also, sending base64 bad idea when can avoid it. why not send stream of raw bytes?)
[ed: portion no longer relevant](also, why server choosing save file [and delaying until server makes pick] - client should indicate save @ start, , server merely sanity checks it. [unless have reason not way])
edit: quick simple implementation of saying:
this server code
int k = s.receive(b); console.writeline("\nrecieved..."); (int = 0; < k; i++) { message += convert.tochar(b[i]); console.write(convert.tochar(b[i])); }
change to
while (true) { int k = s.receive(b); console.writeline("\nrecieved..."); (int = 0; < k; i++) { char bc = convert.tochar(b[i]); // wrong way of doing works guess meh. if (bc == ' ') { // you've struck end! out of infinite loop! goto endmyloop; } message += bc; console.write(bc); } } endmyloop:
this piece of client code
result = client.send(str);
change to
result = client.send(str + " ");
-- base64 can never have space in it, used mark end.
be warned if client errors (and doesn't send space @ end weird reason), code trapped in while loop forever (zero-cpu-usage infinite-wait)
Comments
Post a Comment