Code does not work with generic type Vector<T> in C++ -
i trying send images server through code, code breaks down in method void filesend(const vector& fnames). have overloaded function void filesend(const char* fpath). when did not overload function, able send 1 image, when overloaded void filesend(const vector& fnames), stops in main() @ line " client.filesend(filenames); ". tried finding out problem, not able find it.
#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] = '\0'; return sz; } void filesend(const char *fpath) { // extract filename given path. char filename[50]; int = strlen(fpath); (; > 0; i--) { if (fpath[i - 1] == '\\') break; } (int j = 0; <= (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] = '\0'; send(sock, buffer, size, 0); recv(sock, rec, 32, 0); } size -= 1024; } fclose(fr); } void filesend(const vector<string>& fnames) { (int k = 0; k < fnames.size(); k++) { filesend(fnames[k].c_str()); // call 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, = 1, port = 999; char buf[100]; char msg[100] = ""; char ip[15]; wsadata data; cout << "\t\t\t\tthis client"; cout << "\n\n\t\t\tenter server ip connect to: "; gets(ip); sockaddr_in ser; sockaddr addr; ser.sin_family = af_inet; ser.sin_port = htons(123); //set port ser.sin_addr.s_addr = inet_addr(ip); //set address want connect memcpy(&addr, &ser, sizeof(sockaddr_in)); res = wsastartup(makeword(1, 1), &data); //start winsock cout << "\n\nwsastartup" << "\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 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 server if (res != 0) { client.s_cl("server unavailable", res); } else { cout << "\nconnected 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 number of images want send : "; cin >> numberofimages; (int = 0; < numberofimages; i++) { cout << "select 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(); }
i unable run program , reproduce problem you're facing because didn't provide server code.
but, think you've problems path in line:
string path = "c:\\images:\\";
probably like:
string path = "c:\\images\\";
Comments
Post a Comment