我正在尝试使用c ++将文件上传到我的服务器,但我一直收到错误说:
Init error: Unable to open socket.
我已经尝试添加#include <stdio.h>
和#include <winsock2.h>
等新库,但似乎没有任何帮助。
C ++不是我的主要语言,所以我觉得有点迷失。
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <wininet.h>
#include <tchar.h>
#include <time.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wininet")
using namespace std;
int main(void)
{
HWND hwnd_win = GetForegroundWindow();
ShowWindow(hwnd_win, SW_HIDE);
string filea = "clerks.txt";
string fileb = "products.txt";
string filec = "products2.txt";
ShellExecute(
NULL,
_T("open"),
_T("C:\\connect\\w_icrcom.exe"),
_T("r clerks.txt 5"),
_T("C:\\connect"),
SW_HIDE);
ShellExecute(
NULL,
_T("open"),
_T("C:\\connect\\w_icrcom.exe"),
_T("r products.txt 80"),
_T("C:\\connect"),
SW_HIDE);
ShellExecute(
NULL,
_T("open"),
_T("C:\\connect\\w_icrcom.exe"),
_T("r products2.txt 81"),
_T("C:\\connect"),
SW_HIDE);
//cout << "File Found" << endl;
// Sending the file to the ftp server in order to process it with PHP
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL,0);
hFtpSession = InternetConnect(hInternet, "ftp.domain.com", INTERNET_DEFAULT_FTP_PORT, "login", "pass", INTERNET_SERVICE_FTP, 0, 0);
FtpPutFile(hFtpSession, filea.c_str(), "/public_html/rota/application/incoming/clerks.txt", FTP_TRANSFER_TYPE_BINARY, 0);
FtpPutFile(hFtpSession, fileb.c_str(), "/public_html/rota/application/incoming/products.txt", FTP_TRANSFER_TYPE_BINARY, 0);
FtpPutFile(hFtpSession, filec.c_str(), "/public_html/rota/application/incoming/products2.txt", FTP_TRANSFER_TYPE_BINARY, 0);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
修改 我认为错误来自ShellExecute,因为当我只留下一个Shell它工作正常,但当我离开其中3个时它返回错误。
答案 0 :(得分:-1)
我改变了
ShellExecute(
NULL,
_T("open"),
_T("C:\\connect\\w_icrcom.exe"),
_T("r clerks.txt 5"),
_T("C:\\connect"),
SW_HIDE);
到
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "C:\\connect\\w_icrcom.exe";
ShExecInfo.lpParameters = "r products2.txt 81";
ShExecInfo.lpDirectory ="C:\\connect";
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
现在错误似乎消失了。