简体   繁体   English

通过套接字将数据作为字符串发送的好方法是什么?

[英]What is a good way of sending data as strings through sockets?

As there several ways to exchange data in the form of strings over sockets, such as: 有几种方法可以通过套接字以字符串的形式交换数据,例如:

  • using functions like: 使用如下功能:

    1. sprintf() and sscanf() sprintf()sscanf()
    2. snprintf() and sscanf() snprintf()sscanf()
    3. printf() and strtof() printf()strtof()
  • or converting to char and then passing it as an array 或转换为char然后将其作为数组传递

I would appreciate if you could suggest which way and why it is efficient and better than others, or if there is another way not mentioned above. 如果您可以建议哪种方式以及为什么它比其他方式更有效和更好,或者如果还有其他方法未提及,我将不胜感激。 At the moment I am using the simplest way, I mean sprintf() and sscanf() functions. 目前我使用最简单的方法,我的意思是sprintf()sscanf()函数。 But I read on the web that eg using snprintf() function is more safe. 但我在网上看到,例如使用snprintf()函数更安全。

if you just want to send strings, you can get away with something like this; 如果你只是想发送字符串,你可以逃避这样的事情; it's serialization in a simple form: a header containing the size of the data following.. (pseudo-code) 它以一种简单的形式进行序列化:包含以下数据大小的标头..(伪代码)

Send( socket, const string& str )
{
  const size_t len = str.length();
  send( socket, &len, sizeof( len ) );
  send( socket, str.data(), len );
}

Receive( socket, string& str )
{
  size_t len;
  receive( socket, &len, sizeof( len ) );
  str.resize( len );
  receive( socket, str.data(), len );
}

Edit: see comment 1, a faster Send method would be 编辑:请参阅注释1,更快的发送方法

Send( socket, const string& str, rawmemory& packet )
{
  const size_t len = str.length();
  packet.Reserve( len + sizeof( len ) );
  packet.ResetOffset();
  packet.CopyFrom( &len, sizeof( len ) );
  packet.CopyFrom( str.data(), len );
  send( socket, packet.Data(), packet.Length() );
}

in C++ you can also use StringStream 在C ++中,您也可以使用StringStream

stringstream ss;
 int i = 1;
 float f = "1.0";
 char separtor = ';';

 ss << i << separtor  << f;

you can then extract the string with ss.str().c_str() 然后,您可以使用ss.str().c_str()提取字符串ss.str().c_str()

ss.str().c_str() will result in the case above with ss.str().c_str()将导致上面的情况

"

1;1.0 1; 1.0

"

Have a look at Serialization—How to Pack Data . 看看序列化 - 如何打包数据

It's easy enough to send text data across the network, you're finding, but what happens if you want to send some "binary" data like ints or floats? 您可以轻松地通过网络发送文本数据,但是如果您想发送一些“二进制”数据(如整数或浮点数)会发生什么? It turns out you have a few options. 事实证明你有几个选择。

  1. Convert the number into text with a function like sprintf(), then send the text. 使用类似sprintf()的函数将数字转换为文本,然后发送文本。 The receiver will parse the text back into a number using a function like strtol(). 接收器将使用类似strtol()的函数将文本解析为数字。
  2. Just send the data raw, passing a pointer to the data to send(). 只需发送数据raw,将指针传递给send()。
  3. Encode the number into a portable binary form. 将数字编码为便携式二进制形式。 The receiver will decode it. 接收器将解码它。

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

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