简体   繁体   English

将登录信息与套接字相关联?

[英]Associating login information with a socket?

I'm writing a server that needs to know if you are logged in or not. 我正在编写一个服务器,该服务器需要知道您是否已登录。 To log in, the user sends his username and password as a packet through a socket. 要登录,用户将其用户名和密码作为数据包通过套接字发送。 Based on that socket, how can I tell if the user has been logged in? 基于该套接字,如何确定用户是否已登录?

Should I be associating login information with the socket - at that low a level? 我应该将登录信息与套接字相关联吗?

Better yet, is there a simple solution to this problem that I'm missing? 更好的是,是否存在针对我所缺少的此问题的简单解决方案?

I would just create a struct that groups information about the user with the socket. 我只是创建一个struct ,用套接字将有关用户的信息分组。

It's something I do with a User or Player class when writing in C#, no reason you can't translate that to C. 在用C#编写时,这是我对UserPlayer类所做的事情,没有理由不能将其转换为C。

All you need to do is keep a structure (array, hash, map, whatever; array is simple, hash would more efficient, but hardly available in C), where you store your sockets ids (in C each socket has an int identifier like file), along with all the other information, you would like to have about every single connection/user. 您需要做的就是保持一个结构(数组,哈希,映射等;数组简单,哈希效率更高,但在C中几乎不可用),在此存储套接字ID(在C中,每个套接字都有一个int标识符,例如文件)以及所有其他信息,您希望拥有每个连接/用户。

Each time the package comes you have to lookup the data in the structure. 每次打包时,您都必须在结构中查找数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM