Search code examples
c++socketsserverwinsock

Name for new socket


I am learning socket programming, i had a problem. I want when people connecting to my server, i see his name(For example "on server connected Richard" ). How assign name to socket. P.S. Sorry for my English.


Solution

  • You don't. I mean that you don't "assign a name to a socket". Instead you should keep a structure of some kind containing the socket, and all associated data that's needed for that connection, like the connected users name.

    For example something similar to

    struct user
    {
        SOCKET      socket;  // The users connected socket
        std::string name;    // The name of the connected user
        // TODO: Other attributes needed for the user or the users connection
    };
    

    Exactly how to get the user-name is another matter for another question (though I could give you a hint: States).