sending zero bytes over TCP socket

This is a discussion on sending zero bytes over TCP socket within the Networking/Device Communication forums, part of the General Programming Boards category; hi, i've been having trouble sending zero bytes (0x00) over a TCP socket in a C prog. Apparently, the null ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    sending zero bytes over TCP socket

    hi,

    i've been having trouble sending zero bytes (0x00) over a TCP socket in a C prog. Apparently, the null byte serves as a terminator and a buffer holding such is "truncated" at the first zero byte instance.

    Anyone have a suggestion round this?

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does your send-code look like. As far as I know, TCP is a binary protocol, and doesn't "care" what your data looks like.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Well, according to the definition of write() the buffer argument is an array of (well 'pointer to' anyway) characters (bytes)...

    NAME
    write - write output

    SYNOPSIS
    int write(fd, buf, nbyte)
    int fd;
    char *buf;
    int nbyte;

    In my case i'm trying to send video byte stream. Everything works ok if the data sent is text based since 0x00 stands for null in ascii. But in my case 0x00 stands for nothing in particular and is occuring very frequently.

    Here's the "write" part of the code. Messy but that's not the point currently. in_buff is filled by a seperate thread.

    unsigned char in_buff[256];
    unsigned char buffer[256];

    ...
    memcpy(buffer, in_buff, 256);
    pthread_mutex_unlock(&mutex);

    n = write(sock,buffer,strlen(buffer));
    if (n < 0) error("ERROR writing to socket");


    thanks for you help!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, char is a generic type that corresponds (in 99% of machines) to byte.

    But the problem with your code is that you are using strlen() to figure out the amount of data to send - strlen works fine on strings, but not on binary "any data", - as you've seen, it stops at a zero-byte. [And actually, if you wanted to transmit a string, you may want to use strlen(buff)+1, to include the zero-byte in the send operation].

    --
    Mats

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    ahhh right as rain you are!
    and i'm plainly embarassed!

    thanks a million!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting a struct for sending to socket
    By chrisjmoss in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-08-2008, 09:11 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Sockets... Sending Binary Data. Please help
    By avalanche333 in forum C++ Programming
    Replies: 16
    Last Post: 03-31-2007, 12:56 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

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