{"id":1844,"date":"2019-01-21T11:54:23","date_gmt":"2019-01-21T03:54:23","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1844"},"modified":"2020-12-02T12:09:44","modified_gmt":"2020-12-02T04:09:44","slug":"socket%e5%8f%8a%e5%9f%ba%e6%9c%ac%e5%ba%94%e7%94%a8%ef%bc%88tcp%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2019\/01\/21\/1154231844.html","title":{"rendered":"Socket\u53ca\u57fa\u672c\u5e94\u7528\uff08TCP\uff09"},"content":{"rendered":"<h3>1\u3001\u4ec0\u4e48\u662fSocket\uff08\u63d2\u5ea7\u3001\u5957\u63a5\u5b57\uff09<\/h3>\n<ul>\n<li>IP\uff1a\u7f51\u7edc\u4e2d\u80fd\u6807\u8bc6\u552f\u4e00\u4e00\u53f0\u4e3b\u673a\uff1b<\/li>\n<li>Port\uff1a\u4e3b\u673a\u4e2d\u6807\u8bc6\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\uff1b<\/li>\n<li>Socket\uff1aIP+Port\uff1b<\/li>\n<li>socket\u5728Linux\u4e2d\u4e3a\u4e03\u79cd\u57fa\u672c\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a(\u666e\u901a-\u3001\u76ee\u5f55d\u3001\u8fde\u63a5l\u3001\u7ba1\u9053p\u3001\u5b57\u7b26\u8bbe\u5907c\u3001\u5757\u8bbe\u5907b\u3001\u5957\u63a5\u5b57s)<\/li>\n<\/ul>\n<h3>2\u3001Socket\u7684\u57fa\u672c\u7279\u6027<\/h3>\n<ul>\n<li>socket\u662f\u6210\u5bf9\u51fa\u73b0\uff0c\u7ed1\u5b9aip+\u7aef\u53e3<\/li>\n<li>\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4e24\u4e2a\u7f13\u51b2\u533a\uff0c\u5168\u53cc\u5de5\u8bfb\u5199<\/li>\n<\/ul>\n<h3>3\u3001C\/S\u6a21\u578b<\/h3>\n<ul>\n<li>\u5927\u5c0f\u7aef\u95ee\u9898\uff1a\n<ul>\n<li>\u5c0f\u7aef\uff1a\u4f4e\u5730\u5740-\u5b58\u4f4e\u4f4d\u3001\u9ad8\u5730\u5740-\u5b58\u9ad8\u4f4d<\/li>\n<li>\u5927\u7aef\uff1a\u4f4e\u5730\u5740-\u5b58\u9ad8\u4f4d\u3001\u9ad8\u5730\u5740-\u5b58\u4f4e\u4f4d<\/li>\n<\/ul>\n<\/li>\n<li>\u7f51\u7edc\u6570\u636e\u6d41\u5e94\u91c7\u7528\u5927\u7aef\u5b57\u8282\u5e8f\uff0c\u5373\u4f4e\u5730\u5740\u9ad8\u5b57\u8282\n<pre><code class=\"language-c line-numbers\">#include &lt;arpa\/inet.h&gt;\n\/\/ h\uff1ahost\u3001n\uff1anetwork\u3001l\uff1a\u8868\u793a32\u4f4d\u957f\u6574\u6570\u3001s\uff1a\u8868\u793a16\u4f4d\u77ed\u6574\u6570\n\nuint32_t htonl(uint32_t hostlong);\nuint16_t htons(uint16_t hostshort);\nuint32_t ntohl(uint32_t netlong);\nuint16_t ntohs(uint16_t netshort);\n<\/code><\/pre>\n<\/li>\n<li>ip\u5730\u5740\u76f8\u5173\u8f6c\u6362\u51fd\u6570\n<pre><code class=\"language-c line-numbers\">#include &lt;arpa\/inet.h&gt;\n\/\/ \u4e3b\u673a\u5230\u7f51\u7edc\nint inet_pton(int af, const char *src, void *dst);\n\/\/ \u7f51\u7edc\u5230\u4e3b\u673a\nconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>4\u3001Socket\u6570\u636e\u7ed3\u6784\u4e0e\u76f8\u5173t\u51fd\u6570<\/h3>\n<pre><code class=\"language-c line-numbers\">\/\/ man 7 ip\u67e5\u770b\nstruct sockaddr_in {\n    sa_family_t    sin_family; \/* address family: AF_INET *\/\n    in_port_t      sin_port;   \/* port in network byte order *\/\n    struct in_addr sin_addr;   \/* internet address *\/\n};\n\/* Internet address. *\/\nstruct in_addr {\n    uint32_t       s_addr;     \/* address in network byte order *\/\n};\n\n\n#include &lt;sys\/socket.h&gt;\n\nint socket(int domain, int type, int protocol);\n\n       socket()  creates an endpoint for communication and returns a file descriptor that refers to that endpoint.  The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.\n\n       The domain argument specifies a communication domain; this selects the protocol family which will be used for communication.   These  families  are defined in &lt;sys\/socket.h&gt;.  The currently understood formats include:\n\n       Name                Purpose                          Man page\n       AF_UNIX, AF_LOCAL   Local communication              unix(7)\n       AF_INET             IPv4 Internet protocols          ip(7)\n       AF_INET6            IPv6 Internet protocols          ipv6(7)\n       AF_IPX              IPX - Novell protocols\n       AF_NETLINK          Kernel user interface device     netlink(7)\n       AF_X25              ITU-T X.25 \/ ISO-8208 protocol   x25(7)\n       AF_AX25             Amateur radio AX.25 protocol\n       AF_ATMPVC           Access to raw ATM PVCs\n       AF_APPLETALK        AppleTalk                        ddp(7)\n       AF_PACKET           Low level packet interface       packet(7)\n       AF_ALG              Interface to kernel crypto API\n\n       The socket has the indicated type, which specifies the communication semantics.  Currently defined types are:\n\n       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based byte streams.  An out-of-band data transmission mechanism may be supported.\n\n       SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a fixed maximum length).\n\n       SOCK_SEQPACKET  Provides  a  sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.\n\n       SOCK_RAW        Provides raw network protocol access.\n\n       SOCK_RDM        Provides a reliable datagram layer that does not guarantee ordering.\n\n       SOCK_PACKET     Obsolete and should not be used in new programs; see packet(7).\n\n       Some socket types may not be implemented by all protocol families.\n\n       Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the following values, to modify the behavior of socket():\n\n       SOCK_NONBLOCK   Set the O_NONBLOCK file status flag on the new open file description.  Using this flag saves extra calls to fcntl(2) to achieve the same result.\n\n       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.  See the description of the O_CLOEXEC flag in open(2) for  reasons why this may be useful.\n\nint bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);\n\nstruct sockaddr {\n    sa_family_t sa_family;\n    char        sa_data[14];\n}\n\n\/\/ \u540c\u65f6\u5efa\u7acb\u8fde\u63a5\u7684\u6570\uff08\u5904\u4e8e3\u6b21\u63e1\u624b\u7684\u94fe\u63a5\u6570\u548c\uff09\nint listen(int sockfd, int backlog);\n\n\/\/ struct sockaddr *addr\u4e3a\u4f20\u51fa\u53c2\u6570\uff0csocklen_t*addrlen\u4e3a\u4f20\u5165\u4f20\u51fa\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684socket\u6587\u4ef6\u63cf\u8ff0\u7b26\nint accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);\n\n\/\/ \u8fde\u63a5\nint connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);\n\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2020\/12\/174559B6-74C8-41E1-905C-7C581A86BFB9.png\"\/><\/p>\n<p>5\u3001Socket\u901a\u8baf\u7684\u57fa\u672c\u6b65\u9aa4<\/p>\n<ul>\n<li>Server\n<ul>\n<li>socket( ) \u2014> bind( ) \u2014> listen( ) \u2014> accept( ) \u2014> read( ) \u2014> write( ) \u2014> read( ) \u2014> close( )<\/li>\n<\/ul>\n<\/li>\n<li>Client\n<ul>\n<li>socket( ) \u2014> connect( ) \u2014> write( ) \u2014> read( ) \u2014> close( )<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>6\u3001\u793a\u4f8bDemo<\/h3>\n<p><code>server.c<\/code><\/p>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n#include &lt;ctype.h&gt;\n#include &lt;strings.h&gt;\n\n#define SOCKET_PORT 3000\n\nint main(){\n\n    \/\/ 1\u3001socket\n    int sfd = socket(AF_INET, SOCK_STREAM, 0);\n    if (sfd &lt; 0){\n        perror(\"socket error\");\n        exit(1);\n    }\n    \/\/ 2\u3001bind\n    struct sockaddr_in addr;\n    addr.sin_family = AF_INET;\n    addr.sin_port = htons(SOCKET_PORT);\n    addr.sin_addr.s_addr = htonl(INADDR_ANY);\n    int ret = bind(sfd, (struct sockaddr *)&amp;addr, sizeof(addr));\n    if (ret &lt; 0){\n        perror(\"bind error\");\n        exit(1);\n    }\n    \/\/ 3\u3001listen\n    ret = listen(sfd, 128);\n    if (ret &lt; 0){\n        perror(\"listen error\");\n        exit(1);\n    }\n    \/\/ 4\u3001accept\n    struct sockaddr_in c_addr;\n    socklen_t addrlen = sizeof(struct sockaddr_in);\n    int cfd = accept(sfd, (struct sockaddr *)&amp;c_addr, &amp;addrlen);\n\n    \/\/ \u6253\u5370\u5ba2\u6237\u7aef\u4fe1\u606f\n    char c_addr_s[BUFSIZ];\n    bzero(c_addr_s, sizeof(c_addr_s));\n    printf(\"Client:%s, port:%d\\n\",\n            inet_ntop(AF_INET, &amp;c_addr.sin_addr.s_addr, c_addr_s, sizeof(c_addr_s)),\n            ntohs(c_addr.sin_port));\n\n    \/\/ 5\u3001read\n    char buf[BUFSIZ];\n    while(1){\n        ssize_t n = read(cfd, buf, BUFSIZ);\n        \/\/ 5.1\u3001\u5904\u7406\n        int i;\n        for (i = 0; i &lt; n; i++){\n            buf[i] = toupper(buf[i]);\n        }\n        \/\/ 6\u3001write\n        ret = write(cfd, buf, n);\n        if (ret &lt; 0){\n            perror(\"write error\");\n            exit(1);\n        }\n    }\n    \/\/ 7\u3001close\n    close(sfd);\n    close(cfd);\n    return 0;\n}\n<\/code><\/pre>\n<p><code>client.c<\/code><\/p>\n<pre><code class=\"language-c line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;string.h&gt;\n\n#define SERVER_PORT 3000\n#define SERVER_IP \"10.211.55.6\"\nint main(){\n    \/\/ 1\u3001socket\n    int sfd = socket(PF_INET, SOCK_STREAM, 0);\n    if (sfd &lt; 0){\n        perror(\"socket error\");\n        exit(1);\n    }\n    \/\/ 2\u3001connect\n    \/\/ \u521b\u5efa\u5957\u63a5\u5b57\u7ed3\u6784\u4f53\n    struct sockaddr_in svr_addr;\n    svr_addr.sin_family = PF_INET;\n    svr_addr.sin_port = htons(SERVER_PORT);\n    \/\/ \u6e050\u64cd\u4f5c\n    memset(&amp;svr_addr.sin_addr.s_addr, 0, sizeof(svr_addr.sin_addr.s_addr));\n    \/\/ \u5c06\u70b9\u5206\u5341\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5b57\u8282\u5e8f\n    inet_pton(PF_INET, SERVER_IP, &amp;svr_addr.sin_addr.s_addr);\n    \/\/ \u53d1\u8d77\u7f51\u7edc\u8fde\u63a5\n    int ret = connect(sfd, (struct sockaddr *)&amp;svr_addr, sizeof(svr_addr));\n    if (ret &lt; 0){\n        perror(\"connect error\");\n        exit(1);\n    }else{\n        printf(\"\u670d\u52a1\u5668\u8fde\u63a5\u6210\u529f\uff01\\n\");\n    }\n    char buf[BUFSIZ];\n    while(1){\n        \/\/ \u8bfb\u53d6\u8f93\u5165\u6d41\n        fgets(buf, sizeof(buf), stdin);\n        \/\/ \u5199\u5165\u5957\u63a5\u5b57\n        write(sfd, buf, strlen(buf));\n        \/\/ \u8bfb\u53d6\u670d\u52a1\u5668\u54cd\u5e94\n        int n = read(sfd, buf, sizeof(buf));\n        \/\/ \u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u6d41\n        write(STDOUT_FILENO, buf, n);\n    }\n    close(sfd);\n    return 0;\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-cmd line-numbers\">\/\/ \u670d\u52a1\u7aef\nparallels@ubuntu:~\/Linux\/socket$ .\/server.out\nClient:10.211.55.2, port:59929\n^C\nparallels@ubuntu:~\/Linux\/socket$\n\n\/\/ \u5ba2\u6237\u7aef\nyusian@SianMac2:~\/Linux\/socket$ .\/client.out\n\u670d\u52a1\u5668\u8fde\u63a5\u6210\u529f\uff01\nabc\nABC\nhello world\nHELLO WORLD\n^C\nyusian@SianMac2:~\/Linux\/socket$\n<\/code><\/pre>\n<ul>\n<li>socket\u5e38\u7528\u51fd\u6570\u7684\u5c01\u88c5<\/li>\n<\/ul>\n<p><code>wrap.h<\/code><\/p>\n<pre><code class=\"language-c line-numbers\">#IFNDEF _WRAP_H_\n#DEFINE _WRAP_H_\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n\nint Socket(int domain, int type, int protocol);\nint Bind(int sockfd, const struct sockaddr_in *addr, socklen_t addrlen);\nint Listen(int sockfd, int backlog);\nint Accept(int sockfd, struct sockaddr_in *addr, socklen_t *addrlen);\nint Connect(int sockfd, const struct sockaddr_in *addr, socklen_t addrlen);\n\n#ENDIF\n<\/code><\/pre>\n<p><code>wrap.c<\/code><\/p>\n<pre><code class=\"language-c line-numbers\">#include \u201cwrap.h\"\n#include &lt;errno.h&gt;\n\nvoid print_err(const char* descript)\n{\n    perror(descript);\n    exit(1);\n}\n\nint Socket(int domain, int type, int protocol)\n{\n    int ret = socket(domain, type, protocol);\n    if (ret &lt; 1) print_err(\"socket error\");\n    return ret;\n}\n\nint Bind(int sockfd, const struct sockaddr_in *addr, socklen_t addrlen)\n{\n    int ret = bind(sockfd, (struct sockaddr *)addr, addrlen);\n    if (ret &lt; 0) print_err(\"bind error\");\n    return ret;\n}\n\nint Accept(int sockfd, struct sockaddr_in *addr, socklen_t *addrlen)\n{\n    int sfd;\nagain:\n    sfd = accept(sockfd, (struct sockaddr *)addr, addrlen);\n    if (sfd &lt; 0){\n        \/\/ \u963b\u585e\u7b49\u5f85\uff0c\u8003\u8651\u88ab\u4fe1\u53f7\u4e2d\u65ad\u7684\u60c5\u51b5(\u6162\u901f\u7cfb\u7edf\u8c03\u7528\uff0c\u5728\u7cfb\u7edf\u963b\u585e\u671f\u95f4\u6709\u53ef\u80fd\u88ab\u4fe1\u53f7\u4e2d\u65ad)\n        if ((errno == ECONNABORTED) || (errno == EINTR)){\n            goto again;\n        }else{\n            print_err(\"accept error\");\n        }\n    }\n    return sfd;\n}\n\nint Listen(int sockfd, int backlog)\n{\n    int ret = listen(sockfd, backlog);\n    if (ret &lt; 0) print_err(\"listen error\");\n    return ret;\n}\n\nint Connect(int sockfd, const struct sockaddr_in *addr, socklen_t addrlen)\n{\n    int ret = connect(sockfd, (struct sockaddr *)addr, addrlen);\n    if (ret &lt; 0) print_err(\"connect error\");\n    return ret;\n}\n\nssize_t Readn(int fd, void *buf, size_t count)\n{\n    size_t readed = 0;\/\/ \u5df2\u8bfb\u6570\n        size_t remain = count;\/\/ \u672a\u8bfb\u6570\n    while(remain &gt; 0){\n        int n = read(fd, buf+readed, remain);\n        if (n &lt; 0){    \/\/ \u5f02\u5e38\u5904\u7406\n            if (errno == EINTR){\n                n = 0;\/\/ \u4fe1\u53f7\u4e2d\u65ad\uff0c\u7ee7\u7eed\u5faa\u73af\n            }else{\n                return n;\/\/ \u5f02\u5e38\u60c5\u51b5\u76f4\u63a5\u8fd4\u56de\n            }\n        }else if (n &lt; remain){\n            remain -= n;\n            break;\n        }\n        readed += n;\n        remain -= n;\n    }\n    return count - remain;\/\/ \u8fd4\u56de\u5e94\u8bfb\u6570-\u672a\u8bfb\u6570\n}\n<\/code><\/pre>\n<p><strong>socket\u4e2d\u8bfb\u53d6\u6570\u636eread\u65b9\u6cd5\u8fd4\u56de\u503c\u5206\u6790<\/strong><br \/>\n* <code>&gt; 0<\/code>\uff1a\u5b9e\u9645\u8bfb\u5230\u7684\u5b57\u8282\u6570 buf = 1024 \u6216buf &lt; 1024\u4f46 > 0\uff1b<br \/>\n* <code>= 0<\/code>\uff1a\u6570\u636e\u8bfb\u5b8c\uff08\u8bfb\u5230\u6587\u4ef6\u3001\u7ba1\u9053\u3001socket \u672b\u5c3e\u2014\u5bf9\u7aef\u5173\u95ed\uff09\uff1b<br \/>\n* <code>- 1<\/code>\uff1a\u5f02\u5e38<br \/>\n  * errno = = EINTR  \u88ab\u4fe1\u53f7\u4e2d\u65ad \u91cd\u542f\/quit<br \/>\n  * errno = = EAGAIN\uff08EWOULDBLOCK\uff09\u975e\u963b\u585e\u65b9\u5f0f\u8bfb\uff0c\u5e76\u4e14\u6ca1\u6709\u6570\u636e<br \/>\n  * errno = = \u5176\u4ed6\u503c\uff0c\u51fa\u73b0\u9519\u8bef\uff1aperror( ) exit( )\uff1b<\/p>\n<h3>7\u3001TCP\u5efa\u7acb\u8fde\u63a5\u4e09\u6b21\u63e1\u624b\u3001\u5173\u95ed\u8fde\u63a5\u56db\u6b21\u63e1\u624b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2020\/12\/C2D9C3EA-7303-4B92-B5B8-BA1ECFEEB03D.jpg\" style=\"zoom:50%\"\/><img decoding=\"async\" src=\"https:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2020\/12\/54ADEFA0-D2B7-468D-BA86-696C94FDFB2E.jpg\" style=\"zoom:50%\" \/><\/p>\n<ul>\n<li>SYN\uff1a\u4ee3\u8868\u8bf7\u6c42\u521b\u5efa\u8fde\u63a5\uff0c\u6240\u4ee5\u5728\u4e09\u6b21\u63e1\u624b\u4e2d\u524d\u4e24\u6b21\u8981SYN=1\uff0c\u8868\u793a\u8fd9\u4e24\u6b21\u7528\u4e8e\u5efa\u7acb\u8fde\u63a5\uff0c\u81f3\u4e8e\u7b2c\u4e09\u6b21\u4ec0\u4e48\u7528\uff0c\u5728\u7591\u95ee\u4e09\u91cc\u89e3\u7b54\u3002<\/li>\n<li>FIN\uff1a\u8868\u793a\u8bf7\u6c42\u5173\u95ed\u8fde\u63a5\uff0c\u5728\u56db\u6b21\u5206\u624b\u65f6\uff0c\u6211\u4eec\u53d1\u73b0FIN\u53d1\u4e86\u4e24\u904d\u3002\u8fd9\u662f\u56e0\u4e3aTCP\u7684\u8fde\u63a5\u662f\u53cc\u5411\u7684\uff0c\u6240\u4ee5\u4e00\u6b21FIN\u53ea\u80fd\u5173\u95ed\u4e00\u4e2a\u65b9\u5411\u3002<\/li>\n<li>ACK\uff1a\u4ee3\u8868\u786e\u8ba4\u63a5\u53d7\uff0c\u4ece\u4e0a\u9762\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0d\u7ba1\u662f\u4e09\u6b21\u63e1\u624b\u8fd8\u662f\u56db\u6b21\u5206\u624b\uff0c\u5728\u56de\u5e94\u7684\u65f6\u5019\u90fd\u4f1a\u52a0\u4e0aACK=1\uff0c\u8868\u793a\u6d88\u606f\u63a5\u6536\u5230\u4e86\uff0c\u5e76\u4e14\u5728\u5efa\u7acb\u8fde\u63a5\u4ee5\u540e\u7684\u53d1\u9001\u6570\u636e\u65f6\uff0c\u90fd\u9700\u52a0\u4e0aACK=1,\u6765\u8868\u793a\u6570\u636e\u63a5\u6536\u6210\u529f\u3002<\/li>\n<li>seq\uff1a\u5e8f\u5217\u53f7\uff0c\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u5f53\u53d1\u9001\u4e00\u4e2a\u6570\u636e\u65f6\uff0c\u6570\u636e\u662f\u88ab\u62c6\u6210\u591a\u4e2a\u6570\u636e\u5305\u6765\u53d1\u9001\uff0c\u5e8f\u5217\u53f7\u5c31\u662f\u5bf9\u6bcf\u4e2a\u6570\u636e\u5305\u8fdb\u884c\u7f16\u53f7\uff0c\u8fd9\u6837\u63a5\u53d7\u65b9\u624d\u80fd\u5bf9\u6570\u636e\u5305\u8fdb\u884c\u518d\u6b21\u62fc\u63a5\u3002\u521d\u59cb\u5e8f\u5217\u53f7\u662f\u968f\u673a\u751f\u6210\u7684\uff0c\u8fd9\u6837\u4e0d\u4e00\u6837\u7684\u6570\u636e\u62c6\u5305\u89e3\u5305\u5c31\u4e0d\u4f1a\u8fde\u63a5\u9519\u4e86\u3002\uff08\u4f8b\u5982\uff1a\u4e24\u4e2a\u6570\u636e\u90fd\u88ab\u62c6\u62101\uff0c2\uff0c3\u548c\u4e00\u4e2a\u6570\u636e\u662f1\uff0c2\uff0c3\u4e00\u4e2a\u662f101\uff0c102\uff0c103\uff0c\u5f88\u660e\u663e\u540e\u8005\u4e0d\u4f1a\u8fde\u63a5\u9519\u8bef\uff09<\/li>\n<li>ack\uff1a\u8fd9\u4e2a\u4ee3\u8868\u4e0b\u4e00\u4e2a\u6570\u636e\u5305\u7684\u7f16\u53f7\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u8bf7\u6c42\u65f6\uff0cack\u662fseq+1\uff0c<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\uff1aACK\u4e0eack<\/strong><\/p>\n<h4>7.1\u3001MTU&amp;MSS<\/h4>\n<ul>\n<li>MTU\uff1aMaxitum Transmission Unit \u6700\u5927\u4f20\u8f93\u5355\u5143<\/li>\n<li>MSS\uff1aMaxitum Segment Size \u6700\u5927\u5206\u6bb5\u5927\u5c0f<\/li>\n<li>WIN\uff1a\u6ed1\u52a8\u7a97\u53e3\uff0c\u5f53\u524d\u672c\u7aef\u80fd\u63a5\u6536\u7684\u6570\u636e\u4e0a\u9650\u503c\uff08\u5355\u4f4d\uff1a\u5b57\u8282\uff09<\/li>\n<\/ul>\n<ol>\n<li>\n<p>MSS\u52a0\u5305\u5934\u6570\u636e\u5c31\u7b49\u4e8eMTU. \u7b80\u5355\u8bf4\u62ffTCP\u5305\u505a\u4f8b\u5b50\u3002 \u62a5\u6587\u4f20\u8f931400\u5b57\u8282\u7684\u6570\u636e\u7684\u8bdd\uff0c\u90a3\u4e48MSS\u5c31\u662f1400\uff0c\u518d\u52a0\u4e0a20\u5b57\u8282IP\u5305\u5934\uff0c20\u5b57\u8282tcp\u5305\u5934\uff0c\u90a3\u4e48MTU\u5c31\u662f1400+20+20. \u5f53\u7136\u4f20\u8f93\u7684\u65f6\u5019\u5176\u4ed6\u7684\u534f\u8bae\u8fd8\u8981\u52a0\u4e9b\u5305\u5934\u5728\u524d\u9762\uff0c\u603b\u4e4bMTU\u5c31\u662f\u603b\u7684\u6700\u540e\u53d1\u51fa\u53bb\u7684\u62a5\u6587\u5927\u5c0f\u3002MSS\u5c31\u662f\u4f60\u9700\u8981\u53d1\u51fa\u53bb\u7684\u6570\u636e\u5927\u5c0f\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3a\u4e86\u8fbe\u5230\u6700\u4f73\u7684\u4f20\u8f93\u6548\u80fdTCP\u534f\u8bae\u5728\u5efa\u7acb\u8fde\u63a5\u7684\u65f6\u5019\u901a\u5e38\u8981\u534f\u5546\u53cc\u65b9\u7684MSS\u503c\uff0c\u8fd9\u4e2a\u503cTCP\u534f\u8bae\u5728\u5b9e\u73b0\u7684\u65f6\u5019\u5f80\u5f80\u7528MTU\u503c\u4ee3\u66ff\uff08\u9700\u8981\u51cf\u53bbIP\u6570\u636e\u5305\u5305\u5934\u7684\u5927\u5c0f20Bytes\u548cTCP\u6570\u636e\u6bb5\u7684\u5305\u593420Bytes\uff09\u6240\u4ee5\u5f80\u5f80MSS\u4e3a1460\u3002\u901a\u8baf\u53cc\u65b9\u4f1a\u6839\u636e\u53cc\u65b9\u63d0\u4f9b\u7684MSS\u503c\u5f97\u6700\u5c0f\u503c\u786e\u5b9a\u4e3a\u8fd9\u6b21\u8fde\u63a5\u7684\u6700\u5927MSS\u503c\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4ec0\u4e48\u662fSocket\uff08\u63d2\u5ea7\u3001\u5957\u63a5\u5b57\uff09 IP\uff1a\u7f51\u7edc\u4e2d\u80fd\u6807\u8bc6\u552f\u4e00\u4e00\u53f0\u4e3b\u673a\uff1b Port\uff1a\u4e3b\u673a\u4e2d\u6807\u8bc6\u552f\u4e00\u4e00\u4e2a\u8fdb\u7a0b\uff1b Socket\uff1aIP+Port\uff1b socket\u5728Linux\u4e2d\u4e3a\u4e03\u79cd\u57fa\u672c\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a(\u666e\u901a-\u3001\u76ee\u5f55d\u3001\u8fde\u63a5l\u3001\u7ba1\u9053p\u3001\u5b57\u7b26\u8bbe\u5907c\u3001\u5757\u8bbe\u5907b\u3001\u5957\u63a5\u5b57s) 2\u3001Socket\u7684\u57fa\u672c\u7279\u6027 socket\u662f\u6210\u5bf9\u51fa\u73b0\uff0c\u7ed1\u5b9aip+\u7aef\u53e3 \u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4e24\u4e2a\u7f13\u51b2\u533a\uff0c\u5168\u53cc\u5de5\u8bfb\u5199 3\u3001C\/S\u6a21\u578b \u5927\u5c0f\u7aef\u95ee\u9898\uff1a \u5c0f\u7aef\uff1a\u4f4e\u5730\u5740-\u5b58\u4f4e\u4f4d\u3001\u9ad8\u5730\u5740-\u5b58\u9ad8\u4f4d \u5927\u7aef\uff1a\u4f4e\u5730\u5740-\u5b58\u9ad8\u4f4d\u3001\u9ad8\u5730\u5740-\u5b58\u4f4e\u4f4d \u7f51\u7edc\u6570\u636e\u6d41\u5e94\u91c7\u7528\u5927\u7aef\u5b57\u8282\u5e8f\uff0c\u5373\u4f4e\u5730\u5740\u9ad8\u5b57\u8282 #include &lt;arpa\/inet.h&gt; \/\/ h\uff1ahost\u3001n\uff1anetwork\u3001l\uff1a\u8868\u793a32\u4f4d\u957f\u6574\u6570\u3001s\uff1a\u8868\u793a16\u4f4d\u77ed\u6574\u6570 uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); ip\u5730\u5740\u76f8\u5173\u8f6c\u6362\u51fd\u6570 #include &lt;arpa\/inet.h&gt; \/\/ \u4e3b\u673a\u5230\u7f51\u7edc int inet_pton(int af, const char *src, void *dst); \/\/ \u7f51\u7edc\u5230\u4e3b\u673a const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[8,265,321],"class_list":["post-1844","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-linux","tag-socket","tag-321"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1844"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1844\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}