简体   繁体   English

TCP客户端和服务器同时

[英]Tcp client and server at same time

I've to implement a client communication interface with a server that's a bit weird and I've doubts about how to develop it. 我必须使用有点奇怪的服务器来实现客户端通信接口,并且我对如何开发它有疑问。

Communication follows this rules: 交流遵循以下规则:

  1. It's done vía TCP only. 仅通过TCP完成。
  2. Client sends to server an XML identifying itself. 客户端将标识自己的XML发送到服务器。
  3. If server response is OK, client starts listen incomming requests from server. 如果服务器响应正常,则客户端开始侦听来自服务器的传入请求。

Can a TcpClient act as a server? TcpClient可以充当服务器吗? What would be the best way to implement this? 实施此方法的最佳方法是什么?

Thanks in advance. 提前致谢。

Once the connection has been established, there's no significant distinction between "client" and "server". 建立连接后,“客户端”和“服务器”之间就没有明显的区别。 There's just a TCP stream that can be used to communicate information in both directions. 只有一个TCP流可用于双向交流信息。 You should be fine to use TcpClient , because it sounds like you don't need to be listening for new connections or anything like that; 使用TcpClient应该很好,因为听起来您不需要侦听新的连接或类似的东西; you just need to read requests from the existing stream and provide responses. 您只需要从现有流中读取请求并提供响应即可。

After establishing connection to server, client can send or receive any data. 与服务器建立连接后,客户端可以发送或接收任何数据。 So if you just want to listen requests from server: 因此,如果您只想监听来自服务器的请求:

while(client.Connected)
{
   client.Client.Receive(data);
   DoSomething(data);
}

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

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