-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## UNP | ||
|
||
记录细碎的知识点 | ||
|
||
### TCP客户/服务器程序示例 | ||
网络连接的一些边界问题 | ||
|
||
1. 客户和服务器启动时候发生了什么? | ||
2. 客户正常终止时发生了什么? | ||
3. 若服务器进程在客户之前终止,客户会发生什么? | ||
4. 若服务器主机崩溃,则客户发生什么? | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |