我正在用 C 编写自己的小型服务器守护程序,处理连接、断开连接和接收等基本部分已经在其中,但接收问题仍然存在。
我使用“recv”一次将 256 个字节读取到一个 char 数组中,并且因为它可以包含多行数据作为一个大块,所以我需要能够单独拆分每一行来处理它。仅此一项就不是问题了,但是由于一条线可能会因为它不再适合缓冲区而被切断,我还需要能够查看一条线是否被切断。也没有那么糟糕,只需检查最后一个字符是否为\r
or \n
,但如果线路被切断怎么办?我的代码不允许简单的“继续阅读更多数据”,因为我select()
用来处理多个请求。
基本上,这是我的情况:
//This is the chunk of code ran after select(), when a socket
//has readable data
char buf[256] = { 0 };
int nbytes;
if ((nbytes = recv(i, buf, sizeof(buf) - 1, 0)) <= 0)
{
if (nbytes == 0)
{
struct remote_address addr;
get_peername(i, &addr);
do_log("[Socket #%d] %s:%d disconnected", i, addr.ip, addr.port);
}
else
do_log("recv(): %s", strerror(errno));
close(i);
FD_CLR(i, &clients);
}
else
{
buf[sizeof(buf) - 1] = 0;
struct remote_address addr;
get_peername(i, &addr);
do_log("[Socket #%d] %s:%d (%d bytes): %s", i, addr.ip, addr.port, nbytes, buf);
// split "buf" here, and process each line
// but how to be able to get the rest of a possibly cut off line
// in case it did not fit into the 256 byte buffer?
}
我正在考虑使用更大范围的临时缓冲区变量(可能是malloc()
'd)来保存当前缓冲区,如果它太长而无法立即放入,但如果有更好的解决方案,我总是对引入不必要的高范围变量感到难过:/
我感谢任何指针(XKCD 除外 :))!