通过套接字发送时字符变为int

时间:2013-12-08 09:17:46

标签: c++ sockets character-encoding

好的,我正在通过套接字发送原始数据。

unsigned char data [] = {0xFA, 0xDE, 0xDB, 0xAD, 0x00, 0x00, 0x00, 0x06, 0x54, 0x65, 0x61, 0x67,0x61, 0x6E, 0x19, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x68, 0x6F, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79, 0x6F, 0x75, 0x20, 0x66, 0x62, 0x67, 0x67, 0x6F, 0x74, 0x3F };

最终与此相同。

但是当它被收到时它会像这样结束

fffffffa ffffffde ffffffdb ffffffad 0 0 0 6 54 65 61 67 61 6e 19 48 65 6c 6c 6f 20 68 6f 77 20 61 72 65 20 79 6f 75 20 66 62 67 67 6f 74 3f 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

注意原始的前4个字节如何变为16个字节。

知道造成这种情况的原因是什么?

一个小代码

char data[512];
if ( socket.Receive(address, data, sizeof(data)) ){
    char *point = data;
    if ( (unsigned int ) *point == 0xFADEDBAD )
            fprintf(stderr, " WIN \n");

///////////////////////上面的接收功能///////////////////

            int Receive( Address & sender, void * data, int size )
            {
                    assert( data );
                    assert( size > 0 );

                    if ( socket == 0 )
                            return false;


                    #if PLATFORM == PLATFORM_WINDOWS
                    typedef int socklen_t;
                    #endif

                    sockaddr_in from;
                    socklen_t fromLength = sizeof( from );

                    int received_bytes = recvfrom( socket, (char*)data, size, 0, (sockaddr*)&from, &fromLength );

                    if ( received_bytes <= 0 )
                            return 0;

                    unsigned int address = ntohl( from.sin_addr.s_addr );
                    unsigned int port = ntohs( from.sin_port );

                    sender = Address( address, port );

                    return received_bytes;
            }

    private:

            int socket;
    };

}

////////数据如何发送//////////////////

unsigned char data [] = {0xFA, 0xDE, 0xDB, 0xAD, 0x00, 0x00, 0x00, 0x06, 0x54, 0x65, 0x61, 0x67,0x61, 0x6E, 0x19, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x68, 0x6F, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79, 0x6F, 0x75, 0x20, 0x66, 0x61, 0x67, 0x67, 0x6F, 0x74, 0x3F };

socket.Send (address, (char*)data, sizeof (data)  );

////////////////方法////////////////

 bool Send( const Address & destination, const void * data, int size )
            {
                    assert( data );
                    assert( size > 0 );

                    if ( socket == 0 )
                            return false;

                    sockaddr_in address;
                    address.sin_family = AF_INET;
                    address.sin_addr.s_addr = htonl( destination.GetAddress() );
                    address.sin_port = htons( (unsigned short) destination.GetPort() );

                    int sent_bytes = sendto( socket, (const char*)data, size, 0, (sockaddr*)&address, sizeof(sockaddr_in) );

                    return sent_bytes == size;
            }

1 个答案:

答案 0 :(得分:2)

这不是将字符转换为int的套接字,当你收到签名(!你没有声明数据未签名!)字符时,就是你。您没有显示输出“坏”十六进制值的部分,但我认为它类似于

printf("%02x", data[i])

取数据[i]中的(signed!)字符,将其转换为4字节整数(可能在-128和127之间,0xffffff80到0x0000007f,以十六进制表示),然后输出该整数。

在接收方声明“unsigned char data [512]”将解决这个问题。

另外,你的

if ( (unsigned int ) *point == 0xFADEDBAD )

错了,应该读

if ( *(unsigned int *) point == 0xFADEDBAD )

你没有使用*指向访问(1字节)字符,然后将此字符转换为int;你想将指针转换为指向4字节整数的指针,然后通过指针访问这4个字节。

但是,根据您的硬件,您仍可能遇到字节顺序和对齐问题。如果您不了解这些条款,请使用Google。