C 通过插座发送数据包

C++ sending a packet over socket

本文关键字:数据包 插座      更新时间:2023-10-16

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

Little Endian签名的INT-> ID的大小(4个字节) 类型的大小(4个字节) 车身大小(null终结器的最小值1) null字节至少为10,为值; <<<<<<<<

Little Endian签名int-> ID

Little Endian签名int->数据包类型

null终止ASCII字符串 -> Body

null字节

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

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

如注释中所述,您是:

  • 制作假设关于编译器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;