C++ 每次发送一些字节

C++ 每次发送一些字节,c++,c,C++,C,我正试图找出一种方法,从字符串ech time发送一定量的文本,直到它到达字符串末尾,例如: const char*the_string=你好,世界,很高兴认识你们。让我们成为朋友或者更多,但不能少 输出:你好,世界 输出:,很高兴见到大家 输出:让我们成为朋友或者更多 输出:,但不少于 停止:不再发送字节 这个问题我在谷歌上搜索过,但不理解其中的例子,我花了4天时间试图找到一个好方法,也就是每次发送5个字节,但如果有更少的字节,那么就发送它们,直到你到达字符串的末尾 请帮助我,我会接受C或C+

我正试图找出一种方法,从字符串ech time发送一定量的文本,直到它到达字符串末尾,例如:

const char*the_string=你好,世界,很高兴认识你们。让我们成为朋友或者更多,但不能少

输出:你好,世界

输出:,很高兴见到大家

输出:让我们成为朋友或者更多

输出:,但不少于

停止:不再发送字节

这个问题我在谷歌上搜索过,但不理解其中的例子,我花了4天时间试图找到一个好方法,也就是每次发送5个字节,但如果有更少的字节,那么就发送它们,直到你到达字符串的末尾


请帮助我,我会接受C或C++方式,只要它工作和解释得好。C++中的

< P>,可以使用子字符串子方法来选择要发送的字符串的一部分。在c语言中,您必须手动迭代字符,当到达零或发送了所需的字节数时停止,或者将字符数组的一部分复制到另一个以0结尾的数组并发送该数组

例如,您可以一次发送10个字符,如下所示:

string str = randomstaff.from(whereveryoulike);
for (int i = 0; i < str.size(); i += 10)
{
    destination << str.substr(i, i + 10 < str.size() ? i + 10 : str.size());
}

根据评论中的讨论,我相信OP所寻找的是一种能够通过套接字发送全部数据的方式

使用C++和模板,通过套接字发送任意数据,用于此代码示例,我将使用Winsock相当简单。p> 通用发送功能:

利用这些功能的示例如下:

std::string szSample = "hello world, i'm happy to meet you all. Let be friends or maybe more, but nothing less";

// Note that this assumes that sSock has already been initialized and your connection has been established:
SendData( szSample, sSock );

希望这有助于实现您的目标。

这里有一个C语言的解决方案。希望我理解您的问题

void send_substr(
    const char * str,
    size_t len,
    const size_t bytes_at_a_time,
    void (*sender)(const char *)
    )
/*
   sender() must check the char * manually for
   null termination or call strlen()

   for Unicode just change all size_t to unsigned long
   and all const char * to const wchar_t * (POSIX)
   or LPCWSTR (Win32)
 */
{
  size_t i, index_to_end, tail;

  //for C99 (gcc)
  char ret[bytes_at_a_time];

  //for C89 (Visual C++)
  //char * ret = (char *) malloc(sizeof(char)*bytes_at_a_time);

  tail = len % bytes_at_a_time;
  index_to_end = len - tail;

  for(i = 0; i < index_to_end; i += bytes_at_a_time)
  {
    memcpy(ret, str+i, bytes_at_a_time);
    *(ret + bytes_at_a_time) = '\0';
    (*sender)(ret);
  }
  memcpy(ret, str+index_to_end, tail);
  *(ret + tail) = '\0';
  (*sender)(ret);
  //for C89
  //free(ret);
}

void print_substr(const char * substr)
{
  while(*substr != '\0')
  {
    putchar(*substr);
    substr++;
  }
  putchar('\n');
}

int main()
{
  char test[] = "hello world, i'm happy to meet you all."
    " Let be friends or maybe more, but nothing less";
  send_substr(test, sizeof(test)/sizeof(*test), 5, &print_substr);

  return 0;
}

你能澄清一下你希望如何发送文本吗?可能通过套接字?单词more去了哪里?Cole Johnson,请帮助我,而不是-1它不必一次是5字节,但只是一个例子,只是一个限制器,但如果它小于5,它应该发送其余的,直到它结束。如果您使用流协议(如TCP)发送信息,那么它将根据操作系统TCP/IP堆栈进行打包。你想要一种方法来发送整个字符串/数据,而不是只发送一点点吗?你可能会在这里看到substr的用法:这对我没有帮助,在这个例子中,他们已经知道字符串包含的单词,但在我的程序中它是未知的,因为它会将随机原始图像中的数据保存在字符串中…这应该没有问题。例如,如果要将字符串拆分为大小为10的片段,可以将开始位置设置为i,将结束位置设置为最小值i+10,并将字符串的长度也存储在字符串中。你将i从0迭代到字符串的长度,每一步递增10。Vincent请给我举个例子,这不是家庭作业,而是我想学的东西,请告诉我如何使用代码。。我马上就接受你!还有一条评论,这个网站并不是专门为绝对初学者设计的。如果你刚刚开始学习,下次请在提问之前多看一点,并尝试从优秀的书籍和教程中了解基础知识。否则我很乐意帮忙。谢谢,非常感谢!这正是我需要学习的遗憾的是,我不能两者都选,但我给你们两个+1和picked,这段代码是有效的best@user1417815没问题,很乐意帮忙;只要确保您通读了代码并理解每一行的作用,这将帮助您学得最好嗯,再考虑一下,ret应该放在堆栈上。我修复了ret,但它只适用于C99 gcc。如果你使用MS VisualC++ +没有C99支持,注释掉字符Re[拜斯特拉特AyTime];但是,如果C99方法是C++文件,那么C99方法将与MSVC一起工作。@ USE142071:但是,如果C99文件是C++文件,那么C99方法将与它无关,它不会;C++不支持VLAs。
template <>
int                 SendData( const std::string& szString, SOCKET sSock )
{
    // Send the size first:
    int iResult = SendData( static_cast<unsigned int>(szString.length()) * sizeof(char) + sizeof('\0'), sSock );

    if( iResult <= 0 )
        return iResult;

    iResult = SendData(szString.c_str(), static_cast<unsigned int>(szString.length()) * sizeof(char) + sizeof('\0'), sSock);
    return iResult;
}
std::string szSample = "hello world, i'm happy to meet you all. Let be friends or maybe more, but nothing less";

// Note that this assumes that sSock has already been initialized and your connection has been established:
SendData( szSample, sSock );
void send_substr(
    const char * str,
    size_t len,
    const size_t bytes_at_a_time,
    void (*sender)(const char *)
    )
/*
   sender() must check the char * manually for
   null termination or call strlen()

   for Unicode just change all size_t to unsigned long
   and all const char * to const wchar_t * (POSIX)
   or LPCWSTR (Win32)
 */
{
  size_t i, index_to_end, tail;

  //for C99 (gcc)
  char ret[bytes_at_a_time];

  //for C89 (Visual C++)
  //char * ret = (char *) malloc(sizeof(char)*bytes_at_a_time);

  tail = len % bytes_at_a_time;
  index_to_end = len - tail;

  for(i = 0; i < index_to_end; i += bytes_at_a_time)
  {
    memcpy(ret, str+i, bytes_at_a_time);
    *(ret + bytes_at_a_time) = '\0';
    (*sender)(ret);
  }
  memcpy(ret, str+index_to_end, tail);
  *(ret + tail) = '\0';
  (*sender)(ret);
  //for C89
  //free(ret);
}

void print_substr(const char * substr)
{
  while(*substr != '\0')
  {
    putchar(*substr);
    substr++;
  }
  putchar('\n');
}

int main()
{
  char test[] = "hello world, i'm happy to meet you all."
    " Let be friends or maybe more, but nothing less";
  send_substr(test, sizeof(test)/sizeof(*test), 5, &print_substr);

  return 0;
}