开发者

Sending data byte by byte in C sockets

My code:

void send_message(const Connection* conn, const NetworkMessage *message)
{
    Connection *c = (Connection *)conn;
    int32_t i =0;
    CHECK_NMSG()
    if (!conn || conn->closed) {
        alog("error trying to send network message, connection dead?\n");
        return;
    }
#if _DEBUG
    alog("Sending NetworkMessage, size = %u\n",message->size);
#endif
    do {
       send_socket(c,&message->buffer[i]);
       i++;
#if _DEBUG
        alog("send_message():\n\tmessage->buffer[i] = %c\n",message->buffer[i]);
#endif
    } while (i < message->size && i < NETWORKMESSAGE_MAX_SIZE);
#if _DEBUG
   alog("NetworkMessage sent\n");
#endif
}

What it outputs in console is:

[18:03:04.545] Sending NetworkMessage, size = 16
[18:03:04.546] send_message():
        message->buffer[i] = [18:03:04.548] send_message():
        message->buf开发者_JAVA技巧fer[i] = [18:03:04.549] send_message():
        message->buffer[i] = H
[18:03:04.550] send_message():
        message->buffer[i] = e
[18:03:04.552] send_message():
        message->buffer[i] = l
[18:03:04.553] send_message():
        message->buffer[i] = l
[18:03:04.554] send_message():
        message->buffer[i] = o
[18:03:04.555] send_message():
        message->buffer[i] = 9
[18:03:04.556] send_message():
        message->buffer[i] = [18:03:04.556] send_message():
        message->buffer[i] = [18:03:04.557] send_message():
        message->buffer[i] = 1
[18:03:04.558] send_message():
        message->buffer[i] = 2
[18:03:04.559] send_message():
        message->buffer[i] = 7
[18:03:04.560] send_message():
        message->buffer[i] = .
[18:03:04.561] send_message():
        message->buffer[i] = 0
[18:03:04.563] send_message():
        message->buffer[i] = .
[18:03:04.565] NetworkMessage sent

What i receive is:

5Hello9ello9llo9lo9o99127.0.0.127.0.0.17.0.0.1.0.0.10.0.1

What i expect is:

5Hello9127.0.0.1

any ideas? if more code needed, ill post


These two strings walk into a bar and sit down. The bartender says, "So what'll it be?"

The first string says, "I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu"

"Please excuse my friend," the second string says, "He isn't null-terminated."

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜