iOS client-server app: HTTP or TCP/IP?

后端 未结 5 539
栀梦
栀梦 2021-02-02 02:48

I am about to start designing/developing a client-server iOS app. I am leaning towards using HTTP requests to get/post data from/to a server, but want to make sure this is the r

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 03:33

    In terms of which to go with, you should stick with HTTP unless you know how to guarantee security over a socket connection and can properly handle timeouts etc.

    What he means by sending only the exact data you need is, HTTP is a protocol with several layers of abstraction between the message you're trying to send and the envelope it's being sent in.

    Example of an HTTP request:

    (asking for data)
    GET /dumprequest HTTP/1.1
    Host: djce.org.uk
    User-Agent: Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip, deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Connection: keep-alive
    
    (receiving data)
    HTTP/1.1 200 OK
    Date: Tue, 13 Dec 2011 01:09:14 GMT
    Server: Apache/2.2.9 (Ubuntu) DAV/2 SVN/1.5.1 PHP/5.2.6-2ubuntu4.6 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g mod_perl/2.0.4 Perl/v5.10.0
    Content-Location: dumprequest.pp
    Vary: negotiate
    TCN: choice
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8
    
    *a rather large HTML document here, with data somewhere in the body*
    

    Example of a socket request:

    (asking for data)
    poll
    
    (receiving data)
    *data*
    

    That's much more simplified than what you would do with a socket in reality (you may create your own serialization format for requests to keep them compact but you're almost certainly not going to only have 1 'poll' command), but you get the idea. You discard a whole bunch of extra stuff which has to be parsed, and get down to just the raw data.

    Of course, in reality any packet you send will get wrapped in an PPP/Ethernet frame, then in an AAL5 frame if you use ADSL etc. This is what @hotpaw2 is saying. The real world efficiency of sockets over TCP/IP vs HTTP over TCP/IP is sometimes noticeable but other times not. It depends entirely on your use case (how frequently things need to be sent, how big the packets will be).

提交回复
热议问题