Sending binary data multiple times using Sockets in Java/Android -
i need send binary data multiple times java sockets on android devices. simple object exports run() , send() methods.
public class gpcsocket { private socket socket; private static final int serverport = 9999; private static final string server_ip = "10.0.1.4"; public void run() { new thread(new clientthread()).start(); } public int send(byte[] str) { try { final bufferedoutputstream outstream = new bufferedoutputstream(socket.getoutputstream()); outstream.write(str); outstream.flush(); outstream.close(); } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } return str.length; } class clientthread implements runnable { @override public void run() { try { inetaddress serveraddr = inetaddress.getbyname(server_ip); socket = new socket(serveraddr, serverport); } catch (unknownhostexception e1) { e1.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } } } }
for android programming, use scaloid, , code send binary data multiple times. count
given parameter. result
byte[array] type data, , gpc
initialized in oncreate()
method gpc = new gpcsocket()
.
gpc.run() (i <- 1 count) { val length = gpc.send(result) toast(s"sent: $i $length") }
the issue when try send data multiple times, receiver receives 1 packet. shown in server (receiver) side when send information 5 times:
55:2015-03-21 03:46:51 86: <received data> 10.0.1.27 wrote: 56:2015-03-21 03:46:51 0: 10.0.1.27 wrote: 57:2015-03-21 03:46:51 0: 10.0.1.27 wrote: 58:2015-03-21 03:46:51 0: 10.0.1.27 wrote: 59:2015-03-21 03:46:51 0:
my questions are
- why this? why receiver receives 1 time? sender shows sends information 5 times.
- do need use run() 1 time before sending multiple times? or, have call run() whenever use send()?
this server side code in python:
import socketserver time import gmtime, strftime count = 1 class mytcphandler(socketserver.baserequesthandler): """ requesthandler class our server. instantiated once per connection server, , must override handle() method implement communication client. """ def handle(self): # self.request tcp socket connected client self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) global count count += 1 print "%d:%s %d:%s" % (count, strftime("%y-%m-%d %h:%m:%s", gmtime()), len(self.data), self.data) # send same data, upper-cased self.request.sendall(self.data.upper()) if __name__ == "__main__": host, port = "10.0.1.4", 9999 # create server, binding localhost on port 9999 server = socketserver.tcpserver((host, port), mytcphandler) # activate server; keep running until # interrupt program ctrl-c server.serve_forever()
from post: sending bytearray using java sockets (android programming) , how fix java.net.socketexception: broken pipe?, seems send() method wrong.
close resources when i'm done using them
i modified send() simple create socket , close when i'm done using it.
public int send(byte[] str) throws ioexception { inetaddress serveraddr = inetaddress.getbyname(server_ip); socket = new socket(serveraddr, serverport); out = socket.getoutputstream(); out.write(str); out.flush(); out.close(); socket.close(); return str.length; }
modification in caller code
the scala code calls modified invoke send() method.
// gpc.run() (i <- 1 count) { ... val length = gpc.send(result) println(s"sent: $i $length") } ... in initializer gpc = new gpcsocket()
change policy mode in android
hints post: android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145), code should added android device prevent e/androidruntime﹕ fatal exception: main process: scaloid.example, pid: 1791 android.os.networkonmainthreadexception
error.
val policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy);
Comments
Post a Comment