473,372 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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.


Thanks
Nov 19 '07 #1
3 12469
oler1s
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.

Thanks
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

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

Similar topics

1
by: Johannes Eble | last post by:
Hello Python community, I am trying the echo-client and echo-server examples in Chapter 10, "Programming Python" by Mark Lutz. It is probably the most simple sockets sample: A socket server just...
6
by: Rune | last post by:
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python...
0
by: Ajay | last post by:
hi! my application is throwing a bad socket error raise error(EBADF, 'Bad file descriptor') socket.error: (9, 'Bad file descriptor') basically i have the following interchange application ...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
3
by: Billism | last post by:
My email client (I programmed) recently has stopped being able to send email with my XP machine. I have had 3 other friends test it and the same thing happens. The program has not changed, but...
2
by: Rookie | last post by:
Hi, I was writing a simple program using sockets. The program is supposed to be a TCP server that receives a string sent by the client. For this purpose I defined a char array called readString...
0
by: Tomasz Naumowicz | last post by:
Hello Everybody, I have the following problem with Socket: Socket.Available reports data in the buffer (e.g. 1849 bytes) but I can't read it because Socket.Read throws an exception The...
1
by: niceyama | last post by:
Dear c.l.p, I have recently been doing the tutorial of Python and everything is well, i'm upto the pass section. Anyway, when I try to launch idle now I get the error message: Socket Error:...
1
by: Sergey Kozyrev | last post by:
Hi all, I'm trying to test my rulecore program with Event Generator but when I generate new event, I got error message: "Socket error: Connection refused, error code 10061". What can it be? It's...
3
by: W. Watson | last post by:
After simply trying to write a program with help(MakeQTE), a module, and having it fail with socket errors, I decided to restart IDLE, thinking I knew the cause. I'm now getting msgs like: "IDLE's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.