在C套接字中逐字节发送数据

时间:2011-04-14 16:05:55

标签: c

我的代码:

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
}

它在控制台中输出的内容是:

[18:03:04.545] Sending NetworkMessage, size = 16
[18:03:04.546] send_message():
        message->buffer[i] = [18:03:04.548] send_message():
        message->buffer[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

我收到的是:

5Hello9ello9llo9lo9o99127.0.0.127.0.0.17.0.0.1.0.0.10.0.1

我的期望是:

5Hello9127.0.0.1

任何想法?如果需要更多代码,生病后

1 个答案:

答案 0 :(得分:4)

这两根弦走进一个酒吧坐下。酒保说:“那又怎样呢?”

第一个字符串上写着,“我想我会喝啤酒的时候有一个啤酒嘎嘎咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜咕噜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜

“请原谅我的朋友,”第二个字符串说:“他不是空终止的。”