-6

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

const char* the_string = "hello world, i'm happy to meet you all. Let be friends or maybe more, but nothing less"

输出:你好世界

输出: ,我很高兴见到你们。

输出:成为朋友或者更多

输出:,但仅此而已

停止:不再发送字节。

问题我已经搜索了谷歌,但不理解示例,我花了 4 天时间试图找到一个好方法,也是一次发送 5 个字节,但如果有更少,然后发送它们直到你在末尾字符串。

请帮帮我,我会接受 C 或 C++ 方式,只要它有效且解释清楚。

4

3 回答 3

2

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

使用 C++ 和模板,通过套接字发送任意数据(就本代码示例而言,我将使用 WinSock)相当简单。

通用发送功能:

template <typename T>
int                 SendData( const T& tDataBuffer, SOCKET sSock ) 
{
    // Make sure the class is trivially copyable:
    static_assert( std::is_pod<T>::value && !std::is_pointer<T>::value, "The object type must be trivially copyable" );

    char* chPtr = (char*)(&tDataBuffer);

    unsigned int iSent = 0;
    for( unsigned int iRemaining = sizeof(T); iRemaining > 0; iRemaining -= iSent )
    {
        iSent = send( sSock, chPtr, iRemaining, 0 );
        chPtr += iSent;

        if( iSent <= 0 )
        {
            return iSent;
        }
    }


    return 1;
}

指针重载:

template <typename T>
int                 SendData( T* const &ptObj, unsigned int iSize, SOCKET sSock )
{
    // Make sure the class is trivially copyable:
    static_assert( std::is_pod<T>::value, "The object type must be trivially copyable" );

    char* chPtr = (char*)ptObj;

    unsigned int iSent = 0;
    for( unsigned int iRemaining = iSize; iRemaining > 0; iRemaining -= iSent )
    {
        iSent = send( sSock, chPtr, iRemaining, 0 );
        chPtr += iSent;

        if( iSent <= 0 )
        {
            return iSent;
        }
    }

    return 1;
}

专业化std::string

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 );

希望这可以帮助你实现你想要的。

于 2012-06-10T22:37:11.020 回答
1

在 c++ 中,您可以使用子字符串 (substr) 方法来选择要发送的字符串的一部分。在 c 中,您必须手动遍历字符,在达到零或发送所需的字节数时停止,或者将 char 数组的一部分复制到另一个以 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());
}
于 2012-06-10T21:48:44.507 回答
1

这是 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;
}
于 2012-06-11T14:33:02.240 回答