search

Home  >  Q&A  >  body text

android - 选择http协议还是tcp协议

准备做个安卓应用,服务端用golang写的,客户端语需要和服务端通信,我是用tcp好呢,还是用http协议呢?数据量挺大,哪个协议的效率更高呢,综合开发效率和传输效率

PHP中文网PHP中文网2773 days ago715

reply all(4)I'll reply

  • 阿神

    阿神2017-04-17 13:37:19

    HTTP is an application layer protocol, and TCP is a transport layer protocol (located below the application layer). The analogy is not appropriate.
    But I guess the poster wants to compare "standard HTTP protocol" or "custom protocol (based on TCP Socket)".

    Generally speaking, it is recommended to use HTTP protocol for mobile applications, which has many advantages:
    HTTP is mature
    HTTP has almost become a universal web standard. Web Services, REST, Open API, OAuth, etc. are all based on the HTTP protocol. It is no longer just a transmission standard for Hyper Text. Almost all data transmission (multimedia, XML, JSON) can use HTTP.
    Backend reuse
    Because many applications, in addition to mobile versions, also have web versions and even desktop versions.
    The front-end and back-end interactions in the Web version, whether it is a page request or an AJAX request, all use the standard HTTP protocol. Then other clients have no reason to redesign a protocol.
    HTML 5 applications
    Many mobile products now use or semi-use HTML 5 technology, so the interaction with the server has returned to AJAX. Needless to say, HTTP is still indispensable.
    But there are also some limitations. For example, the following scenarios are not suitable for HTTP protocol:
    Real-time data push
    In addition to the standard Apple message push center provided by iOS development, other mobile products may still need to use Socket long connections to ensure real-time communication.
    The more common one is the XMPP protocol used by many instant messaging software.
    Streaming
    Suitable for audio playback, video playback, voice conferencing, etc., generally RTMP protocol may be used.

    Http is the upper layer protocol of TCP. Http is based on TCP, so if you use HTTP, it is equivalent to using TCP

    So, comparing the pros and cons of Http and TCP is a non-existent problem.

    Of course, this is a very good question. It asks about custom protocols based on tcp.

    In fact, from a macro level, I have already answered this question myself.

    Why do you need to customize the protocol? It's very simple. The http protocol cannot meet the needs and has to be customized.
    In other words, the custom protocol can meet many needs that the http protocol cannot.
    So what needs cannot be met by the http protocol?
    This is also very simple. You can look up the definition of the http protocol to see what kind of packaging and definitions it provides. Those that fall outside of it are unsatisfactory. If you really want to go into details, there are too many. , for example:

    For example: http is a simplex blocking protocol. If you need a full-duplex, non-blocking two-way transmission, then http cannot satisfy it

    For example: http definition provides many kinds of request methods, from get to post, I will not list them one by one, but none of the request response modes you need and the various definitions it defines can achieve the request response modes you need. You just need to customize the protocol

    For example: http defines its own header. If you think transmission efficiency is extremely important, such a header is too bloated, and you also need to customize the protocol

    If http can fully meet your needs, then why do you need to customize the protocol? It is obviously a good choice to use a mature protocol immediately.

    Now that REST has come out, the complexity and bloat of SOAP in the past has been changed. The HTTP protocol itself has been expanding, so it is applicable to a wider range and is easier to use. There are also fewer scenarios and requirements that require custom protocols.

    If you want to compare the pros and cons from a micro level, at least you have to tell what this custom protocol is?
    There are so many custom protocols on TCP. Which one should I use for comparison?

    TCP long links are always connected and never disconnected. If it is TCP:
    The server side is not easy to expand, which tests the access capability of a single server. The server cluster is not easy to set up.
    On the client side, the thread that handles socket connections is responsible for doing various things. The logic of all network protocols is concentrated here, and the structure is not easy to match. As for http, the structure is completely different.

    The difference lies in the development cost. http has a large number of ready-made structures, servers, and databases. If there is a problem, it will not completely collapse, and the debugging cost is low.
    TCP must customize the protocol and then handle it by itself; implement the server and monitor the port by itself; when encountering problems, create a series of debugging methods by itself. If you make your own wheels, the development cost is an order of magnitude higher.

    I happen to be using the http protocol recently, which was done by one person before taking over. There is no way to rewrite the code. Custom protocols based on sockets are not suitable for rapid iteration of mobile development, unless there are some relatively low-level requirements. It is estimated that some like WeChat may customize the protocol, otherwise the bandwidth load will be too high. But I don’t know the specifics.

    So where http can be used, don’t use tcp. However, some things must use tcp, such as online games, and there is no way around it.

    A very important advantage of the HTTP protocol is firewall traversal.
    If there is a security device between the client and the server, the only port that may be open is TCP:80.

    This is especially true for mobile development. You don’t want users to complain all day long that they can’t access your server.

    This article introduces it in detail

    By the way, let me advertise, Qiniu is a pioneer in domestic golang back-end service development. Upload, download, and data processing interfaces all use the http protocol

    You can also mix them. This practice of Ctrip is very useful for reference

    Many Android frameworks are based on http, such as android-async-http, okhttp, AndroidAsync, volley...etc.

    In summary, it is recommended to use the http protocol unless you have very special needs

    reply
    0
  • 阿神

    阿神2017-04-17 13:37:19

    Are you sure these two protocols can be compared together?

    TCP is the underlying communication protocol, which defines the specification of data transmission and connection methods
    HTTP is an application layer protocol, which defines the specification of the content of transmitted data
    The data in the HTTP protocol is transmitted using the TCP protocol, so if you support HTTP, you must also support TCP

    http Wikipedia
    TCP Wikipedia

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:37:19

    I personally recommend TCP to meet your basic needs

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:37:19

    I guess, the university only knows that tcp is compared with udp.

    reply
    0
  • Cancelreply