简单的聊天协议 [英] Simple chat protocol

查看:71
本文介绍了简单的聊天协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C# 中的网络和线程.为此,我正在开发网络聊天功能.

I'm learning networking and threading in C#. For that purpose I'm developing chat over network.

目前我在客户端 - 服务器(TCP)之间有基本的通信.服务器可以与多个客户端一起工作.但只有客户端-服务器通信.基本上,客户端将 ASCII 编码的消息发送到服务器,然后服务器对其进行解码并显示在控制台中.

Currently I have basic communication between client - server (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.

现在我想实现客户端-客户端通信.

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.

下一步是点击按钮,这将组成一个 Socket 并发送,然后服务器应该知道谁是寻址消息.

Next step is clicking button, which will compose a Socket and send, then Server should understand whom is addressed message.

那么,我的消息结构应该是什么,我应该如何理解服务器中的消息是谁寻址的?

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?

我研究了 XMPP.它很重.我只需要方向,我怎么能做到这一点.我的目标是学习,而不是实施然后忘记.

I have looked into 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 是基于流的,这意味着在 TCP 的帮助下,您永远不会知道消息何时开始和结束.任何消息/协议设计都需要解决这个问题.

TCP is stream based which means that you will never know, with the help of TCP, when a message begins and ends. 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.

HTTP 两者都使用.它使用空行来确定标题何时结束.在标题中,它有一个 Content-Length 标题,它告诉正文有多大.

HTTP uses both. It uses an empty line to determine when the header ends. And in the header it got a Content-Length header which tells how large the body is.

对于二进制协议,我建议您使用固定长度的标头,其中第一个整数(4 个字节)是版本,第二个整数是正文长度.通过这种方式,您可以轻松地在版本之间切换标题布局(因为版本是第一个整数).

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. In this way you can easily switch header layout between versions (since the version is the first integer).

对于文本协议,它实际上取决于消息内容的外观.问题是内容可能不包括要使用的分隔符(如果您正在传输聊天消息,这可能会很困难).如果分隔符存在于实际聊天消息中,您当然可以对其进行转义.但恕我直言,更好的方法是使用像 HTTP 这样的标头/正文布局(因为它也很容易解析,而且您可以拥有 X 个标头而无需更改解析器).

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).

一条消息看起来像:

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

This is a text

请注意,长度为 16,这是因为正文中包含了新行.

Notice that the length is 16, this is since the new line was included in the body.

对于客户端-客户端通信,如果您是初学者,我总是会通过服务器.这要容易得多,否则您必须确保至少有一个客户端不在路由器后面(否则将无法传递消息).

As for client-client communication I would always go through the server if you are a beginner. 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).

只需检查 To 标题,如果它是用于聊天室或用户.

Just check the To header if it's for a chat room or a user.

这篇关于简单的聊天协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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