简体   繁体   English

简单的聊天协议

[英]Simple chat protocol

I'm learning networking and threading in C#.我正在学习 C# 中的网络和线程。 For that purpose I'm developing chat over network.为此,我正在开发网络聊天。

Currently I have basic communication between client - server (TCP).目前我在客户端 - 服务器(TCP)之间进行了基本的通信。 Server can work with multiple clients.服务器可以与多个客户端一起工作。 But only client - server communication.但只有客户端-服务器通信。 Basically client sends ASCII encoded message to server, then server decodes it and shows in console.基本上,客户端将 ASCII 编码的消息发送到服务器,然后服务器对其进行解码并在控制台中显示。

By now I want to implement Client-Client communication.现在我想实现客户端-客户端通信。

Suppose we have online list of clients in each client and message box for sending message to each client.假设我们有每个客户端的在线客户端列表和用于向每个客户端发送消息的消息框。

Next step is clicking button, which will compose a Socket and send, then Server should understand whom is addressed message.下一步是单击按钮,它将组成一个 Socket 并发送,然后服务器应该了解谁是寻址消息。

So, what should be my structure of message, and how I should understand in Server, whom addressed message?那么,我的消息结构应该是什么,以及在服务器中我应该如何理解,谁发送消息?

Generally I don't need code, I want theory.通常我不需要代码,我需要理论。 Simple and short.简单而简短。 Maybe tutorials?也许教程?

I have looked into XMPP.我研究过XMPP。 It's very heavy.它很重。 I just need direction, how I can do this.我只需要方向,我怎么能做到这一点。 My goal is to learn, not implement it and forgot.我的目标是学习,而不是实施并忘记。

TCP is stream based which means that you will never know, with the help of TCP, when a message begins and ends. TCP 是基于 stream 的,这意味着在 TCP 的帮助下,您永远不会知道消息何时开始和结束。 Any message/protocol design needs to address that.任何消息/协议设计都需要解决这个问题。

There are two ways to detect when a message ends.有两种方法可以检测消息何时结束。 The first way is to add a delimiter at the end of message, and the second way is to include the length in a header.第一种方法是在消息末尾添加分隔符,第二种方法是在 header 中包含长度。

HTTP uses both. HTTP 两者都使用。 It uses an empty line to determine when the header ends.它使用空行来确定 header 何时结束。 And in the header it got a Content-Length header which tells how large the body is.在 header 中,它有一个 Content-Length header ,它告诉我们身体有多大。

For binary protocols I suggest that you use a fixed length header where the first integer (4 bytes) is a version and the second integer is the body length.对于二进制协议,我建议您使用固定长度 header ,其中第一个 integer (4 字节)是一个版本,第二个 integer 是正文长度。 In this way you can easily switch header layout between versions (since the version is the first integer).通过这种方式,您可以轻松地在版本之间切换 header 布局(因为版本是第一个整数)。

For text protocol it really depends on how the message contents looks like.对于文本协议,它实际上取决于消息内容的外观。 The problem is that the content may not include the delimiter to be used (which can be hard if you are transporting chat messages).问题是内容可能不包含要使用的分隔符(如果您正在传输聊天消息,这可能会很困难)。 You could of course escape the delimiter if it exists in the actual chat message.如果分隔符存在于实际聊天消息中,您当然可以转义它。 But imho a better approach is to use a header/body layout like HTTP (since it's also quite easy to parse and you can have X number of headers without having to change the parser).但恕我直言,更好的方法是使用像 HTTP 这样的标题/正文布局(因为它也很容易解析,您可以拥有 X 个标题而无需更改解析器)。

A message would look like:一条消息如下所示:

From: Arne
To: #ChannelName
WrittenAt: 2011-07-03 12:00 GMT
Content-Length: 16

This is a text

Notice that the length is 16, this is since the new line was included in the body.请注意,长度为 16,这是因为新行包含在正文中。

As for client-client communication I would always go through the server if you are a beginner.至于客户端-客户端通信,如果您是初学者,我将始终通过服务器 go。 It's a lot easier since otherwise you have to make sure that at least one of the clients is not behind a router (or it will be impossible to deliver the message).这要容易得多,否则您必须确保至少有一个客户端不在路由器后面(否则将无法传递消息)。

Just check the To header if it's for a chat room or a user.如果是聊天室或用户,只需检查To header。

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

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