簡體   English   中英

初始化錯誤無法打開套接字 - C++ 文件上傳

[英]Init error Unable to open socket - C++ file upload

我正在嘗試使用 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 個時它返回錯誤。

我變了

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);

現在錯誤似乎消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM