1. 说明
1.1 功能
第5章的第一个程序
client从终端读入一行,写到socket中
server从socket中读入一行,并将读到的data写到socket中
client从socket中读取一行,并显示到终端
1.2 问题
在
unix网络编程笔记---4.时间获取server与client 中ps命令显示server有僵死进程,
同时对慢系统调用函数accept的EINTR没有处理
1.3 主要修改了server端,client端没有变化
2. 源码
server.c
-
cong@msi:/work/test/tcpip/6tcp/serv1$ cat tcpserv.c
-
#include "utils.h"
-
-
#define LISTENQ 10
-
void sig_chld(int signo) //问题1.处理sigchld
-
{
-
pid_t pid;
-
int stat;
-
pid = wait(&stat);
-
dbmsg("child %d terminated", pid);
-
return ;
-
}
-
void str_echo(int sockfd)
-
{
-
ssize_t n;
-
char buf[MAXLINE];
-
again:
-
while( (n=read(sockfd, buf, MAXLINE)) > 0)
-
write(sockfd, buf, n);
-
-
if( (n<0) && (errno==EINTR) )
-
goto again;
-
else if(n < 0)
-
dbmsg("read_error");
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int listenfd, connfd;
-
struct sockaddr_in servaddr, cliaddr;
-
signal(SIGCHLD, sig_chld); //问题1.捕获信号sigchld
-
pid_t childpid;
-
char buf[1024];
-
int len;
-
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(listenfd< 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
//inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
-
bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
listen(listenfd, LISTENQ);
-
while(1)
-
{
-
len = sizeof(cliaddr);
-
//connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
-
connfd = accept(listenfd, (struct sockaddr*)&cliaddr,(socklen_t*)&len);
-
dbmsg("accept:%s,port=%d", inet_ntop(AF_INET,&cliaddr.sin_addr,buf, sizeof(buf)), ntohs(cliaddr.sin_port));
-
if(errno == EINTR) //问题2.慢系统调用accept返回eintr时,需要重启accept
-
{
-
dbmsg("EINTR");
-
continue;
-
}
-
if( (childpid=fork()) == 0)
-
{
-
close(listenfd);
-
str_echo(connfd);
-
exit(0); //问题1.子进程exit时会产生信号sigchld
-
}
-
dbmsg("close connfd");
-
close(connfd);
-
}
-
-
return EXIT_SUCCESS;
-
}
client.c
-
cong@msi:/work/test/tcpip/6tcp/client$ cat client.c
-
#include "utils.h"
-
-
//#define TIME_SERV_ADDR "192.168.4.98"
-
#define TIME_SERV_ADDR "127.0.0.1"
-
#if 0
-
//这段readline有bug
-
//buf的末尾没有加'\0',导致显示时出错
-
ssize_t readline(int fd, void* vptr, size_t maxlen)
-
{
-
ssize_t n, rc;
-
char c,*ptr;
-
ptr = vptr;
-
for(n=1; n<maxlen; n++)
-
{
-
again:
-
if((rc=read(fd,&c,1)) == 1)
-
{
-
*ptr++ = c;
-
if(c == '\n')
-
break;
-
} else if(rc == 0) {
-
*ptr = 0;
-
return (n-1);
-
}else {
-
if( errno == EINTR)
-
goto again;
-
return -1;
-
}
-
}
-
}
-
#endif
-
ssize_t readline(int fd, void* vptr, size_t maxlen)
{
ssize_t n, rc;
char c,*ptr;
ptr = vptr;
for(n=0; n
again:
if((rc=read(fd,&c,1)) == 1)
{
*ptr++ = c;
if(c == '\n')
break;
} else if(rc == 0) {
*ptr = 0;
return n;
}else {
if( errno == EINTR)
goto again;
return -1;
}
}
*ptr = '\0';
return n;
}
-
void str_cli(FILE* fp, int sockfd)
-
{
-
char sendline[MAXLINE], recvline[MAXLINE];
-
while(fgets(sendline, MAXLINE, fp) != NULL)
-
{
-
write(sockfd, sendline, strlen(sendline));
-
if(readline(sockfd, recvline, MAXLINE) == 0)
-
dbmsg("server terminated");
-
fputs(recvline, stdout);
-
}
-
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int sockfd, n;
-
struct sockaddr_in servaddr;
-
char recvline[1024];
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(sockfd < 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
dbmsg("next connect");
-
connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
dbmsg("connect success");
-
-
str_cli(stdin, sockfd);
-
return EXIT_SUCCESS;
-
}
utils.h
-
cong@msi:/work/test/tcpip/6tcp/serv$ cat utils.h
-
#ifndef UTILS_H__
-
#define UTILS_H__
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/types.h> /* See NOTES */
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <time.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <signal.h>
-
-
#define MAXLINE 1024
-
-
#define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
-
-
#endif
client.rar (下载后改名为client.tar.gz)
serv.rar (下载后改名为serv.tar.gz)
阅读(991) | 评论(0) | 转发(0) |