简体   繁体   English

C#TCP / IP服务器和客户端

[英]C# TCP/IP server and client

i have a simple TCP client/server application. 我有一个简单的TCP客户端/服务器应用程序。 How do set up the two program so on the server side it requires a username and a password, and when the client tries to connect, they have to enter the correct user name and password and send it to the server side, the server side will check to see if thats the correct username and password, if its correct then it allows connection, otherwise it rejects the connction 如何设置两个程序,以便在服务器端需要用户名和密码,并且当客户端尝试连接时,他们必须输入正确的用户名和密码并将其发送到服务器端,服务器端将检查这是否是正确的用户名和密码,如果正确,则允许连接,否则拒绝连接

Use WCF since you obviously have not used pure sockets before. 使用WCF,因为您以前显然没有使用过纯套接字。 It's will most probably be a more stable solution then if you try to learn sockets AND invent your own protocol. 如果您尝试学习套接字并发明自己的协议,那将是最稳定的解决方案。

You can use WCF together with the netTcpBinding to achieve what you want. 您可以将WCF与netTcpBinding一起使用以实现所需的功能。 You got a example here: http://www.dotnetspider.com/resources/19314-Client-server-sample-using-WCF-net-tcp-binding.aspx 您在这里有一个示例: http : //www.dotnetspider.com/resources/19314-Client-server-sample-using-WCF-net-tcp-binding.aspx

I'm sorry if this wasn't the answer you where looking for, but there is a lot that need to be implemented correctly to get a hassle free tcp/ip client/server solution. 很抱歉,如果这不是您要找的答案,那么要获得麻烦的免费tcp / ip客户端/服务器解决方案,有很多事情必须正确实施。

This can not be done using simple TCP/IP. 使用简单的TCP / IP无法做到这一点。 You'd either have to invent some kind of protocol or use WCF or the like. 您要么必须发明某种协议,要么使用WCF等。

As the "connect" happens before anything can be sent, there's no way of sending credentials "along with the connect". 由于“连接”发生在发送任何东西之前,因此无法“连同连接”发送凭据。 You'd have to have the client connect, receive a command for "Login", check credentials and pass back some unique identifier that then needs to be sent along with every command so the server can check whether the client was authenticated. 您必须让客户端连接,接收用于“登录”的命令,检查凭据并传回一些唯一的标识符,然后该标识符与每个命令一起发送,以便服务器可以检查客户端是否已通过身份验证。 That's how I'd do it if WCF was not an option. 如果没有WCF,那就是我会这样做的方式。

White Thorsten is technically correct, I still think there is a solution: White Thorsten在技术上是正确的,我仍然认为有解决方案:

Let the server accept all connections, but then expect as the first data in the connection the username and password. 让服务器接受所有连接,然后将用户名和密码作为连接中的第一个数据。 If they are incorrect, the server will then close the connection, and not process any requests that the client may have sent on the connection. 如果它们不正确,则服务器将关闭连接,并且不处理客户端可能已在连接上发送的任何请求。

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

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