|
-
August 17th, 2011, 06:59 AM
#1
Socket and vector
Does any one know how i can put sockets into a vector?
-
August 17th, 2011, 07:20 AM
#2
Re: Socket and vector
just a guess:
vector<socket> sockets;
-
August 17th, 2011, 07:33 AM
#3
Re: Socket and vector
This is what i have done
But how do i store each indvidual socket into an array
Code:
vector<SOCKET> ClientSocket(100);
vector<SOCKET>::iterator itl;
for(itl = ClientSocket.begin(); itl != ClientSocket.end();itl++)
{
}
-
August 17th, 2011, 08:00 AM
#4
Re: Socket and vector
Code:
SOCKET s;
vector<SOCKET> sockets(100);
//...in here code to create a new socket
sockets.push_back(s);
then to use the iterator to get at the socket:
Code:
vector<SOCKET>::iterator itl;
for(itl = sockets.begin(); itl != sockets.end();itl++)
{
itl->connect(....);
}
to get a socket:
ahoodin
To keep the plot moving, that's why.

-
August 17th, 2011, 08:15 AM
#5
Re: Socket and vector
Thanks ahoodin works like a charm
-
August 17th, 2011, 08:28 AM
#6
Re: Socket and vector
Good I am glad to help.
If you appreciated my help, please rate me by clicking the text that says "rate this post" next to the scale icon. I do like positive ratings...
Thanks!
ahoodin
To keep the plot moving, that's why.

-
August 17th, 2011, 09:40 AM
#7
Re: Socket and vector
There is one thing however when it cycles through the loop the vector dosent add on the socket element instead it replaces it
-
August 17th, 2011, 10:44 AM
#8
Re: Socket and vector
Is this a socket client or socket listener?
if it is a listener, then you could pushback the result of accept().
please post your code.
ahoodin
To keep the plot moving, that's why.

-
August 17th, 2011, 01:17 PM
#9
Re: Socket and vector
Code:
ClientSocket = accept(ListenSocket, (sockaddr*)&addr,&length);
vector<SOCKET> sockets(100);
sockets.push_back(ClientSocket);
vector<SOCKET>::iterator itl;
for(itl = sockets.begin(); itl != sockets.end();itl++)
{
}
-
August 17th, 2011, 01:18 PM
#10
Re: Socket and vector
 Originally Posted by gaar321
Does any one know how i can put sockets into a vector?
You can't just place things in a vector -- they need to be safely copy constructible and assignable. I don't know what your "socket" is, but if it is not safely copyable, then you shouldn't be placing these types in a vector.
If you posted what a "socket" is, then we will see if it has these requirements.
Regards,
Paul McKenzie
-
August 17th, 2011, 01:21 PM
#11
Re: Socket and vector
 Originally Posted by gaar321
[code]
ClientSocket = accept(ListenSocket, (sockaddr*)&addr,&length);
vector<SOCKET> sockets(100);
Again, post SOCKET. If it contains pointer members, or members where there is no useful or meaningful way to copy those members, then SOCKET should not be placed in a vector. If you then go ahead and use vector for this type, then your program will exhibit undefined behaviour when vector starts to make copies of that SOCKET object.
If that is the case, then you should be using vector<SOCKET*> or a vector of smart pointers (not auto_pointer) to SOCKET instead of vector<SOCKET>.
Regards,
Paul McKenzie
-
August 17th, 2011, 01:24 PM
#12
Re: Socket and vector
well how can i put all my client sockets into an array?
-
August 17th, 2011, 01:30 PM
#13
Re: Socket and vector
 Originally Posted by gaar321
well how can i put all my client sockets into an array?
It makes no difference if it's an array or vector. If SOCKET cannot be copied safely, then you cannot assign sockets to each other, which you will wind up doing regardless if you use an array or vector.
Code:
SOCKET s1;
//...
SOCKET s2 = s1;
//...
SOCKET s3;
s3 = s1;
That is what is in question -- assume that s1 is a socket that has already been set up. Now, can you go and create an s2 using an s1, or assign an s1 to s3 without weird things occurring or corrupting something? This is essentially what will be happening behind the scenes if you have an array of SOCKET or vector of SOCKET.
Why not post or tell us what SOCKET really is? What header file is it defined in? Then we can tell you if it can be copied safely.
Regards,
Paul McKenzie
-
August 17th, 2011, 01:31 PM
#14
Re: Socket and vector
Code:
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
-
August 17th, 2011, 01:45 PM
#15
Re: Socket and vector
 Originally Posted by gaar321
Code:
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
So SOCKET is a UINT_PTR (integer). There shouldn't be a problem on the surface, since integers are copyable.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|