C# - 服务器端密码保护

发布于 2024-09-25 04:57:09 字数 889 浏览 0 评论 0原文

我正在编写两个控制台应用程序,一个客户端和一个服务器。 我有点卡在两件事上,一开始看起来相当简单。

#1:我想为下面的代码编写一个函数,将位转换为字符串,但我无法理解它 当我使用它时,服务器总是崩溃。我的函数与这个有点不同,但那是因为我当前的代码必须包含连接信息,而且我认为有更好的方法来做到这一点:

        byte[] b = new byte[100];
        int k = s.Receive(b);

            string packet = null;
        for (int i = 0; i < k; i++)
        {
            Console.Write(Convert.ToChar(b[i]));
            packet = packet + Convert.ToChar(b[i]);
        }

我想函数不是问题,但我如何使用它是。任何帮助将非常感激。

编辑: 我像这样调用和使用它:

    byte[] b = new byte[100];
    string response = BitConvert(b);

    if (response == "Hi there")

#2 我希望客户端总是只发送一次带有密码的数据包。并且如果该密码与作为字符串提到的密码不匹配服务器,它应该关闭与客户端的连接。

我知道如何只发送一次数据包,但我不知道如何为每个客户端在服务器中检查一次数据包。

或者换句话说,目前服务器无法知道客户端是否已经通过身份验证。所以我猜客户端需要有某种套接字 ID,服务器需要一个包含该 ID 的表,以及一个布尔值来查看它是否经过身份验证。

I am writing two console applications, a client and a server.
I'm a little stuck at two things, which seemed rather easy at first..

#1: I want to write a function for the following piece of code, that converts bits to a string, but I cant just figure it out. The server always crashes when I use it. My function is a little bit different than this one, but that's because my current code has to include the connection information, and I think there's a better way to do it:

        byte[] b = new byte[100];
        int k = s.Receive(b);

            string packet = null;
        for (int i = 0; i < k; i++)
        {
            Console.Write(Convert.ToChar(b[i]));
            packet = packet + Convert.ToChar(b[i]);
        }

I guess the function is not the problem, but how I use it is. Any help would be very much apreciated.

Edit:
I am calling and using it like this:

    byte[] b = new byte[100];
    string response = BitConvert(b);

    if (response == "Hi there")

#2 I want the client to álways send a packet just once, with a password. And if that password doesn't match the password mentioned as a string in the server, it should close the connection with the client.

I know how to send the packet just once, but I don't know how to check the packet in the server just once for each client.

Or in other words, at the moment the server has no way of knowing if the client has already been authenticated. So I guess the client needs to have some sort of socket ID, and the server needs a table with the ID, and a boolean to see if it's autenticated or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信仰 2024-10-02 04:57:10

第一部分,将字节放入字符串中……怎么样:

byte b[] = new byte[100];
int k = s.Receive(b, b.Length, 0);

string packet = Encoding.ASCII.getString(b, 0, k);

第二部分……我不太确定。

The first part, getting the bytes into a string ... how about:

byte b[] = new byte[100];
int k = s.Receive(b, b.Length, 0);

string packet = Encoding.ASCII.getString(b, 0, k);

Part 2 ... not sure off the top of my head.

就此别过 2024-10-02 04:57:10

我在上面的第 1 部分和第 2 部分中同意 Rob 的观点...
假设您使用 TCP 连接,System.Net.Sockets 类应该处理您的
需要。

如果您使用 AcceptSocketAcceptTcpClient 从传入连接请求队列中提取连接,您将获得该连接特有的套接字或 tcpclient。只需将其保持打开状态,直到完成即可。 (如果您的服务还有其他事情要做,还有非阻塞替代方案......)

如果客户端关闭它或打开新连接,则必须以某种方式重新进行身份验证。 (这可能是通过包含您在他们首次进行身份验证时为他们生成的令牌 - 如果您使用 UDP 连接,这是您唯一的选择)。

I'm with Rob above on part 1 and as for part 2...
Assuming you're using a TCP connection the System.Net.Sockets class should handle your
needs.

If you use either AcceptSocket or AcceptTcpClient to pull a connection from the incoming connection request queue you'll get a socket or tcpclient that is unique to that connection. simply leave it open until you're done with it. (There are also non-blocking alternatives if your service has other things to do...)

If the client closes it or opens up a new connection the will have to reauthenticate somehow. (This may be by including a token that you generate for them when they first authenticate - that's your only option if you're using UDP connections).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文