章节5.8-5.10
作者:互联网
服务器代码:
#include <unp.h>
void
str_echo(int sockfd)
{
ssize_t n;
char buf[MAXLINE];
again:
while ((n = read(sockfd, buf, MAXLINE)) > 0)
Writen(sockfd, buf, n);
if (n < 0 && errno == EINTR)
goto again;
else if (n < 0)
err_sys("str_echo: read error");
}
void sig_chld(int singo)
{
pid_t pid;
int stat;
#if 0
pid = wait(&stat);
printf("child %d terminated\n", pid);
#else
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0 ) {
printf("child %d terminated\n", pid);
}
#endif
return;
}
int
main(int argc, char **argv)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6001);
Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
Signal(SIGCHLD, sig_chld);
for ( ; ; ) {
clilen = sizeof(cliaddr);
if ((connfd = Accept(listenfd, (SA*)&cliaddr, &clilen)) < 0) {
if (errno == EINTR)
continue;
else
err_sys("accept error");
}
if ((childpid = fork()) == 0) {
Close(listenfd);
str_echo(connfd);
exit(0);
}
Close(connfd);
}
}
客户端代码:
#include "unp.h"
void
str_cli(FILE *fp, int sockfd)
{
char sendline[MAXLINE], recvline[MAXLINE];
while (Fgets(sendline, MAXLINE, fp)) {
Writen (sockfd, sendline, strlen(sendline));
if (Readline(sockfd, recvline, MAXLINE) == 0 )
err_quit("str_cli:serv terminated prematurely");
Fputs(recvline, stdout);
}
}
int
main(int argc, char **argv)
{
int i;
int sockfd[5];
int ret = 0;
struct sockaddr_in servaddr;
if (argc != 2) {
err_quit("usage:tcpcli <IPaddress>");
}
for (i = 0; i < 5; i++) {
sockfd[i] = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6001);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
Connect(sockfd[i], (SA *)&servaddr, sizeof(servaddr));
}
str_cli(stdin, sockfd[0]);
exit(0);
}
wait的测试结果如下:
waitpid的测试结果如下:
标签:章节,5.8,listenfd,pid,sin,int,5.10,sockfd,servaddr 来源: https://www.cnblogs.com/xiaohei123/p/16197252.html