代码不适用于 C++ 中的泛型类型 Vector。<T>
Code does not work with generic type Vector<T> in C++
我正在尝试通过此代码将图像发送到服务器,但是该代码在方法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\";
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中