IoT
IoT

Reputation: 639

TCP socket sending less number of bytes

I am trying to send a large number of bytes using boost.asio library as following:

void tcp_send(boost::asio::io_service &io, const char *dst_ip, uint16 dst_port)
{
    uint8 *sbuff;
    size_t slen;
    ip::tcp::socket sock(io);

    sock.connect(ip::tcp::endpoint(ip::address::from_string(dst_ip), dst_port));

    sbuff = new uint8[100412];
    sbuff[0] = 67;
    sbuff[1] = 193;
    sbuff[2] = 136;
    sbuff[3] = 60;

    boost::asio::async_write(sock, boost::asio::buffer(sbuff, 100412),
             boost::bind((&send_handler), placeholders::error));
}

When I check the number of transmitted bytes using wireshark, the sender sends always and only 65536 bytes of data excluding TCP header bytes. So what might be the problem? Is there any parameter I need to modify.

I am running the application on linux ubuntu. It seems that the maximum number of transmitted bytes is 2^16.

Wireshark TCP Stream Flow

Upvotes: 0

Views: 1095

Answers (2)

IoT
IoT

Reputation: 639

I found out the source of the problem. First of all, I was trying to send the message by using tcp_Send function and after it returns from the call, I exist the program. Because of this the TCP stack does not have enough time to send all the packets in its buffer. So I should give it some time.

Now in the second trial, I kept the application running after sending the message, but now I got an error in the send_handler, which states bad file descriptor. And this due to that the socket being declared is destroyed after exiting the tcp_send function. So to solve this issue I should define the socket as a pointer. Or the best thing is to use write instead of async_Write

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137438

TCP is a stream. How many bytes appear in the IP datagrams or Ethernet frames should be of no concern to you.

Successfully calling send doesn't necessarily even mean that the data was sent yet; just that the TCP stack accepted the data and promises to send it (unless you specify PSH).

Upvotes: 1

Related Questions