C ++通过套接字发送数据包

时间:2017-12-08 17:57:22

标签: c++ sockets

我正在尝试与我安装的服务器程序进行通信。服务器以构造的数据包的形式发送和接收所有数据,这些数据包遵循以下设置: int int int string nullbyte 像这样:

little endian signed int - > ID的大小(4个字节)+类型的大小(4个字节)+ Body的大小(空终止符的最小值为1)+空字节的最小值为10;

little endian signed int - > ID

little endian signed int - >包类型

Null终止ascii字符串 - >体

null byte

我设法读取数据包就好了但是当我尝试使用密码发送数据包时,服务器完全忽略它,这意味着数据包是错误的。我像这样构建数据包:

void Packet::build(){

/*
 * Create unsigned char vector to store
 * the data while we build the byte array
 * and create a pointer so the byte array can
 * be modified by the required functions.
 */
std::vector<unsigned char> packet(m_size);
unsigned char *ptr = packet.data();

/*
 * Convert each of the three integers as well
 * as the string into bytes which will be stored
 * back into the memory that ptr points to.
 *
 * Packet data follows format:
 * int32 -> Size of ID + Server Data + body (minimum of 10).
 * int32 -> ID of request. Used to match to response.
 * int32 -> Server Data type. Identifies type of request.
 * String -> Minimum of 1 byte for null terminator.
 * String -> Null terminator.
 */
storeInt32Le(ptr, m_sizeInPacket);
storeInt32Le(ptr, m_requestID);
storeInt32Le(ptr, m_serverData);
storeStringNt(ptr, m_body);

/*
 * Store the vector in member variable m_cmdBytes;
 *
 */
m_cmdBytes = packet;
}

storeInt32Le:

void Packet::storeInt32Le(unsigned char* &buffer, int32_t value) {
/*
 * Copy the integer to a byte array using
 * bitwise AND with mask to ensure the right
 * bits are copied to each segment then
 * increment the pointer by 4 for the next
 * iteration.
 */
buffer[0] = value & 0xFF;
buffer[1] = (value >> 8) & 0xFF;
buffer[2] = (value >> 16) & 0xFF;
buffer[3] = (value >> 24) & 0xFF;
buffer += 4;
}

storeStringNt:

void Packet::storeStringNt(unsigned char* &buffer, const string &s) {
/*
 * Get the size of the string to be copied
 * then do a memcpy of string char array to
 * the buffer.
 */
size_t size = s.size() + 1;
memcpy(buffer, s.c_str(), size);
buffer += size;

}

最后,我发送它:

bool Connection::sendCmd(Packet packet) {
unsigned char *pBytes = packet.bytes().data();
size_t size = packet.size();

while (size > 0){
    int val = send(m_socket, pBytes, size, 0);

    if (val <= 0) {
        return false;
    }

    pBytes += val;
    size -= val;
}

return true;
}

Packet :: bytes()只返回m_cmdBytes

1 个答案:

答案 0 :(得分:0)

如评论中所述,您是:

  • 对编译器int数据类型的字节大小和字节序进行假设。由于您的协议需要非常特定的字节大小和endian作为整数,因此您需要在准备数据包时强制您的代码遵循这些要求。

  • 错误地使用memcpy()。您已将源缓冲区和目标缓冲区反转。

  • 无法确保send()实际上正确发送完整数据包。

尝试更像这样的事情:

void store_uint32_le(unsigned char* &buffer, uint32_t value)
{
    buffer[0] = value & 0xFF;
    buffer[1] = (value >> 8)  & 0xFF;
    buffer[2] = (value >> 16) & 0xFF;
    buffer[3] = (value >> 24) & 0xFF;
    buffer += 4;
}

void store_string_nt(unsigned char* &buffer, const std::string &s)
{
    size_t size = s.size() + 1;
    memcpy(buffer, s.c_str(), size);
    buffer += size;
}

...

std::vector<unsigned char> packet(13 + m_body.size());

unsigned char *ptr = packet.data(); // or = &packet[0] prior to C++11
store_uint32_le(ptr, packet.size() - 4);
store_uint32_le(ptr, m_requestID);
store_uint32_le(ptr, m_serverData);
store_string_nt(ptr, m_body);

...

unsigned char *pBytes = packet.data(); // or = &packet[0] prior to C++11
size_t size = packet.size();
while (size > 0)
{
    int val = send(m_socket, pBytes, size, 0);
    if (val < 0)
    {
        // if your socket is non-blocking, enable this...
        /*
        #ifdef _WINDOWS // check your compiler for specific defines...
        if (WSAGetLastError() == WSAEWOULDBLOCK)
        #else
        if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR))
        #endif
            continue;
        */

        return false;
    }

    if (val == 0)
        return false;

    pBytes += val;
    size -= val;
}

return true;