Android to Java TCP/IP reads too slow -
so, i've been creating wireless mouse app utilizes bluetooth , wifi (the user decides), i've decided go udp tcp connection noticed 1 of mainstream mouse apps uses tcp instead of udp.
my problem: sending multiple byte arrays on tcp-ip server end feels though there lag, there way potentially speed how fast receiving byte arrays?
server code involving receiving:
socket client = null; bufferedinputstream bis = null; try { client = serversocket.accept(); } catch (ioexception e) { e.printstacktrace(); } try { bis = new bufferedinputstream(client.getinputstream()); } catch (ioexception e) { e.printstacktrace(); } byte data[] = new byte[2]; if (bis != null) { try { while(alive && (bis.read(data)) != -1) { system.out.println(data[0] + " " + data[1]); pointerinfo = mouseinfo.getpointerinfo(); point b = a.getlocation(); int x = (int)b.getx(); int y = (int)b.gety(); dx = data[0]; dy = data[1]; newx = x + dx; newy = y + dy; if(dx == -98 && dy == -98) { // right click r.mousepress(inputevent.button3_down_mask); r.mouserelease(inputevent.button3_down_mask); } else if (dx == -99 && dy == -99) { // left click r.mousepress(inputevent.button1_down_mask); r.mouserelease(inputevent.button1_down_mask); } else if (dx == -97 && dy == -97) { // middle click r.mousepress(inputevent.button2_down_mask); r.mouserelease(inputevent.button2_down_mask); main.wifidisconnect.doclick(); } else { if (!recieve && (dx != 0 || dy != 0)) { // no current thread , non empty values - start new 1 newx = x + dx; newy = y + dy; = 0; recieve = true; mmthread = new thread(new runnable() { @override public void run() { while (recieve){ r.mousemove(newx + * dx, newy + * dy); r.delay(8); i++; } } }); mmthread.start(); } else if (recieve && dx == 0 && dy == 0) { recieve = false; try { mmthread.join(); } catch (interruptedexception e) { e.printstacktrace(); } mmthread = null; } else { newx = x + dx; newy = y + dy; = 0; } } }
client code (android) involving sending:
public class sendtask extends asynctask<byte, void, void> { @override protected void doinbackground(byte ...bytes) { try { byte x = bytes[0].bytevalue(); byte y = bytes[1].bytevalue(); system.out.println("message sending: " + x + " " + y); byte buf[] = {x, y}; bos.write(buf); //buffered output stream bos.flush(); } catch (unknownhostexception e) { e.printstacktrace(); } catch (socketexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } }
first, network sniffer wireshark , find out going on. it's impossible know without that.
but speculate... tcp has called "nagle's algorithm" dealing small packets. basically, delays transfer of small packet on chance more data ready send , can aggregated single larger packet, overall benefit being more efficient network connection @ cost of milliseconds.
you can set socket option tcp_nodelay
disable , have written bytes sent immediately.
Comments
Post a Comment