CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    TCP Sending Unsigned Char...

    Hi...

    I want to send over tcp/ip some data, i have the data in hexadecimal, and when i try to send it, i have a few problems.

    I give the data as a parameter to the program... argv[1]

    Example of argv[1] = "0518EF5600AD"

    This is hexadecimal data (05 18 EF 56 00 AD)

    The main problem for me is the Send function...

    Code:
    int send(
      __in          SOCKET s,
      __in          const char* buf,
      __in          int len,
      __in          int flags
    );
    How can i send this data if the 2 parameter in the send funcion is a CHAR... and i need to send some null strings like Hex(00)...

    I would like to send this:

    Code:
    unsigned char info[] = 
    	{
    		0x84, 0xFA, 0xEF, 0x00,
    		0x01, 0xEF, 0x05, 0x31,
    	        0x01, 0x01, 0x01, 0x20,
    		0xF9, 0xC7, 0xDC, 0x79,
    		0x80, 0x99, 0xF1, 0xF2,
    		0xF3 
    	};
    I been trying with some like this:
    Code:
    string sento;
    sento=HexToAscii('02 01 EF 05 31 00 01 00 20 F9 06 02 01 EF 05 33')
    nBytesSent = send(Socket, sento.data(), sizeof(sento), 0);
    And works, but the other side, all the hex data over 79 change... EF >> 6F... check this:

    send: 02 01 EF 05 31 00 01 00 20 F9 06 02 01 EF 05 33
    recv : 02 01 6F 05 31 00 01 00 20 79 06 02 01 6F 05 33

    ...dunno why. With C_Str si the same. I did the same code in Delphi and works perfect... but in C++ i cant. The problem is when i send !

    How can i send the value of "info[]" or correct my program?

    Thanks you !

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: TCP Sending Unsigned Char...

    It's probably hex values over 0x7F. The sign bit is being stripped somehow; probably on the receiving end; possibly by string:ata() (I haven't tested with std::string so I don't know if the data() member might be stripping the sign bit).

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: TCP Sending Unsigned Char...

    Are you sure your HexToAsci works correct?

    Anyhow, this should work:
    Code:
    unsigned char info[] = 
    	{
    		0x84, 0xFA, 0xEF, 0x00,
    		0x01, 0xEF, 0x05, 0x31,
                            0x01, 0x01, 0x01, 0x20,
    		0xF9, 0xC7, 0xDC, 0x79,
    		0x80, 0x99, 0xF1, 0xF2,
    		0xF3 
    	};
    
    nBytesSent = send(Socket, info, sizeof(info), 0);
    For the socket, the data to be sent has no meaning. It is just a sequence of bytes.

    HTH,
    Richard

  4. #4
    Join Date
    Oct 2009
    Posts
    2

    Re: TCP Sending Unsigned Char...

    Yes... the HexToAscii function works fine.

    The problem is simple, Char is -128 to 127... if i send HEX 79 i recive HEX 79, but if i send HEX 80 i recive 00... HEX 79 is DEC 127, so Char limit is 127. Is an UTF problem, encoding problem.

    The function SEND, expect a Char as a second parameter, and i dont know too much C.

    I been trying with String.data() or String.c_str() but is the same... i need some kind of Socket that let me send this data, with unsigned char parameter or string, but i dont know if exist another way.

    Thanks for reply.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: TCP Sending Unsigned Char...

    It's not the socket. Like Richard.J said, send() and recv() don't care what your sending as long as it can be represented by a stream of bytes. Something else either before send() on one end, or after recv() on the other end is stripping the sign bit.

    P.S. 0x79 = 121, not 127. 0x7F is 127.

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: TCP Sending Unsigned Char...

    and the one stripping the sign bit is not the string class, I just tested it.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: TCP Sending Unsigned Char...

    std::string has no problem holding UTF-8 data. It's not that.

  8. #8
    Join Date
    Oct 2009
    Posts
    13

    Re: TCP Sending Unsigned Char...

    remove any std::string (or else) and use array of BYTE type in send and in receive functions.
    My gues is that std::string mess up your data.
    For receive allocate BYTE array and receive data in it.
    As others say, this should work pretty good :
    for send :

    BYTE info[] =
    {
    0x84, 0xFA, 0xEF, 0x00,
    0x01, 0xEF, 0x05, 0x31,
    0x01, 0x01, 0x01, 0x20,
    0xF9, 0xC7, 0xDC, 0x79,
    0x80, 0x99, 0xF1, 0xF2,
    0xF3
    };

    nBytesSent = send(Socket, info, sizeof(info), 0);

    ====================================
    for receive :

    BYTE info_recv[2000];
    int rcv_bytes = recv(sock, info_recv, 2000, /* MSG_WAITALL */ or 0);

    ====================================
    It is good also if you organize some header of each packet of data you send. With at least bytes expected inside. So receiver will know what length to expect.
    Keep in mind that you may need to call recv() several times before to receive whole data that is send.
    i.e. you may (and will) receive your data on several chunks, or 2 or more whole packets can be received at once.
    So you should organize some Q of received data and at each completion of recv() to add data to this Q...then if whole packet (completely defined by you) is received, process it, remove ONLY bytes of processed packet from this Q...and continue to wait for next packets.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured