Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’?".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading