代码不适用于 C++ 中的泛型类型 Vector。<T>

Code does not work with generic type Vector<T> in C++

本文关键字:gt lt Vector 泛型类型 适用于 不适用 C++ 代码      更新时间:2023-10-16

我正在尝试通过此代码将图像发送到服务器,但是该代码在方法void FileSend(const vector&amp; fnames)中分解。我已经用void fileend(cont char* fpath)超载了此功能。当我不超载此功能时,我就可以发送一个映像,但是当我与void fileend(const vector&amp; fnames)超载时,它在line" client.filesend(fileNames);"的main()中停止。我尝试发现这个问题,但找不到。

#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
class Client
{
public:
    SOCKET sock, clientt;
    void s_handle(int s)
    {
        if (sock)
            closesocket(sock);
        if (clientt)
            closesocket(clientt);
        WSACleanup();
        cout << "EXIT SIGNAL :" << s;
        exit(0);
    }
    void s_cl(char *a, int x)
    {
        cout << a;
        s_handle(x + 1000);
    }
    int sendData(char *sendbuf)
    {
        return send(sock, sendbuf, strlen(sendbuf), 0);
    }
    int recvData(char *recvbuf, int size)
    {
        int sz = recv(sock, recvbuf, size, 0);
        recvbuf[sz] = '';
        return sz;
    }
    void fileSend(const char *fpath)
    {
        // Extract only filename from given path.
        char filename[50];
        int i = strlen(fpath);
        for (; i > 0; i--)
        {
            if (fpath[i - 1] == '')
                break;
        }
        for (int j = 0; i <= (int)strlen(fpath); i++)
        {
            filename[j++] = fpath[i];
        }
        ifstream myFile(fpath, ios::in | ios::binary | ios::ate);
        int size = (int)myFile.tellg();
        myFile.close();
        char filesize[10]; itoa(size, filesize, 10);
        send(sock, filename, strlen(filename), 0);
        char rec[32] = ""; recv(sock, rec, 32, 0);
        send(sock, filesize, strlen(filesize), 0);
        recv(sock, rec, 32, 0);
        FILE *fr = fopen(fpath, "rb");
        while (size > 0)
        {
            char buffer[1030];
            if (size >= 1024)
            {
                fread(buffer, 1024, 1, fr);
                send(sock, buffer, 1024, 0);
                recv(sock, rec, 32, 0);
            }
            else
            {
                fread(buffer, size, 1, fr);
                buffer[size] = '';
                send(sock, buffer, size, 0);
                recv(sock, rec, 32, 0);
            }
            size -= 1024;
        }
        fclose(fr);
    }

    void fileSend(const vector<string>& fnames)
    {
        for (int k = 0; k < fnames.size(); k++)
        {
            fileSend(fnames[k].c_str()); // call the original function
        }
    }
};
int main(int argc, char *argv[])
{
    Client client;
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    //Declarations
    DWORD poll;
    int res, i = 1, port = 999;
    char buf[100];
    char msg[100] = "";
    char ip[15];
    WSADATA data;
    cout << "ttttThis is a Client";
    cout << "nntttEnter the Server IP to connect to: ";
    gets(ip);
    sockaddr_in ser;
    sockaddr addr;
    ser.sin_family = AF_INET;
    ser.sin_port = htons(123);                    //Set the port
    ser.sin_addr.s_addr = inet_addr(ip);      //Set the address we want to connect to
    memcpy(&addr, &ser, sizeof(SOCKADDR_IN));
    res = WSAStartup(MAKEWORD(1, 1), &data);      //Start Winsock
    cout << "nnWSAStartup"
        << "nVersion: " << data.wVersion
        << "nDescription: " << data.szDescription
        << "nStatus: " << data.szSystemStatus << endl;
    if (res != 0)
        client.s_cl("WSAStarup failed", WSAGetLastError());
    client.sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);       //Create the socket
    if (client.sock == INVALID_SOCKET)
        client.s_cl("Invalid Socket ", WSAGetLastError());
    else if (client.sock == SOCKET_ERROR)
        client.s_cl("Socket Error)", WSAGetLastError());
    else
        cout << "Socket Established" << endl;
    res = connect(client.sock, &addr, sizeof(addr));               //Connect to the server
    if (res != 0)
    {
        client.s_cl("SERVER UNAVAILABLE", res);
    }
    else
    {
        cout << "nConnected to Server: ";
        memcpy(&ser, &addr, sizeof(SOCKADDR));
    }
    char RecvdData[100] = "";
    int ret;
    vector<string> filenames;
    char rec[32] = "";
    client.sendData("FileSend");
    client.recvData(rec, 32);
    string path = "C:\Images:\";
    string imageName;
    string fullPath;
    int numberOfImages;
    while (true)
    {
        cout << "Enter the number of images you want to send : ";
        cin >> numberOfImages;
        for (int i = 0; i < numberOfImages; i++)
        {
            cout << "Select an image:";
            cin >> imageName;
            fullPath = path + imageName;
            filenames.push_back(fullPath);
        }
        client.fileSend(filenames);
        printf("File Sent.............n");
        ret = recv(client.sock, RecvdData, sizeof(RecvdData), 0);
        if (ret > 0)
        {
            cout << endl << RecvdData;
            strcpy(RecvdData, "");
        }
    }
    closesocket(client.clientt);
    WSACleanup();
}

我无法运行您的程序并复制您所面临的问题,因为您没有提供服务器代码。

,但是,我认为您在这条线路中的道路有问题:

string path = "C:\Images:\";

可能是:

string path = "C:\Images\";