为什么 recv() 在 accept 工作时给我 errno 88?
Why does recv() give me errno 88 when accept works?
为什么我在下面的代码中会得到 recv() 的 errno 88?我似乎使用了正确的套接字并且接受似乎有效。程序运行良好,我可以从客户端连接,但是只要我发送一个字节的数据,recv() 就会失败。据我所知,我使用的是正确的套接字 fd,但我不知道是什么导致了失败。
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "log.h"
#include "server.h"
#include "protocol.h"
bool run_alarm_server ()
{
llog(LOG_INFO, "Starting alarm server.");
alarm_on = false;
unlink(SOCKET_PATH);
int success;
int sock;
int conn;
bool run;
char buf[1];
struct sockaddr_un srvaddr;
success = false;
memset(&srvaddr, 0, sizeof(srvaddr));
srvaddr.sun_family = AF_UNIX;
strcpy(srvaddr.sun_path, SOCKET_PATH);
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
llog(LOG_ERROR, "Failed to create socket.");
} else {
llog (LOG_DEBUG, "Created socket.");
}
if (bind(sock, (struct sockaddr *) &srvaddr, SUN_LEN(&srvaddr)) < 0) {
llog(LOG_ERROR, "Failed to bind to socket.");
} else {
llog(LOG_DEBUG, "Bound to socket.");
}
if (listen(sock, SERVER_QUEUE_LEN) < 0) {
llog(LOG_ERROR, "Failed to listen on socket.");
} else {
llog(LOG_INFO, "Listening on socket.");
}
run = true;
while (run) {
if ((conn = accept(sock, NULL, NULL) < 0)) {
llog(LOG_ERROR, "Failed to accept connection on socket.");
} else {
llog(LOG_INFO, "Accepted connection on socket.");
}
if (recv(conn, buf, sizeof(buf) / sizeof(buf[0]), 0) < 0) {
llog(LOG_ERROR, "Failed to receive data on socket.");
} else {
llog(LOG_DEBUG, "Received data on socket.");
}
switch (buf[0]) {
case ALARM_END:
alarm_on = false;
llog(LOG_ALARM, "The alarm has ended.");
break;
case ALARM_START:
alarm_on = true;
llog(LOG_ALARM, "The alarm has started.");
break;
case ALARM_ARM:
llog(LOG_ALARM, "The system has been armed.");
break;
case ALARM_DISARM:
alarm_on = false;
llog(LOG_ALARM, "The system has been disarmed.");
break;
case ALARM_STATUS:
llog(LOG_INFO, "The alarm status has been requested.");
char alarm_status[1];
if (alarm_on) {
alarm_status[0] = (char) 0x01;
} else {
alarm_status[0] = (char) 0x00;
}
if (send(conn, (const void *) alarm_status, 1, 0) < 0) {
llog(LOG_ERROR, "Failed to send alarm status.");
run = false;;
} else {
llog(LOG_DEBUG, "Alarm status sent.");
}
break;
case ALARM_QUIT:
llog(LOG_INFO, "Exiting.");
run = false;
success = true;
break;
}
}
if (socket >= 0) {
close(sock);
}
if (conn >= 0) {
close(conn);
}
unlink(SOCKET_PATH);
return success;
}
你的问题在这里:
if ((conn = accept(sock, NULL, NULL) < 0)) {
这行被解释为:
if (conn = (accept(sock, NULL, NULL) < 0)) {
... 这意味着如果 accept() 调用 returns 负值,conn
将被设置为 1(又名布尔值 true),否则设置为 0(又名布尔值 false) .
我认为你的意图是这样的:
if ((conn = accept(sock, NULL, NULL)) < 0) {
为什么我在下面的代码中会得到 recv() 的 errno 88?我似乎使用了正确的套接字并且接受似乎有效。程序运行良好,我可以从客户端连接,但是只要我发送一个字节的数据,recv() 就会失败。据我所知,我使用的是正确的套接字 fd,但我不知道是什么导致了失败。
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "log.h"
#include "server.h"
#include "protocol.h"
bool run_alarm_server ()
{
llog(LOG_INFO, "Starting alarm server.");
alarm_on = false;
unlink(SOCKET_PATH);
int success;
int sock;
int conn;
bool run;
char buf[1];
struct sockaddr_un srvaddr;
success = false;
memset(&srvaddr, 0, sizeof(srvaddr));
srvaddr.sun_family = AF_UNIX;
strcpy(srvaddr.sun_path, SOCKET_PATH);
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
llog(LOG_ERROR, "Failed to create socket.");
} else {
llog (LOG_DEBUG, "Created socket.");
}
if (bind(sock, (struct sockaddr *) &srvaddr, SUN_LEN(&srvaddr)) < 0) {
llog(LOG_ERROR, "Failed to bind to socket.");
} else {
llog(LOG_DEBUG, "Bound to socket.");
}
if (listen(sock, SERVER_QUEUE_LEN) < 0) {
llog(LOG_ERROR, "Failed to listen on socket.");
} else {
llog(LOG_INFO, "Listening on socket.");
}
run = true;
while (run) {
if ((conn = accept(sock, NULL, NULL) < 0)) {
llog(LOG_ERROR, "Failed to accept connection on socket.");
} else {
llog(LOG_INFO, "Accepted connection on socket.");
}
if (recv(conn, buf, sizeof(buf) / sizeof(buf[0]), 0) < 0) {
llog(LOG_ERROR, "Failed to receive data on socket.");
} else {
llog(LOG_DEBUG, "Received data on socket.");
}
switch (buf[0]) {
case ALARM_END:
alarm_on = false;
llog(LOG_ALARM, "The alarm has ended.");
break;
case ALARM_START:
alarm_on = true;
llog(LOG_ALARM, "The alarm has started.");
break;
case ALARM_ARM:
llog(LOG_ALARM, "The system has been armed.");
break;
case ALARM_DISARM:
alarm_on = false;
llog(LOG_ALARM, "The system has been disarmed.");
break;
case ALARM_STATUS:
llog(LOG_INFO, "The alarm status has been requested.");
char alarm_status[1];
if (alarm_on) {
alarm_status[0] = (char) 0x01;
} else {
alarm_status[0] = (char) 0x00;
}
if (send(conn, (const void *) alarm_status, 1, 0) < 0) {
llog(LOG_ERROR, "Failed to send alarm status.");
run = false;;
} else {
llog(LOG_DEBUG, "Alarm status sent.");
}
break;
case ALARM_QUIT:
llog(LOG_INFO, "Exiting.");
run = false;
success = true;
break;
}
}
if (socket >= 0) {
close(sock);
}
if (conn >= 0) {
close(conn);
}
unlink(SOCKET_PATH);
return success;
}
你的问题在这里:
if ((conn = accept(sock, NULL, NULL) < 0)) {
这行被解释为:
if (conn = (accept(sock, NULL, NULL) < 0)) {
... 这意味着如果 accept() 调用 returns 负值,conn
将被设置为 1(又名布尔值 true),否则设置为 0(又名布尔值 false) .
我认为你的意图是这样的:
if ((conn = accept(sock, NULL, NULL)) < 0) {