Because of specifications requirement, I'm not allowed to use recv() or similar functions to read from socket's fd. I was wondering how I would remake that function using read()?
I've attempted creating it with the following code
int recvFromFd(int connectionFd, char *buf, int size)
{
char c;
int i = 0;
for (read(connectionFd, &c, 1); i < size; i++)
{
buf[i] = c;
if (c == '\n' || read(connectionFd, &c, 1) == EOF)
break;
}
return i;
}
This code works to an extent, but often when when the sender send one message after another, the function read both of those message at the same time to the same buffer (appends).
You can use read(2) in place of recv(2) on Unix (but I'm not sure if it works on Windows, where is no read() function). But read() function has some limitations:
You can't get peer's address (in case of UDP protocol);
You can't receive zero sized datagrams (UDP);
You can't pass file descriptors (via unix domain socket);
You can't work with special socket types (AF_NETLINK domain, SOCK_SEQPACKET socket type), which is used to obtain network configuration from the kernel.
Linux manpage says:
As I think, for TCP protocol where is almost no difference in use of read(2) or recv(2).