简体   繁体   English

Visual Studio (C++):如何使用 TCP 套接字发送多个值?

[英]Visual Studio (C++): How to send more than one value with TCP socket?

it's probably a pretty basic question, but I couldn't really find something online so I hope you can help me out here::)这可能是一个非常基本的问题,但我真的无法在网上找到一些东西,所以我希望你能在这里帮助我::)

I'm currently working with C++ in Visual Studio and I'm trying to send different variables via TCP Connection at the same time.我目前正在 Visual Studio 中使用 C++ 并且我正在尝试同时通过 TCP Connection 发送不同的变量。 But I'm not quite sure, what the best way to do it would lookn' like.但我不太确定,最好的方法是什么。

I was thinking about sending a long string at once with all variables and between every variable there could be a symbol which shows the end of the variable, just like:我正在考虑一次发送一个包含所有变量的长字符串,并且在每个变量之间可能有一个显示变量结尾的符号,就像:

123.45@16.45@33@true@..... 123.45@16.45@33@true@.....

but the programm has to run as fast as possible.但程序必须尽可能快地运行。 A algorithm which sorts every income variables doesn't sound pretty efficient, does it?对每个收入变量进行排序的算法听起来效率不高,是吗?

The second thought was to create a socket for every variable I got, with a different Port.第二个想法是为我得到的每个变量创建一个套接字,使用不同的端口。 But I'm not quite sure if that isn't a dirty programming behavior.但我不太确定这是否是一种肮脏的编程行为。

So what do you think?所以你怎么看? Any ideas/experiences?有什么想法/经验吗?

Thanks in Advance!提前致谢!

Before answering this question, we need to be aligned with some concepts.在回答这个问题之前,我们需要与一些概念保持一致。

TCP is a stream protocol. TCP 是一个 stream 协议。 It's like you're accessing a regular file, but socket API encapsulates many complex tasks from users.就像您正在访问一个常规文件,但是套接字 API 封装了来自用户的许多复杂任务。 When you do a write on a socket, you always append to this socket's TCP stream.当您在套接字上进行写入时,您总是将 append 写入此套接字的 TCP stream。 So accessing the same socket from two different threads will not give you what you want, instead two different atomic data segments added to TCP stack (you have just made 2 append operation to the stream).所以从两个不同的线程访问同一个套接字不会给你你想要的,而是添加到 TCP 堆栈的两个不同的原子数据段(你刚刚对流进行了 2 个 append 操作)。

"sending at the same time" may have 2 different meanings: “同时发送”可能有两种不同的含义:

i) sending the variables at the same TCP session. i) 在相同的 TCP session 发送变量。

In this case, TCP session is no different than writing to a binary file.在这种情况下,TCP session 与写入二进制文件没有什么不同。 This is rather a serialization problem than a network protocol problem in that case.在这种情况下,这是一个序列化问题,而不是网络协议问题。 You have many options:你有很多选择:

a) convert everything to the string representation and send this buffer (fairly slow and inefficient). a)将所有内容转换为字符串表示并发送此缓冲区(相当缓慢且效率低下)。 This is the simplest method.这是最简单的方法。 You also need some additional protocol to handle written data as you mentioned (adding @ in between).您还需要一些额外的协议来处理您提到的写入数据(在两者之间添加 @ )。 JSON or XML are good examples of that. JSON 或 XML 就是很好的例子。

b) if you know the exact order to write and read (encode/decode) just write ints, char arrays, shorts, longs as binary (hey float and double serialization may not be that straightforward). b)如果您知道写入和读取(编码/解码)的确切顺序,只需将 int、char arrays、short、longs 写入二进制(嘿,浮点和双序列化可能不是那么简单)。 Decoder will read binary data in the exact same order.解码器将以完全相同的顺序读取二进制数据。 Remember to convert network byte-order if you pick this solution.如果您选择此解决方案,请记住转换网络字节顺序。

ii) sending the variables simultaneously ii) 同时发送变量

If you really want to send many variables at the same time (i mean almost simultaneously), then you'd better to have many threads and many sockets at that case.如果你真的想同时发送许多变量(我的意思是几乎同时),那么在这种情况下你最好有很多线程和很多 sockets。 Maybe you are reading integer sensor data from one sensor which creates thousands of integer in a second, and double values from another sensor.也许您正在从一个传感器读取 integer 传感器数据,该传感器在一秒钟内创建数千个 integer,并从另一个传感器读取双倍值。 The meaning of simultaneously here is that you do not have to wait for reading the whole bunch of integers to read doubles or vice versa.这里同时的意思是你不必等待读取整堆整数来读取双精度数,反之亦然。 This also saves encoding/decoding time.这也节省了编码/解码时间。

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

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