简体   繁体   English

我应该使用TCP套接字还是HTTP发布将文件发送到Android应用中的服务器

[英]Should I use TCP socket or HTTP post to send file to server in Android app

我有一个处理10秒音频剪辑的应用程序,想知道使用自定义TCP套接字将处理后的麦克风录制的音频剪辑发送到服务器比使用HTTP更快吗?

I don't think anyone would be able to measure a difference either way. 我认为任何人都无法以任何方式衡量差异。

If you send the file in one request the overhead of http will be quite small. 如果您在一个请求中发送文件,则http的开销将很小。 Normally the overhead on http is mostly creating a large number of connections but since you will only be making one you should be in the clear. 通常,http上的开销通常会创建大量连接,但是由于您只会建立一个连接,因此您应该很清楚。

It depends heavily on the purpose of your transfer. 这在很大程度上取决于您转移的目的。

If you are transmitting, playing, and discarding the sound, then use UDP transfer. 如果要传输,播放和丢弃声音,请使用UDP传输。

If you are sending the file as a binary blob file, then both protocols are being used (remember that HTTP is built over TCP/IP) 如果您将文件作为二进制Blob文件发送,则将同时使用两种协议(请记住,HTTP是基于TCP / IP构建的)

If you are sending the file directly over a TCP connection, the overhead is marginally smaller then the HTTP header added. 如果直接通过TCP连接发送文件,则开销比添加的HTTP标头要小一些。 (I would suggest around 30~10% increase in binary transfers, from my experience) (根据我的经验,我建议二进制传输大约增加30%到10%)

Edit: 编辑:

Like you said it pretty much gets discarded immediately after sending.how will I guarantee the arrival of the file though without TCP. 就像您说的那样,发送后几乎立即将其丢弃。尽管没有TCP,我如何保证文件的到达。 Would I need to implement my own acknowledgements? 我需要执行自己的确认吗?

You will not guarantee the arrival. 您将不能保证到达。 That is basically the difference between TCP and UDP. 这基本上就是TCP和UDP之间的区别。 If your purpose is to be "stream-like" behavior, and you are fine with loosing some part of the content, then UDP is great, since a "second" worth of information lost is not relevant to the continuation of a conversation, in a phone, this would be similar to hiccups or sharp noises in the call. 如果您的目的是“类似流”的行为,并且可以丢失部分内容,那么UDP很好,因为丢失的“第二”价值信息与对话的继续无关。电话,这类似于通话中出现打h或尖锐的声音。

However, if you are required to ensure that every bit and byte must reach its destination, then TCP is necessary. 但是,如果需要确保每个位和字节都必须到达其目的地,则必须使用TCP。

Finally, regarding to HTTP and TCP, TCP will always be "smaller" than HTTP, but overall their sizes will not be significantly different. 最后,关于HTTP和TCP,TCP总是比HTTP“小”,但是总体上它们的大小不会有显着差异。 Specially if you intend on performing some HTTP funcionality that TCP does not directly implement. 特别是如果您打算执行一些TCP不能直接实现的HTTP功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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