使用TCP在WCF中进行双向通信 [英] Two way communication in WCF using TCP

查看:77
本文介绍了使用TCP在WCF中进行双向通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Every,



我的项目要求是需要在服务器和客户端之间进行通信。目前我可以从客户端到服务器进行通信,这意味着当客户端请求服务服务器时响应请求,但我希望服务器请求服务客户端响应请求相同的响应。我已经使用了回调函数,但问题是它只支持字符串数据类型,但在我的应用程序中,我需要在两个方向上携带大数据,并且服务应该仅使用TCP协议在一侧托管。









预付款,



Arun RV

Hello Every ,

My project requirement is need to communicate between server and client .Presently I can communicate from client to server ,means when client request for service server response to request but I want the same response where server request for service client response for request . I have worked with callback function but the problem is it only supports string data type but in my application I need to carry large data in both direction and the service should be hosted only on one side using TCP protocol.




Advance thanks,

Arun R.V

推荐答案

您遇到了互联网设计方式的根本问题。设置为客户端与服务器断开连接,并始终从服务器请求数据。因此,对话必须始终由客户端启动。



通常,如果要让服务器向客户端发送内容,则必须设置轮询方法在服务器上。客户端调用它并等待。反过来,服务器将保持呼叫,直到一段时间到期(其中它返回一条消息,指示没有任何兴趣发生),或者它在超时之前向客户端返回事件通知。如果客户端仍在侦听,则会对消息或超时做出反应。如果是超时,客户端将立即再次呼叫该服务。这是一种昂贵的方法,因为服务器必须为连接的每个客户端保持连接打开并消耗资源。所以它不是一个非常可扩展的解决方案。但它可以工作,它避免了防火墙和其他令人讨厌的东西背后的NAT'd客户的很多问题。



如果你的服务器运行IIS,还有另一个办法。有一个名为SignalR的库,您可以使用它来完成所有这些工作而无需所有开销。您实质上是在客户端注册要处理的服务器上创建一个回调方法。然后,服务器可以随时调用该方法,就像任何其他方法一样。客户端上的方法处理程序将获取消息并处理它。如果可用,系统将使用Web套接字来减少客户端和服务器上的开销,或者如果没有Web套接字支持则将回退到轮询。好的是,作为开发人员,您不必担心它。
You are running into a fundamental problem with the way the Internet was designed. It was setup such that the clients are disconnected from the servers and ALWAYS request data from the server. Therefore, conversations must always be started by the client.

Normally, if you want to get the server to send something to the client, you have to setup a polling method on the server. The client calls it and waits. The server, in turn, will hold onto the call until a time period expires (in which it returns a message indicating nothing of interest happened) or it returns the notification of an event to the client before the timeout. The client, if it is still listening, reacts to the message or the timeout. If it is the timeout, the client will immediately call the service again. This is an expensive way to do it because the server must keep a connection open for each client that is connected and that consumes resources. So it isn't a very scalable solution. But it works and it avoids a lot of problems with NAT'd clients behind firewalls and other nasty stuff like that.

If your server is running IIS, there is another way. There is a library called SignalR that you can use to do all of this without all of the overhead. You essentially create a callback method on the server that your clients register to handle. Then the server can call the method anytime passing data in just like any other method. The method handler on the client will get the message and handle it. The system will use web sockets if available to reduce the overhead on the client and server or will fall back to polling if there is no web socket support. The nice thing is that as the developer, you don't need to worry about it.


这篇关于使用TCP在WCF中进行双向通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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