Storing SOCKET in a vector

Asked

Viewed 126 times

2

I am working on a project developed in c/c++ where a server handles several connections, I do not have much experience in this language, I would like to know how I should proceed to add a SOCKET in a vector, to be able to access it then.

  • c/c++ is not a language. Either 'and C or 'and C++. Although similar, each language has its own particularities (which may not function properly in the other language)

1 answer

2


You may be creating a data structure

/* Struct Client */  
struct HYPNOS_STRUCT
{
    SOCKET socket;
};

Then define a vector of structures with the structure created above.

const int MAX_CLIENTS = 50;
std::vector<HYPNOS_STRUCT> hypnos_client(MAX_CLIENTS);

So once you accept a new socket you may be accessing a position of your vector to add it!

SOCKET incoming = INVALID_SOCKET;
incoming = accept(server_socket, (struct sockaddr *)&client_info, &addrsize);

hypnos_client[i].socket = incoming;
  • Is there any advantage in using the struct in relation to a SOCKET or pointer to SOCKET(std::vector<SOCKET*> sockets;) straightforward?

  • 3

    i use the structure to manipulate various data that a client may have, for example Hostname, id, Username ai is already organized for that client at that vector position

  • @Rhayden Thank you very much!

  • /the need, we are there! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.