Skip to content

Commit

Permalink
简单的回射代码
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsoon committed May 23, 2020
1 parent 2ee4c76 commit aa57b1e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 23 deletions.
13 changes: 13 additions & 0 deletions muduo/unp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## UNP

记录细碎的知识点

### TCP客户/服务器程序示例
网络连接的一些边界问题

1. 客户和服务器启动时候发生了什么?
2. 客户正常终止时发生了什么?
3. 若服务器进程在客户之前终止,客户会发生什么?
4. 若服务器主机崩溃,则客户发生什么?


23 changes: 0 additions & 23 deletions muduo/unp/byteorder.c

This file was deleted.

3 changes: 3 additions & 0 deletions muduo/unp/daytimetcp/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
23 changes: 23 additions & 0 deletions muduo/unp/tcpcliser/tcpcli01.c
Original file line number Diff line number Diff line change
@@ -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 <IPaddress>");
}

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);
}
57 changes: 57 additions & 0 deletions muduo/unp/tcpcliser/tcpserv01.c
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit aa57b1e

Please sign in to comment.