How to use read syscall with an integer fd in C++

I am working with sockets. On creating one using socket(), we get an integer file descriptor. I then want to read data from this fd.

To do so, I was following functioning C code – using the read() syscall, in the following line:

read (sockfd /* int */, buff /* char* */, 50);

However, compiling gives the error "read was not declared in this scope; did you mean ‘fread’?".

On simply compiling the given functioning C client code as a C++ file with g++, I get the same errors.

The includes in this C++ program are (in-order): iostream, netdb.h, netinet/in.h, stdio.h, stdlib.h, string.h, sys/socket.h, sys/types.h

And the std namespace is in use.
What am I doing wrong?

>Solution :

You might be missing #include <unistd.h>
But I also recommend using recv, which is declared in sys/socket.h

If you’re stuck, always try manpages: man read.2

Leave a Reply