From aa57b1e412dfe2ce6c473162a1cf47e967d3949a Mon Sep 17 00:00:00 2001 From: yangsoon Date: Sat, 23 May 2020 11:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E5=9B=9E=E5=B0=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- muduo/unp/README.md | 13 ++++++++ muduo/unp/byteorder.c | 23 ------------- muduo/unp/daytimetcp/server.c | 3 ++ muduo/unp/tcpcliser/tcpcli01.c | 23 +++++++++++++ muduo/unp/tcpcliser/tcpserv01.c | 57 +++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 muduo/unp/README.md delete mode 100644 muduo/unp/byteorder.c create mode 100644 muduo/unp/tcpcliser/tcpcli01.c create mode 100644 muduo/unp/tcpcliser/tcpserv01.c diff --git a/muduo/unp/README.md b/muduo/unp/README.md new file mode 100644 index 0000000..fadc9da --- /dev/null +++ b/muduo/unp/README.md @@ -0,0 +1,13 @@ +## UNP + +记录细碎的知识点 + +### TCP客户/服务器程序示例 +网络连接的一些边界问题 + +1. 客户和服务器启动时候发生了什么? +2. 客户正常终止时发生了什么? +3. 若服务器进程在客户之前终止,客户会发生什么? +4. 若服务器主机崩溃,则客户发生什么? + + diff --git a/muduo/unp/byteorder.c b/muduo/unp/byteorder.c deleted file mode 100644 index d75109b..0000000 --- a/muduo/unp/byteorder.c +++ /dev/null @@ -1,23 +0,0 @@ -// byteorder.c -#include "unp.h" - -int main(int argc, char *argv[]) { - union { - short s; - char c[sizeof(short)]; - } un; - un.s = 0x0102; - printf("%s: ", CPU_VENDOR_OS); - if (sizeof(short) == 2) { - if (un.c[0] == 1 && un.c[1] == 2) { - printf("big-endian\n"); - } else if (un.c[0] == 2 && un.c[1] == 1) { - printf("little-endian\n"); - } else { - printf("unknown\n"); - } - } else { - printf("sizeof(short) = %d\n", sizeof(short)); - } - exit(0); -} \ No newline at end of file diff --git a/muduo/unp/daytimetcp/server.c b/muduo/unp/daytimetcp/server.c index b38ce43..43b82a6 100644 --- a/muduo/unp/daytimetcp/server.c +++ b/muduo/unp/daytimetcp/server.c @@ -67,6 +67,9 @@ int main(int argc, char **argv) { // 返回值是和参数3一致的如果不一样说明出错 if (write(connfd, buff, sizeof(buff)) != sizeof(buff)) err_sys("write error"); + + // 当调用close函数的时候 close函数尝试发送已排队等待发送到对端的数据 发送完毕后是正常的TCP的四次挥手 + // 当调用close函数的时候 只有对应的socket 描述符的引用计数为0时 才会引发四次挥手 if (close(connfd) == -1) err_sys("close error"); } diff --git a/muduo/unp/tcpcliser/tcpcli01.c b/muduo/unp/tcpcliser/tcpcli01.c new file mode 100644 index 0000000..b8a1354 --- /dev/null +++ b/muduo/unp/tcpcliser/tcpcli01.c @@ -0,0 +1,23 @@ +#include "unp.h" + +int main(int argc, char **argv) { + int sockfd; + struct sockaddr_in servaddr; + if(argc != 2){ + err_quit("usage: tcpcli "); + } + + sockfd = Socket(AF_INET, SOCK_STREAM, 0); + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(SERV_PORT); + Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); + + if(connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr))<0) { + err_sys("connect error"); + } + + str_cli(stdin, sockfd); + + exit(0); +} \ No newline at end of file diff --git a/muduo/unp/tcpcliser/tcpserv01.c b/muduo/unp/tcpcliser/tcpserv01.c new file mode 100644 index 0000000..59b9b4f --- /dev/null +++ b/muduo/unp/tcpcliser/tcpserv01.c @@ -0,0 +1,57 @@ +#include "unp.h" + +void str_echo_local(int sockfd) { + ssize_t n; + char buf[MAXLINE]; +again: + // 如果客户端关闭连接 那么接收到客户端的FIN将导致read的返回值n=0,进而导致函数返回 + while((n=read(sockfd, buf, MAXLINE))>0) { + Write(sockfd, buf, n); + } + + if(n < 0 && errno == EINTR) { + goto again; + } else if (n < 0) { + err_sys("read error"); + } +} + +int main(int argc, char **argv) { + int listenfd, connfd; + pid_t childpid; + socklen_t clilen; + struct sockaddr_in servaddr, cliaddr; + + if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + err_sys("socket error"); + } + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(SERV_PORT); + + if(bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){ + err_sys("bind error"); + } + + if(listen(listenfd, LISTENQ)<0) { + err_sys("listen error"); + } + + for(;;) { + clilen = sizeof(cliaddr); + connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen); + + if((childpid = fork()) == 0) { + if(close(listenfd)<0) { + err_sys("listen error"); + } + str_echo_local(connfd); + exit(0); + } + + if(close(connfd)<0) { + err_sys("close error"); + } + } +} \ No newline at end of file