470,848 Members | 1,005 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Socket program in c++. gives "‘read’ was not declared " error on read () system call

Hi guys I wrote a socket program in c++ and used blocking system call read () for receiving data on socket. But it gives following error while compilation

[HTML]g++ -Wall -g -I`pwd`/../include -lpthread -c SocketDescriptor.cc -o `pwd`/../lib/SocketDescriptor.o
SocketDescriptor.cc: In member function ‘Status SocketDescriptor::Recv(int&, unsigned char*, int&)’:
SocketDescriptor.cc:79: error: ‘read’ was not declared in this scope
make: *** [SocketDescriptor.o] Error 1[/HTML]

definition of this read () call is:
int count read (int socketFd, char * buffer, int bufferlength, int flags)

When use the same call in a C programe I don't get this error. Moreover programe works fine. I think C library uses certain default headers and C++ do not.

Do anybody has any idea? Or please tell how I can look for it.

Nov 19 '07 #1
3 11149
671 Expert 512MB
Let's see some code.

Here's the first thought that passed into my head. You didn't include the header properly. C implicitly assumes a function returns an int. I.e., you could potentially get away with a lot of bad code, because Wall won't trap everything. C++ is more strict though.
Nov 19 '07 #2
Following are the headers I have included in both the programs.
#include <netinet/in.h>
#include <linux/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

C code:
13 memset(buff, '\0', BUFFLEN);
14 if (read(sock, buff, BUFFLEN, flags) < 0){/*error handling*/}
15 printf("[SERVER] %s", buff);

C++ code:

75 Status SocketDescriptor::Recv(Int32 & msgType, UInt8* data, Int32 & data len)
76 {
77 UInt8 *buff = new UInt8[MAX_DATA_LEN];
78 Int32 len, totallen;
79 if ( read(descriptor, buff, len, 0) < 0)
80 {
81 return ERROR;
82 }

It would be a great help if you are able resolve it. From two days I am looking for it.

Nov 22 '07 #3
Hi I figured it out in a way.
I added an h and c file to my code having wrappers over read and write sys calls. Compiled them with C compiler. And used the c header in the code where I needed read and write with C lang linkage specifier.
It works fine now.
Nov 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Johannes Eble | last post: by
6 posts views Thread by Rune | last post: by
reply views Thread by Ajay | last post: by
3 posts views Thread by Billism | last post: by
2 posts views Thread by Rookie | last post: by
reply views Thread by Tomasz Naumowicz | last post: by
1 post views Thread by niceyama | last post: by
3 posts views Thread by W. Watson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.