如何编写(实现)应用层网络协议

发布于 2024-10-09 18:35:24 字数 103 浏览 9 评论 0原文

我想在 TCP 之上编写(实现)一个应用层网络协议。你能告诉我如何开始做吗? 任何在线教程也会​​有帮助 伙计们,我也需要 sm 实施教程。我对此很陌生,时间也较少:( RFC更多的是关于规则

I want to write(implement) a Application layer network protocol above TCP. Can you tell me how to start doing it?
Any online tutorial will also be helpful
guys i need sm implementation tutorial too. I am new to this and have less time also :(
RFC is more about rules

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎言笑 2024-10-16 18:35:24

首先阅读 SMTP 协议标准。这是(最初)一个非常简单的协议,没有附加任何条件,也没有令人困惑或虚假的陈述。 HTTP 和 FTP 更复杂(对于初学者来说),因为它们功能更丰富,非线性(在许多方面)等。

更新:我已经将链接放到了原始 RFC 821 ,现在已经过时了。然而,它比最新(且有效)的 SMTP RFC 更小且更易于阅读。

Start by reading a standard for SMTP protocol. This is (originally) a very simple protocol with no strings attached and no confusing or bogus statements. HTTP and FTP are more complicated (for beginner) as they are more feature-rich, non-linear (in many aspects) etc.

Upd: I've put the link to the original RFC 821 , which is now obsolete. Yet it is much smaller and easier to read than the most recent (and valid) RFC for SMTP.

蒲公英的约定 2024-10-16 18:35:24

最好的解决方案是关注当前的解决方案,例如 torrent、ftp、http。它应该给你一些知识。然后设计的协议将取决于你的想象力。

The best solution is to watch over current solutions like torrent, ftp, http. It should give you some knowlegde. Then disigned protocol will depend on your imagination.

趁年轻赶紧闹 2024-10-16 18:35:24

设计自己的协议并不是一件简单的事情。然而,将其构建在 TCP/IP 之上会更容易。您指出 RFC 是有关协议的规则,您是对的。然而,这通常就是协议的含义:关于如何完成某件事的一组规则和协议。这里的美妙之处在于您可以指定自己的规则。您需要考虑的一些事情是要发送的数据类型、每部分的长度以及状态(如果有)。例如,HTTP 是一种无状态协议,具有定义请求或结果的标头以及定义所发送数据的有效负载,该数据可以是表单帖子或 html 页面。

因此,您需要定义要发送或接收的数据、数据长度(以字节为单位),可以通过数据类型或预期数据量来定义。如果您的协议有一个状态 - 即必须先发送和接收某些内容,然后才能发送和接收其他内容 - 那么您需要定义该状态。除此之外,它是简单的网络编程(在应用程序中发送和接收数据)。

例如,一个简单的协议可以这样:

Command 1 Byte
Length  4 Bytes
Data    Length Bytes

前两个定义标头,其中包含有关另一端需要能够读取所有内容的数据的元数据。最后一个是实际数据。命令虽然不是必需的,但用于表明您可以让另一方根据收到的字节对数据执行特定操作。数据的长度很重要,因为另一端必须知道何时读取了所有数据,然后才能对其进行处理。

这个答案确实有助于巩固我所说的内容。我希望这有帮助!
https://stackoverflow.com/a/11658296/2498017

Designing your own protocol is no simple matter. However, building it on top of TCP/IP makes it easier. You pointed out that RFCs are rules about a protocol and you're right. However, that's generally what a protocol is: a set of rules and agreements of how something is to be done. The beauty here is that you get to specify your own rules. Some of the things you need to consider are the type(s) of data to be sent, the length of each piece, and state (if any). HTTP for example is a stateless protocol with a header that defines the request or result, and a payload defining the data being sent which could be a form post or an html page.

So you will need to define the data to be sent or received, the length of the data in bytes which can be defined by the datatype or the amount of data expected. If there is a state to your protocol - i.e. something must first be sent and received before something else can - then you need to define that state. Beyond that, it's simple network programming (sending and receive data in your application).

For example a simple protocol could have this:

Command 1 Byte
Length  4 Bytes
Data    Length Bytes

The first two define your header which contains metadata about the data the the other end needs to be able to read everything. The last would be the actual data. Command, though not necessary, serves to show that you can have the other side perform a specific action with the data depending on the byte received. The lengths of the data is important because the other end must know when it's read all of the data in before it can work on it.

This answer really helps to solidify what I'm saying. I hope this helps!
https://stackoverflow.com/a/11658296/2498017

帅气尐潴 2024-10-16 18:35:24

我建议不要在 C 中这样做。有更简单的方法可以解决这个问题。尝试访问:www.twistedmatrix.com 并查看他们的 Python Web 框架。如果你会写 C 语言,那么你当然可以毫不费力地写 Python。此外,Twisted 还解决了通常与 C 等较低级语言的网络编程相关的大部分问题。

I would suggest not doing this in C. There are much easier ways to go about this. Try taking a look at: www.twistedmatrix.com and looking at their web framework for Python. If you can write C you certainly can do Python no sweat. Also, twisted takes care of most of the nastiness that is normally associated with network programming in lower level languages like C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文