By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,841 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,841 IT Pros & Developers. It's quick & easy.

Variable Length Packet

P: n/a
Hi,

I am using read and send of the socket library. I would like to send packets
that have variable length, and I would like to read these packets. I have
designated the first 2 bytes to state the size of the packet.

So far, my program works when I force the packet size to be the size of the
buffer. However, when I try and change the buffer size to correspond to the
packet size, the receiving end does not work. I try to receive the first two
bytes in order to read the size. This works. However, when I read from the
buffer again to get the rest of the packet it does not work.

Bytes_sent = send(server_socket, sbuffer, *sbuffer, 0);

Where *sbuffer stores the message size, thereby sending the full message.

Let's say that the message is 7 bytes, where the first 2 bytes state the
message size.

On the receiving end I want to read the message size, and then read
according to how many bytes are left. So I have the following:

Bytes_received = read(client_socket, rbuffer, 2);

Size_of_packet = *rbuffer;

Bytes_received += read(client_socket, (rbuffer+2), (Size_of_packet-2));

This code does not work. I am not able to read the packet.

Please help.

Thanks,

Marcia Hon
Nov 14 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.