473,804 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL & pipe, socket descriptors


Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectob jects(20);

all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_ba ck(...) as well. If I
initilize objects like that :

object* first_object = new object;
object* second_object = new object;

it is ok. (the descriptors given by pipe(descr); are different)

I want to use STL vector/list/whatever but it just doesn't work. I can
understand that vector<objectob jects(20); may construct just one
object and then copy it 20 times, but shouldn't push_back() be any
better?

Please help.

Marcin Endraszka

Jun 16 '07 #1
8 3371
M.*********@gma il.com wrote:
>
Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectob jects(20);
this calls

vector<object>: :vector(vector< object>::size_t ype, const object& t=object());

so the definition of objects is the same as if you wrote

vector<objectob jects(20, object());

and this creates 20 copies of the temporary created as second argument.
all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_ba ck(...) as well.
What do you mean initialize with push_back?

vector<objectob jects;
for (int i = 0; i < 20; ++i)
objects.push_ba ck(object());

?

It's hard to guess without seeing any code.

--
rbh
Jun 16 '07 #2
M.*********@gma il.com wrote:
Hi,

I am writing a simple irc-like server and came across weird behavior
while debugging. Could anyone tell me why the following happens :

I have a class which stores pipe descriptors as a member field.
Descriptors are initialized with pipe(descr) in class constructor.
Then I have an array of objects of this class, preferably a STL
vector. If I initialize the vector with:

vector<objectob jects(20);

all the objects have the same pipe descriptors (pipes are the same &
there is no point in have 20 of them)
this happens if I initialize with objects.push_ba ck(...) as well. If I
initilize objects like that :

object* first_object = new object;
object* second_object = new object;

it is ok. (the descriptors given by pipe(descr); are different)

I want to use STL vector/list/whatever but it just doesn't work. I can
understand that vector<objectob jects(20); may construct just one
object and then copy it 20 times, but shouldn't push_back() be any
better?

Please help.

Marcin Endraszka
Post some code. Probably your definition of object is wrong, but who can
say without seeing some code.

john
Jun 16 '07 #3
On 16 Cze, 21:08, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
It's hard to guess without seeing any code.
class object
{
public:
int p[2];

object(int id)
{
pipe(p);
printf("Object %d has descriptor %d.",id,p[0]);
}

~object()
{
close(p[0]);
close(p[1]);
}
};
Static array of those will return eg.

object objects[3];
Object 1 has descriptor 7.
Object 2 has descriptor 9.
Object 3 has descriptor 11. (there are pairs of them of
c.)

STL vector will return:

vector<objectob jects;
objects.push_ba ck(1);objects.p ush_back(2);obj ects.push_back( 3);

Object 1 has descriptor 7.
Object 2 has descriptor 7.
Object 3 has descriptor 7.

Which is totally not what I want. What's more if there was a socket
descriptor no.7 before, I may get same pipe descriptor AND socket
descriptor at the same time, which is so wrong.

Ask whatever you need.

Marcin

Jun 16 '07 #4
M.*********@gma il.com wrote:
Static array of those will return eg.

object objects[3];
Object 1 has descriptor 7.
Object 2 has descriptor 9.
Object 3 has descriptor 11. (there are pairs of them of
c.)

STL vector will return:

vector<objectob jects;
Try at this point:

objects.reserve (3);
objects.push_ba ck(1);objects.p ush_back(2);obj ects.push_back( 3);

Object 1 has descriptor 7.
Object 2 has descriptor 7.
Object 3 has descriptor 7.

Which is totally not what I want.
You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this is
your problem. The vector copies your objects and makes temporaries which
get destructed, closing your pipes. So the next pipe gets an old yet unused
descriptor.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 16 '07 #5
M.*********@gma il.com wrote:
On 16 Cze, 21:08, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
>It's hard to guess without seeing any code.

class object
{
public:
int p[2];

object(int id)
{
pipe(p);
printf("Object %d has descriptor %d.",id,p[0]);
}

~object()
{
I'll give you a hint. Insert

printf("Descrip tors %d and %d closed\n", p[0], p[1]);

here.
close(p[0]);
close(p[1]);
}
};

--
rbh
Jun 16 '07 #6
On 16 Cze, 22:38, "Thomas J. Gritzan" <Phygon_ANTIS.. .@gmx.dewrote:
You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this is
your problem. The vector copies your objects and makes temporaries which
get destructed, closing your pipes. So the next pipe gets an old yet unused
descriptor.
Ok. I see the sequence is CONSTRUCT TEMP -COPY TO DESTINATION ->
DESTROY TEMP
which I don't like, but still makes sense

Writing a copy constructor seems to help

object(object const& in)
{
id = in.id;
pipe(p);
}

I also added assigment operator as one of you suggested:

object& operator=(const object& in)
{
p[0] = in.p[0];
p[1] = in.p[1];
}

Though I think compiler generated one would do the same.
Thanks for mentioning the "rule of three"

Cheers

Jun 16 '07 #7
M.*********@gma il.com wrote:
On 16 Cze, 22:38, "Thomas J. Gritzan" <Phygon_ANTIS.. .@gmx.dewrote:
>You definitly violate the "rule of three". That may be the problem here.
Google for it. If it works with a reserve(3) in this example, that this
is your problem. The vector copies your objects and makes temporaries
which get destructed, closing your pipes. So the next pipe gets an old
yet unused descriptor.

Ok. I see the sequence is CONSTRUCT TEMP -COPY TO DESTINATION ->
DESTROY TEMP
which I don't like, but still makes sense.
It's what C++ does.
Writing a copy constructor seems to help

object(object const& in)
{
id = in.id;
pipe(p);
}
IMHO, this is the wrong solution. It creates a lot of pipes, and it makes
the object harder to use (copies aren't equal as one would usually expect).
I also added assigment operator as one of you suggested:

object& operator=(const object& in)
{
p[0] = in.p[0];
p[1] = in.p[1];
}

Though I think compiler generated one would do the same.
Your compiler would generate the same thing. You need the rule of three, but
not this way.

Solution 1)
class object {
... //as before
private:
void operator = (const object&); // No need to actually implement these
object(const object&);
};

vector<boost::s hared_ptr<objec t objects;
for (...)
objects.push_ba ck(boost::share d_ptr<object>(n ew object(i)));

This would make the objects non copyable, and your boost::shared_p tr takes
care of deleting. You could also use std::auto_ptr instead of
boost::shared_p tr.

Solution 2)
class object {
struct arr {
int p[2];
};
boost::shared_p tr<arrp;
public:
object() : p(new arr)
{ pipe(p->p); }
// compiler generated copy ctor, assignment op and dtor works
//...
};

Boost::shared_p tr can be downloaded from <URL:http://boost.org>. It's also
up for the new standard, and might be found if your compiler ships the tr1
libs. In that case it's called std::tr1::share d_ptr (turn on tr1 and
#include <memory>)

--
rbh
Jun 16 '07 #8
On Jun 16, 7:34 pm, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
wrote:
vector<boost::s hared_ptr<objec t objects;
for (...)
objects.push_ba ck(boost::share d_ptr<object>(n ew object(i)));

This would make the objects non copyable, and your boost::shared_p tr takes
care of deleting. You could also use std::auto_ptr instead of
boost::shared_p tr.
no, you couldn't

Diego

Jun 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
27360
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However, periodically, I see "java.IO.Exception: There is no process to read data written to a pipe" error message in my log file. Can anybody tell me in what cases this error message could occur? The other strange thing is that our customer would report...
2
2352
by: Hans Deragon | last post by:
Greetings. I am performing: commands.getstatusoutput("rsync <params>"); rsync takes a long time and prints out a steady stream of lines showing which file it is currently working on.
7
2464
by: Dario | last post by:
I have an unmanaged library that handle many TCP/IP connections. In my .NET application i want to test if there is input available on these connections. Using an existing function of the unmanaged library I'm able to retrieve the list of Int32 socket-descriptors handled by the unmanaged library. How can I use this list of Int32 socket-descriptors inside my C# program to test if there is input available on these socket-descriptors? I...
4
26881
by: Pascal Ehlert | last post by:
I don't know if this is the right newsgroup because the question is maybe a bit linux specific so if not slap me ;-) I'm trying to send the output of a perl-script to a socket. So I'm opening a pipe to the program (FILE fp = popen("foobar.pl", "r")) and read and write it directly to the socket.. This is my code: ------ while(!feof(fp)) { bytes = fread(b,1,1024,fp); fwrite(b,1,bytes,sock);
4
6588
by: billiejoex | last post by:
Hi, I'm writing a small asyncore-based server application serving a lot of clients. When I have to handle more than 1021 client simoultaneously the 'binded' socket object raises an error: connections: 1018 connections: 1019 connections: 1020 connections: 1021
11
8623
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
3
5275
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue is about Python or POSIX. In the following, the first time through, everything works. On the second connection, though, the same file descriptor as the first connection may be re-used, but for some reason, trying to do os.read/close on that...
1
1823
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 17:35:44 -0700 (PDT), schickb <schickb@gmail.comwrote: Alas, you haven't actually closed src's standard input. Though you did close src.stdin, you didn't close the copy of it which was inherited by dst! So though the file descriptor is no longer open in your main process, it remains open due to the reference dst has to it. You can fix this by having Popen close all file descriptors except 0, 1, and 2 before it execs...
3
8736
by: Jeremy Sanders | last post by:
Hi - I have some code which works under linux. It starts a remote python process using subprocess and communicates to it via a pipe created by os.pipe. As far as I understand, child processes should inherit file descriptors from the parent if close_fds=False on the suprocess.Popen command line. This code doesn't work under Window, but gives "bad file descriptor" when trying to read from the pipe in the child process. I have some example...
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.