Reputation: 2785
I' m developing a simple client/server application (you can think of an ftp-like program).
Once connected to the server, the client sends a message like this:
GET filename
The filename length can of course vary. How can i be sure to create a variable enough big to store the filename string?
char buffer[512];
recv(buffer, sizeof(buffer), 0);
Upvotes: 0
Views: 678
Reputation: 28685
You could say file name length will never exceed some number say 128. Then just declare array with that length.
Otherwise first send the length of the file name, and then the file name. Since length of the file name is integer, its length is fixed, hence you know that say first 2 bytes represent the length of the file name (mind enianness). Afterwards, when you read file name length, you can use malloc
to allocate space enough for the file name.
This seems weird too
sizeof(buffer-1)
buffer
decays to pointer of the first element of array, I doubt you want to subtract one from that.
Note: recv
may receive fewer bytes than requested, so you may have to loop (and parse) till you receive necessary number of bytes.
Upvotes: 2