开发者

Send string with binary null '\0'

Hi I have programmed linux daemon who sends files in udp packets. problem is that in string "abc\0asdf" it sends only abc not null character and asdf (all characters after null symbol),

there is udp client code, which send packets:

int sock;
    struct sockaddr_in server_addr;
    struct hostent *host;
    host= (struct hostent *) gethostbyname((char *)ip);
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
        perror("socket");
        exit(1);
    }
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    memset(server_addr.sin_zero,0,8);

and code which send buffer:

if (sendto(sock, buf, sizeof buf, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))==-1)

in serverside I need to receive binary buffer:

defining socket code:

int sock;
    int addr_len, bytes_read;
    struct开发者_StackOverflow社区 sockaddr_in server_addr , client_addr;
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    //bzero(&(server_addr.sin_zero),8);
    memset(server_addr.sin_zero,0,8);
    if (bind(sock,(struct sockaddr *)&server_addr,
    sizeof(struct sockaddr)) == -1){
        perror("Bind");
        exit(1);
    }
    addr_len = sizeof(struct sockaddr);

                            diep("sendto()");

and receive buffer (in big loop):

bytes_read = recvfrom(sock,buf,sizeof (buf),0,
            (struct sockaddr *)&client_addr, &addr_len);

does anyone know why I didn't receive full buffer?


By looking at the comments, the error is most likely that you treat the received buffer as a string.

If you want to print/output the buffer, you need to convert the null character into something else first.


You should use a for loop to print your received buffer instead of printf:

 for (int i=0; i<bytes_read; i++)
     printf("%c",buf[i]);


This is incorrect (formatting changed so it fits on a screen for me):

if (sendto(sock,
           buf,
           sizeof buf,
           0,
           (struct sockaddr *)&server_addr,
           sizeof(struct sockaddr))==-1)

You want sizeof(server_addr) as the length. This will be larger than sizeof(struct sockaddr).

Also, from the manpage:

Return Value On success, these calls return the number of characters sent. On error, -1 is returned, and errno is set appropriately.

You haven't accounted for the case where it returns some value less than sizeof(buf). Not sure how that can happen but it seems to be something to handle.

My comment on the overall approach is similar to what @jgauffin says. buf is just bytes. It's only a convention for C strings that '\0' terminates them, not a requirement. Typically when using binary byte buffers you also track the size. You're just assuming that all of sizeof(buf) will be used which doesn't make sense. (Suggestion: Perhaps part of your sendto payload should include the size of the message that follows?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜