我正在使用 TCP 服务器,但出现了一些非常奇怪的东西。
当我与一个客户联系时,一切都很好。
当两个或更多客户端连接时,流量仍然很好。
但是,当任何客户端断开连接时,服务器会在收割者调用后立即卡住。它只是坐在那里等待。
当我从 2 个客户端中断开 1 个客户端并尝试重新连接时,我发现了这一点。重新连接的客户端根本不显示任何错误消息,它运行顺利。包可以发送到服务器,但它堆积在服务器内部。
另一方面,服务器挂在那里,等待一个特定的客户端断开连接。如果该客户端断开连接,服务器将恢复功能,执行堆积在其中的所有请求。
下面是我用于服务器结构的准系统代码。
此代码还演示了上述问题。
有人可以请,请指出错误发生在哪里?
void reaper(int sig)
{
int status;
while (waitpid(-1, &status, WNOHANG) >= 0)
/* empty */;
}
int errexit(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
int errno;
unsigned short portbase = 0; /* port base, for non-root servers */
int passivesock(const char *service, const char *transport, int qlen)
{
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry*/
struct sockaddr_in sin; /* an Internet endpoint address */
int s, type; /* socket descriptor and socket type */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
/* Map service name to port number */
if ( pse = getservbyname(service, transport) )
sin.sin_port = htons(ntohs((unsigned short)pse->s_port)
+ portbase);
else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
errexit("can't get \"%s\" service entry\n", service);
/* Map protocol name to protocol number */
if ( (ppe = getprotobyname(transport)) == 0)
errexit("can't get \"%s\" protocol entry\n", transport);
/* Use protocol to choose a socket type */
if (strcmp(transport, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("can't create socket: %s\n", strerror(s));
/* Bind the socket */
if (errno=bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
errexit("can't bind to %s port: %s\n", service,
strerror(errno));
if (type == SOCK_STREAM && listen(s, qlen) < 0)
errexit("can't listen on %s port: %s\n", service,
strerror(type));
return s;
}
int passiveTCP(const char *service, int qlen)
{
return passivesock(service, "tcp", qlen);
}
#define QLEN 32 /* maximum connection queue length */
#define BUFSIZE 4096
int TCPechod(int fd);
int main(int argc, char *argv[])
{
char *service; /* service name or port number */
struct sockaddr_in fsin; /* the address of a client */
unsigned int alen; /* length of client's address */
int msock; /* master server socket */
int ssock; /* slave server socket */
if (argc !=2)
errexit("usage: %s port\n", argv[0]);
service = argv[1];
msock = passiveTCP(service, QLEN);
(void) signal(SIGCHLD, reaper);
while (1) {
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock < 0) {
if (errno == EINTR)
continue;
errexit("accept: %s\n", strerror(ssock));
}
printf("Accept connection %d from %s:%d\n", ssock, inet_ntoa(fsin.sin_addr), (int)ntohs(fsin.sin_port));
switch (fork()){
case 0:
(void) close(msock);
TCPechod(ssock);
close(ssock);
exit(0);
default:
close(ssock);
break;
case -1:
errexit("fork: %s\n", strerror(errno));
}
}
}
int TCPechod(int fd)
{
char buf[BUFSIZE];
int cc;
while (cc = read(fd, buf, sizeof(buf))) {
if (cc < 0)
errexit("echo read: %s\n", strerror(cc));
if (errno=write(fd, buf, cc) < 0)
errexit("echo write: %s\n", strerror(errno));
}
return 0;
}
任何抬头将不胜感激。
我提前谢谢你。