Does any one know how i can put sockets into a vector?
Printable View
Does any one know how i can put sockets into a vector?
just a guess:
vector<socket> sockets;
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++)
{
}
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:
to get a socket:Code:vector<SOCKET>::iterator itl;
for(itl = sockets.begin(); itl != sockets.end();itl++)
{
itl->connect(....);
}
Code:SOCKET s = *itl;
Thanks ahoodin works like a charm :D
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...:D
Thanks!
There is one thing however when it cycles through the loop the vector dosent add on the socket element instead it replaces it
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.
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++)
{
}
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
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
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.
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.Code:SOCKET s1;
//...
SOCKET s2 = s1;
//...
SOCKET s3;
s3 = s1;
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
Code:SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;