简体   繁体   English

在C#中创建客户端/服务器聊天客户端

[英]Creating a Client/Server Chat Client in C#

I want to to create a simple client/server chat application. 我想创建一个简单的客户端/服务器聊天应用程序。

The idea is that when one client sends a message to the server the server informs the other clients with the change. 这个想法是,当一个客户端向服务器发送消息时,服务器会将更改通知其他客户端。

What is the best way to do so without having to deal with firewall problems? 无需解决防火墙问题的最佳方法是什么?

Can it be done with web services over http ? 可以通过http上的Web服务完成吗?

WCF service with wsHttpBinding or basicHttpBinding plays nice with firewalls. 带有wsHttpBindingbasicHttpBinding WCF服务可与防火墙配合使用。 The major problem you will face is the server informing clients for which modifying some firewall settings might be necessary. 您将面临的主要问题是服务器通知客户端,可能需要修改其防火墙设置。

The server needs to be listening on a port. 服务器需要在端口上侦听。 That port needs to have access through a firewall. 该端口需要通过防火墙进行访问。 These two things are unavoidable. 这两件事是不可避免的。 Usually you will let your server listen on port number you make up (greater than 1024 to avoid clashing). 通常,您将让服务器侦听您组成的端口号(大于1024以避免冲突)。 You will then let your firewall pass that port to your server. 然后,您将让防火墙将该端口传递到服务器。

If you're expecting ordinary folk to host a server then things will be a bit tricky. 如果您期望普通人托管服务器,那么事情将有些棘手。 Either you need them to enable port forwarding on their home router or you need to have a centralized server for matchmaking. 您需要他们在其家用路由器上启用端口转发,或者需要具有集中式服务器以进行配对。

This is how Skype or Halo works (Skype calls the central server a supernode). 这就是Skype或Halo的工作方式(Skype将中央服务器称为超节点)。 Here is an example. 这是一个例子。

C is the central Skype server. C是中央Skype服务器。 A is Alice and B is bob. A是爱丽丝,B是鲍勃。 Alice wants to call Bob. 爱丽丝想给鲍勃打电话。

C listens on some port and C's firewall lets that through C监听某个端口,C的防火墙允许它通过
A logs into Skype, connecting to C A登录到Skype,连接到C
B logs into Skype, connecticg to C B登录到Skype,连接到C
A sends a message to C saying they want to call B A向C发送一条消息,称他们想呼叫B
C sends a message to B (since B is already connected this is ok) saying as such. C这样向B发送一条消息(因为B已经连接好了)。

Once the call is connected A and B send all their messages to C and C forwards them on to their destination. 呼叫接通后,A和B将所有消息发送给C,C将它们转发到目的地。

您可以在服务器上运行HTTP服务器,并在每个客户端上保持打开状态。

You could accomplish this using a COMET style web service: 您可以使用COMET样式的Web服务来完成此操作:

COMET (programming) COMET(编程)

If you're building your services using WCF, you would implement a service that runs on port 80 with a Duplex Binding. 如果使用WCF构建服务,则将实现在端口80上运行带有双工绑定的服务。

Duplex Services (MSDN) 双工服务(MSDN)

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

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