how to send a string with 0x00 in it?

This is a discussion on how to send a string with 0x00 in it? within the C Programming forums, part of the General Programming Boards category; I'm on a program that sends TCP packages, and some characters of the data in the packages are 0x00 which ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Question how to send a string with 0x00 in it?

    I'm on a program that sends TCP packages, and some characters of the data in the packages are 0x00 which is taken as the end of the string. So when the packages was sent, only the characters before 0x00 were in the package.

    Then how can I completely send all the characters including 0x00 and those after it?

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    Who are you sending the packet to? Who is receiving packets and expecting to get 0 in the middle of them?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    send() doesn't care about 0x00.
    If you keep a separate count of the number of bytes to be sent / received, you'll do just fine.

    The problems occur when you try and do things like strlen() on binary data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by Richardcavell View Post
    Who are you sending the packet to? Who is receiving packets and expecting to get 0 in the middle of them?
    In fact I'm writing a client program to chat on omegle.com without using the browser, and I analyzed the package and found that the data of the package are like " 01 74 00 02 ..." and so on. This is where the 0x00 locates.

    Quote Originally Posted by Salem View Post
    send() doesn't care about 0x00.
    If you keep a separate count of the number of bytes to be sent / received, you'll do just fine.

    The problems occur when you try and do things like strlen() on binary data.
    Really? I tried but it seems nothing changed in the package.
    Last edited by ZuckMitnick; 03-11-2011 at 03:20 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well I suppose you could post some actual code to show us how you're sending data, as opposed to us trying to guess which of the myriad ways you could have screwed up.

    Lots of programs and protocols send binary data without an issue, so my guess is that you've done something wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Code:
    void main()
    {
        WSADATA wsaData;
        SOCKET ClientSocket;
        SOCKADDR_IN ServerAddr;
        int ServerPort=1365;
        char ServerIPAddr[50]="69.164.215.124";
        char SendData[20];
        SendData[0]=(char)0x01;
        SendData[1]=(char)0x73;
        SendData[2]=(char)0x00;
        SendData[3]=(char)0x02;
        SendData[4]='h';
        SendData[5]='i';
        SendData[6]='\0';
        printf("%s",SendData);
        char ReceiveBuffer[1024]="";
        int SendLength=-1;
        int Result=-1;
        WSAStartup(MAKEWORD(2,2),&wsaData);
        ClientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        memset(&ServerAddr,0,sizeof(ServerAddr));
        ServerAddr.sin_family=AF_INET;
        ServerAddr.sin_port=htons(ServerPort);
        ServerAddr.sin_addr.s_addr=inet_addr(ServerIPAddr);
        connect(ClientSocket,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr));
        SendLength=send(ClientSocket,SendData,7,0);
        int ReceiveLen;
        memset(ReceiveBuffer,0,sizeof(ReceiveBuffer));
        ReceiveLen=recv(ClientSocket,ReceiveBuffer,1024);
        closesocket(ClientSocket);
        WSACleanup();
    }
    This simply shows the program without the mechanism on error handling.
    btw, i don't know how to combine the hex numbers and the characters in an easy way.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So is SendLength ending up less than seven? IF not, it's sending fine, and it's as Salem said earlier: Your receiver is treating what you receive as a string, when it should instead be treating it as seven random bytes of data. Stop using string functions on things that aren't strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Don't know why, when i applied this yesterday the problem remained here. Today, it has gone.

    Thanks, guys.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    In that case, expect it to come back tomorrow, or the day after.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    You weren't basing your analysis on this faulty printf were you?

    printf("%s",SendData);

    Because this would stop at the \0 at index 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Compile time switch
    By Roaring_Tiger in forum C Programming
    Replies: 13
    Last Post: 09-12-2004, 01:16 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21