开发者

How can I transfer data between two phones in a local wireless network for testing?

开发者 https://www.devze.com 2023-04-04 09:24 出处:网络
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 tha

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.

开发者_JS百科

Appreciate any idea/suggestion :)


Probably one of the simplest options would be to just use a Socket, wrap its streams in a PrintWriter and a LineNumberReader and then send JSON-encoded data. Should be more than adequate for small variables/arrays.


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.

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.


Use an existing chat library (like Smack) which provides an implementation of a standardized chat protocol (XMPP) and forget about the low level details altogether.

Note: FTP is for file transfers, whatever comms you use it will almost certainly be socket based using TCP. The only question then is whether you use raw TCP and write the protocol yourself, or use an existing protocol and implementation so you can worry about the application details.

0

精彩评论

暂无评论...
验证码 换一张
取 消