3

求助关于局域网 Socket 传送文件时好时坏的问题

 2 years ago
source link: https://www.v2ex.com/t/835264
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Linux

求助关于局域网 Socket 传送文件时好时坏的问题

  llr8031 · 1 天前 · 879 次点击

Client 树莓派 4B 2G 运行 debian 11 x64 Server Macbook macOS 两者采用有线连接至同一台交换机中

在传输文件时有时成功,有时失败。失败时在终端打印乱码,但对于同一文件打印出来的乱码相同,个人猜测是编码问题,但是 debian 和 macOS 用的应该都是 UTF-8 ?而且传字符串没有问题

下面给出 Server 端和 Client 端的代码

//Server 接收端
struct PacketHeader {
    int size;            // 大小:字节
    PacketHeader() {
        size = 0;
    }
};

void *receiveMsg(void *sock) {
    // system("pwd");
    char buffer[1024];
    int *socket = (int *) sock;
    while (1) {
        usleep(500);
        int nRecv = recv(*socket, buffer, 1024, 0);
        if (nRecv <= 0) {
            continue;
        } else if (nRecv == sizeof(PacketHeader)) {
            PacketHeader ph;
            memcpy(&ph, buffer, nRecv);
            cout << "大小:" << ph.size << "Bytes" << endl;
            string filename = ("../Received/" + getCurrentTime() + ".jpg");
            FILE *fp = fopen(filename.c_str(), "wb");
            if (fp == NULL) {
                cout << "file not found" << endl;
            }
            cout << "开始接收图片" << endl;
            nRecv = 0;
            while (nRecv < ph.size) {
                usleep(500);
                memset(buffer, 0, sizeof(buffer));
                int byteCount = recv(*socket, buffer, 1024, 0);
                if (byteCount <= 0) {
                    continue;
                }
                fwrite(buffer, 1, byteCount, fp);
                nRecv += byteCount;
                cout << "已接收 " << nRecv << " Bytes" << endl;
            }
            cout << "共接收 " << nRecv << " Bytes" << endl;
            fclose(fp);
        } else {
            buffer[nRecv] = '\0';
            cout << buffer << endl;
        }
    }
    
# Client 发送端
struct PacketHeader
{
    int size; // 大小:字节
    PacketHeader()
    {
        size = 0;
    }
};

void *sendMsg(void *socket)
{
    int sock = *((int *)socket);
    while (1)
    {
        char msg[4096];
        string filename;
        cin >> filename;
        FILE *fp = fopen(("../image/" + filename).c_str(), "rb");
        if (fp == NULL)
        {
            cout << "file not found" << endl;
            continue;
        }
        int sz = FileSize(("../image/" + filename).c_str());
        cout << "文件打开成功,大小:" << sz << " Bytes" << endl;

        PacketHeader ph;
        ph.size = sz;
        send(sock, (const char *)&ph, sizeof(ph), 0);
        cout << "已发送头部信息" << endl;

        int nSend = 0;
		char buffer[1024];
	    while(nSend < sz)
        {
            int nBytes = fread(buffer, sizeof(char), sizeof(buffer), fp);
            if (nBytes <= 0)
                break;
            send(sock, buffer, nBytes, 0);
            cout << "已发送 " << nBytes << " Bytes" << endl;
            nSend += nBytes;
        }
        cout<<"总计发送 "<<nSend<<" Bytes"<<endl;
		fclose(fp);
        cout << "successfully send " + filename << endl;
    }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK