我该如何进行测试传输在本地无线网络在两部手机之间的数据? [英] How can I transfer data between two phones in a local wireless network for testing?

查看:186
本文介绍了我该如何进行测试传输在本地无线网络在两部手机之间的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我设法建立一个聊天应用程序,通过本地无线网络的工作。什么是对通信传输的最佳选择? (假设每个手机都有它的本地IP的唯一信息)套接字?

For testing purposes, I'm managing to develop a chat app to work through local wireless network. What would be the best options for the communication transfer? (assuming that the only information that each phone have it's the local IP) Sockets?

我提到聊天,因为它只是用于测试,我的目的是像一些变量的值传递数据,阵列等。

I referred chat because It's just for testing, my purpose is to transfer data like value of some variables, arrays etc.

鸭preciate任何想法/建议:)

Appreciate any idea/suggestion :)

推荐答案

使用简单的插座和缓冲流。制定恩,你需要codeD的消息一组JSON的。利用这些信息包装你的数据,并通过套接字发送。你需要结合的方式来表示JSON消息,因为从经验有多长,有滞后/差距有时,在信息传输的中间发生/接收。我们所做的是前4个字节(例如,一个Int)的消息表明后续消息的(字节)的长度。例如。如果你有JSON字符串30个字节,发送30(字节)第一,那么实际的消息,使得在插座所有的传输是对长度的有效载荷组合。

Use simple Sockets and Buffered streams. Formulate a set of JSON encoded messages that you need. Wrap your data using those messages and send it through the socket. You need to incorporate a way to indicate how long the JSON message is since from experience, there are lag/gaps sometimes that happen in the middle of message transmit/receive. What we did was the first 4 bytes(e.g. an Int) of the message indicates the (byte)length of the succeeding message. E.g. if you have json string 30 bytes long, you send 30(in bytes) first then the actual message such that all transmission over the socket is by pairs of length-payload combinations.

就个人而言,我已经在Android项目中做到了这一点。这个过去的的ObjectInputStream / ObjectOutputStream的路线的优点是绝对更轻和可互操作与其它平台(例如iOS的,等等)。我们甚至采用分块的base64 EN codeD JSON有效载荷(所以所有的消息仍然在JSON)。

Personally, I've done this in an android project. The advantage of this over going the ObjectInputStream/ObjectOutputStream route is that is definitely lighter and interoperable with other platforms(e.g. iOS, etc). We even did file transfer using chunked base64 encoded json payloads(so all messages are still in JSON).

我不知道长度有效载荷对是去了解它虽然最好/唯一途径。

I am not sure the length-payload pair is the best/only way to go about it though.

这篇关于我该如何进行测试传输在本地无线网络在两部手机之间的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆