两个协议的相同TCP连接 [英] Same TCP connection for two protocols

查看:97
本文介绍了两个协议的相同TCP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++编写客户端服务器应用程序。这些应用程序使用TCP套接字相互通信。我想使用两个应用程序协议进行通信。一个二进制和一个基于文本的协议。有两种方法。我可以为每个协议使用两个连接,或者可以为这两个协议使用相同的连接。



两个协议使用相同连接的优缺点是什么?



我的尝试:



NA

两个协议使用相同连接的优缺点是什么?

I am writing a client server application in C++. These applications talk to each other using TCP sockets. I want to use two application protocols to communicate. One binary and one text based protocol. There are two ways. I can use two connections for each protocol or can use the same connection for both the protocols.

What are the pros and cons of using same connection for the two protocols?

What I have tried:

NA
What are the pros and cons of using same connection for the two protocols?

推荐答案

如果你对两个协议使用单个连接,那么你只能严格地说一个协议,因为你的客户必须决定必须使用哪种数据处理,这个决定是由协议定义的。



如果这两个协议不是标准协议但是你定义的只是将它们组合成一个。每个协议通常都有某种标头,其中包含有关以下数据的信息。定义这样的标题,指示数据是文本还是二进制。



使用标准协议时,您也可以这样做。使用指定数据类型的标头定义您自己的协议。



使用单个连接应该比使用关于代码的两个连接更简单。创建和解析标题只需要几行代码,而处理两个连接则更复杂。



所以我会根据保持简单来解决这个问题。 规则。



但是,可能会出现使用两个连接可能更合适的情况。例如,参见使用控制和数据连接的FTP。
If you use a single connection for two protocols you would have strictly speaking only one protocol because your client has to decide which kind of data handling has to be used and this decision is defined by the protocol.

If the two protocols are not standard ones but defined by you just combine them into one. Each protocol usually has some kind of header that contains information about the following data. Define such a header that indicates if the data are text or binary.

When using standard protocols you can do the same. Define your own protocol with a header that specifies the type of data.

Using a single connection should be simpler than using two connections regarding the code. Creating and parsing a header requires only a few lines of code while handling two connections is more complex.

So I would go for this solution according to the "Keep it simple" rule.

However, there may be situations where using two connections might be more appropriate. See for example FTP which uses a control and data connection.


这篇关于两个协议的相同TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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