sockets - Communication on 2 ports between 2 Android devices using WiFi Direct -
i creating application monitor movements in particular android device (client) , report such instances android device (server). also, under specific conditions, client take picture , transmit image server.
i using wifi direct setup connection between 2 devices. after using socket connections explained in wifi direct demo. using port 8988 send motion sensor events , using port 8987 send images capture.
on server side, using 2 different instances of same async task serversocket connecting different ports listen incoming messages. works fine long motion sensor events being sent across. first image capture being sent/received correctly. however, after server doesn't receive additional messages. tried having 2 different async task classes avoid having 2 instances of same class didn't work well. tried having 1 async task , intent service doesn't work.
this intentservice using send messages across server.
public class messagesender extends intentservice { public static final string extras_timeout = "timeout"; public static final string extras_address = "go_host"; public static final string extras_port = "go_port"; public static final string extras_data = "data"; private handler handler; public messagesender(string name) { super(name); } public messagesender() { super("messagetransferservice"); } @override public int onstartcommand(intent intent, int flags, int startid) { handler = new handler(); return super.onstartcommand(intent, flags, startid); } @override protected void onhandleintent(intent intent) { string host = intent.getextras().getstring(extras_address); socket socket = new socket(); int port = intent.getextras().getint(extras_port); byte[] data = intent.getextras().getbytearray(extras_data); int timeout = intent.getextras().getint(extras_timeout); try { socket.bind(null); socket.connect((new inetsocketaddress(host, port)), timeout); outputstream stream = socket.getoutputstream(); stream.write(data); } catch (final ioexception e) { handler.post(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "exception has occurred: " + e.getmessage(), toast.length_short).show(); } }); } { if (socket != null) { if (socket.isconnected()) { try { socket.close(); /*handler.post(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "socket connection closed now..", toast.length_short).show(); } });*/ } catch (ioexception e) { // give e.printstacktrace(); } } } } } }
this async task on server starts listeners on 2 ports (8987 , 8988) receiver information of motion sensor events , images.
public class messagereceiver extends asynctask<void, void, string> { private context context; private int port; private bitmap mbitmap; public messagereceiver(context context, int port) { this.context = context; this.port = port; } @override protected string doinbackground(void... params) { try { serversocket serversocket = new serversocket(port); socket client = serversocket.accept(); inputstream inputstream = client.getinputstream(); string returnstring = ""; if (port == mainactivity.port_sensor_comm) { // } else if (port == mainactivity.port_image_comm) { //do } serversocket.close(); return returnstring; } catch (exception e) { return "exception occurred:" + e.getmessage(); } } @override protected void onpostexecute(string result) { boolean startnewtask = true; if (port == mainactivity.port_sensor_comm) { //do } else if (port == mainactivity.port_image_comm) { //do } //doing start listening new messages again new messagereceiver(context, port).executeonexecutor(asynctask.thread_pool_executor); } @override protected void onpreexecute() { } }
i wondering whether android wifidirect allows parallel communication between 2 devices on different ports. searched docs could'nt find help. doing wrong? correct method accomplish trying do? appreciated. looking.