TcpClient同时向tcp服务器发送2个字符串。 [英] TcpClient send 2 string to tcp server at the same time.

查看:138
本文介绍了TcpClient同时向tcp服务器发送2个字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tcpclient / tcplistner创建登录功能,但我只能通过流而不是密码输入将用户名输入发送到服务器。服务器现在收到的字符串就像usernamepassword。有没有办法将输入用户名和密码作为两个不同的变量发送?我试图将用户名:密码字符串与Split(':')分开,然后在服务器上显示输出,如下所示:

Hi, i'm making a login feature using tcpclient/tcplistner but i'm only able to send the username input to the server through the stream and not the password input. The string that the server recieve now is like "usernamepassword". Is there a way to send both inputs username and password as two diffrent variables? i've tried to separate the username:password string with the Split(':') and then displaying the output on the server like this:

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          data = Encoding.ASCII.GetString(bytes, 0, i);

          string[] credentials = data.Split(':');

          string username = data[0].ToString();
          string password = data[1].ToString();

          Console.WriteLine("Username: {0}", username);
          Console.WriteLine("Password: {0}", password);
      }





但是输出只是用户名和密码的第一个字符,例如,如果凭据字符串是用户名:密码输出将是:





but the output is only the first character of the username and the password for example if the credential string is username:password the output will be:

Username: u
Password: p





为什么会这样?并且最好的方法是发送两个字符串而不是分割它但是无法使其工作更友好。



任何帮助都将受到赞赏。



Why is it like this? and the best way would be to send two strings instead of splitting it but couldn't get that to work ethier.

Any help would be appreciated.

推荐答案

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          data = Encoding.ASCII.GetString(bytes, 0, i);
 
          string[] credentials = data.Split(':');
 
          string username = credentials[0];
          string password = credentials[1];
 
          Console.WriteLine("Username: {0}", username);
          Console.WriteLine("Password: {0}", password);
      }





那将是这样做的方式,使用凭据,而不是数据,否则你只是采取第一个数据的第二个字母。



That would be the way to do it, use credentials, not data, otherwise you are just taking the first and second letter of the data.


maxbre写道:

好的谢谢。我稍后会加密密码。

Ok thanks. I will encrypt the passwords later.

重要! 加密密码不是一个正确的主意。



请查看我过去的答案:

我已经加密了我的密码,但是当我登录时给了我一个错误。如何解密 [ ^ ] ,

使用用户名和密码进行TCP连接 [ ^ ],

解密加密密码 [ ^ ],

以安全的方式存储密码值int sql server [ ^ ]。



理想情况下,您应该将它与HTTPS一起使用,因为创建密码的仪式本身可以从一开始就被窃听。在这种情况下,恶意艺术家将不需要您的密码,但会按原样使用哈希。请参阅:

http://en.wikipedia.org/wiki/HTTPS [< a href =http://en.wikipedia.org/wiki/HTTPStarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Transport_Layer_Security [ ^ ]。



-SA

Important! "Encrypting password" is not a right idea.

Please see my past answers:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
TCP Connection with username and password[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

Ideally, you should use it with HTTPS, because the ceremony of creation of the password can itself be eavesdropped from the very beginning. In such case, the malicious artist won't need your password but would used the hash as is. Please see:
http://en.wikipedia.org/wiki/HTTPS[^],
http://en.wikipedia.org/wiki/Transport_Layer_Security[^].

—SA


这篇关于TcpClient同时向tcp服务器发送2个字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆