简体   繁体   中英

How to send data to the same application installed on other android device using wifi

i have an application on my android device like "A" and same application installed on the other android device like "B", now i want to send data from app "A" to app "B" using WIFI service. so please suggest me how can i implement this feature.

i tried many times to get help from google but all is vain. it is possible from WIFI direct or NFC.

You could use a simple p2p architecture.

You will need to use this , this and a pair of streams that works with the kind of data you need to send, like this .

On sender side:

Socket s = new Socket(IP,PORT);
s.connect();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.write("hello".toByteArray());

Then on receiver side:

ServerSocket ss = new ServerSocket(PORT);
Socket s = ss.accept(); //This call will block execution, use separate thread
DataInputStream dis = new DataInputStream(s.getInputStream);
byte[] data = dis.read();

With this you can send and receive bytes, just use the stream that works with your data type.

Of course, once connection is established, both clients could send/write, just make the appropiate Input/Output Stream.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM