Reputation: 4523
I need to send some files over a socket and I'm trying to figure out how to do it. It's pretty easy to send a single file, but I need to, once I'm connected, send an integer that represent a file unique id and after that send file content. Once completed, I have to send another id with its file content. So I need to have something like this:
Most of the solutions that I've found to send files use a byte[] as buffer and I don't know how to differentiate the id from the file content. What can I do?
Upvotes: 0
Views: 180
Reputation: 229234
You need to delimit the TCP stream, so you can know when one file starts and ends. You need to create your own protocol.
One simple thing to do is to first send a small fixed length header with the file id and file size.
e.g.
This can be repeated as many times you need it to over the same TCP socket.
The receiving end would need to:
Once all the bytes are received, it can start over again, reading a new header of file-id and file-size.
Upvotes: 2