Designing a file transfer protocol in c++ [closed]

▼魔方 西西 提交于 2019-12-06 14:32:27

Here's a take on your possible classes:

  • ISender and IReceiver interfaces for senders and receivers

  • StreamSender and StreamReceiver concrete implementations of ISender and IReceiver respectively (using sockets I guess)

  • IDataSource interface to get data from source

  • FileReader a concrete implementation of IDataSource to read file off disk to be fed into StreamSender

Example:

ISender sender=new StreamSender(new FileReader("filename.txt"));
sender.setDest(new StreamReceiver(host));
sender.setName(senderName);
sender.send();

Obviously this is just an example off the top of my head. There will be missing pieces.

hmjd

(I think this what you were asking for)

A simple protocol could be to precede the file content with a list of name=value pairs that define attributes of the file being sent, with an empty name=value pair terminating the list. A name=value pair is terminated by a newline character.

For example:

sending-user-name=userA\n
file-name=filea.txt\n
is-binary=false\n
byte-count=442\n\n
<file-content-here>

sending-user-name=userZ\n
file-name=filez.zip\n
is-binary=true\n
byte-count=1087\n\n
<file-content-here>

The server code would process the attributes and would be able to open the file in correct mode (binary or not) and would know exactly how many bytes it has to read.

As commented by Narrakan look into non-blocking sockets and select(), which you can use to determine when a socket is readeable or writeable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!