已解决:更改了c ==接受(...到c =接受(... 当==使用不当时,应该使用-Wall来查看警告。
我配置syslog连接开放
openlog("program", LOG_PID, LOG_USER);
服务器代码:
for (;;) {
syslog(LOG_NOTICE, "before accept");
if ((c == accept(s, (struct sockaddr *)&sa, &b)) == -1) {
syslog(LOG_ERR, "could not accept on socket");
exit(1);
}
syslog(LOG_NOTICE, "after accept");
pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "could not fork to handle client session");
exit(1);
} else if (0 == pid) {
close(s);
handle_client(c);
exit(0);
syslog(LOG_ERR, "exit after");
} else
close(c);
}
然后我使用telnet连接
telnet localhost 2345
...
Connection closed by foreign host.
日志 /var/log/user.log 显示
...some_info...: client was handled
...some_info...: client was handled
...some_info...: before accept
...some_info...: after accept
...some_info...: before accept
...some_info...: after accept
...some_info...: client was handled
看起来rsyslog以随机顺序多次记录消息, 虽然我知道我只打过一次telnet。 我清楚地理解程序是如何执行的,但是不明白为什么rsyslog会记录多次。
你能否建议如何解决这个&#34; bug&#34;?
答案 0 :(得分:3)
这是一个错误:
if ((c == accept(s, (struct sockaddr *)&sa, &b)) == -1) {
c
永远不会等于-1
。它应该是
if ((c = accept(s, (struct sockaddr *)&sa, &b)) == -1) {
修改强>
这会对使用c
的后续代码产生连锁效应,0
只能包含值1
或handle_client(c);
。
close(c);
和
machine.config