在c ++中设计文件传输协议 [英] Designing a file transfer protocol in c++

查看:131
本文介绍了在c ++中设计文件传输协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C ++中编写一个简单的文件传输协议,将文件从客户端上传或下载到服务器。现在,我的客户端和服务器应用程序都是全面的脚本。我想要在类/方法中模块化所有内容,所以我可以更容易地修改事情。

I need to write a simple file transfer protocol in C++ to upload or download a file from a client to a server. Right now both my client and server applications are do-it-all scripts. I want to modularize everything in classes/methods so I can more easily modify things later.

要上传文件,我需要用户发送文件的名称(无身份验证),文件名,文件大小和文件内容。这是我在客户端所在的:

To upload a file, I need the name of user sending file (no authentication though), file name, file size, and file contents. This is what I have on client side:

int sendFile(string fileName, SOCKET s) {
    ifstream file;
    int ibytesent;
    int size;
    char fileSize[packetSize];

    // Open file, set get pointer at end of file
    file.open (fileName.c_str(), ios::binary | ios::ate);

    // Get file size and pass it to buffer
    size = (int) file.tellg();

    //itoa(size, fileSize, packetSize);
    sprintf(fileSize, "%d", size);

    // Set pointer to beginning
    file.seekg(0, ios::beg);

    // Send file size
    if (send(s, fileSize, packetSize, 0) == SOCKET_ERROR) {
        throw "Send failed\n";
    }

    cout << "Size sent: " << size;

    // Buffer the file Name
    char fileNameBuffer[packetSize];

    sprintf(fileNameBuffer, fileName.c_str());

    // Send the file name
    if (send(s, fileNameBuffer, packetSize, 0) == SOCKET_ERROR) {
        throw "Send failed\n";
    }

    cout << "File name sent: " << fileName << endl;

    char buf[packetSize];
    ibytesent = 0;
    // Loop until the whole file is sent
    while(file.good()) {
        // Read next packetSize bytes into buf
        file.read(buf, packetSize);

        // Send the read data
        ibytesent += send(s, buf, packetSize, 0);

        if (ibytessent == SOCKET_ERROR) {
            throw "Send failed\n";
        }
    }

    cout << "File sent.\n";
    //wait for reception of server response.
    ibytesrecv=0;
    /*
    if((ibytesrecv = recv(s,szbuffer,128,0)) == SOCKET_ERROR)
        throw "Receive failed\n";
    else
        cout << "File received.";
     */
    file.close();

    return 1;
}

然后有一个main()方法打开一个套接字并连接到服务器。我的问题真的是关于如何设计/实现一个正确的文件传输协议。什么是好的结构?我也想知道你对如何将文件分解(和发送)成一个数据包结构,而不是我现在的方式的意见。我不一定需要代码,但是将问题分解成这个和该类等。

And then there is a main() method that opens up a socket and connects to the server. My question really is about how to design/implement a proper file transfer protocol. What is a good structure for it? I would also like to know your opinions about how to split (and send) a file into a packet structure rather than the way I do it now. I don't necessarily need code, but separation of concerns into this and that class, etc.

请不要建议已经存在的协议,因为这不是

Please don't recommend an already existing protocol since that isn't the point of this exercise.

感谢

编辑我发现类似的东西我在这里寻找什么: http://www.eventhelix.com/realtimemantra/ PatternCatalog / protocol_layer.htm

EDIT I kind of found something similar to what I'm looking for here: http://www.eventhelix.com/realtimemantra/PatternCatalog/protocol_layer.htm

推荐答案

以下是可能的课程:


  • 发件人和接收者的发送者和收件人接口

  • ISender and IReceiver interfaces for senders and receivers

StreamSender和StreamReceiver具有ISender的实现和IReceiver(使用套接字我猜)

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

IDataSource接口从源头获取数据

IDataSource interface to get data from source

FileReader具体实现IDataSource读取文件f光盘进入StreamSender

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

示例:

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.

这篇关于在c ++中设计文件传输协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆