CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 2010
    Posts
    172

    Unhappy Socket and vector

    Does any one know how i can put sockets into a vector?

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Socket and vector

    just a guess:

    vector<socket> sockets;

  3. #3
    Join Date
    Apr 2010
    Posts
    172

    Arrow 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++)
    		   {
    			   
    
    			  
    
    		   }

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    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:
    Code:
    SOCKET s = *itl;
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Apr 2010
    Posts
    172

    Resolved Re: Socket and vector

    Thanks ahoodin works like a charm

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    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.

  7. #7
    Join Date
    Apr 2010
    Posts
    172

    Arrow 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

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    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.

  9. #9
    Join Date
    Apr 2010
    Posts
    172

    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++)
    		   {
    			 
    		
    
    		   }

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Socket and vector

    Quote Originally Posted by gaar321 View Post
    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

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Socket and vector

    Quote Originally Posted by gaar321 View Post
    [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

  12. #12
    Join Date
    Apr 2010
    Posts
    172

    Arrow Re: Socket and vector

    well how can i put all my client sockets into an array?

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Socket and vector

    Quote Originally Posted by gaar321 View Post
    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

  14. #14
    Join Date
    Apr 2010
    Posts
    172

    Re: Socket and vector

    Code:
    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Socket and vector

    Quote Originally Posted by gaar321 View Post
    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured