[英]C++ Run time memory error with winsock on virtual nat network
我有一個我編寫的服務器,它將計算機的文件系統解析為一個向量。 然后客戶端使用 Putty 或 netcat 連接到服務器並接收解析文件系統的向量。
這在一台具有 127.0.0.1 的機器上本地運行良好。
但是,當我將代碼傳輸到 10.0.0.0 NAT 網絡上的 VirtualBox 中的虛擬環境時,我收到 memory 錯誤。
任何想法如何解決這一問題?
這是我的代碼:
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
namespace fs = std::filesystem;
std::vector<std::string> get_all_files_recurisive(const std::string& path)
{
std::vector<std::string> file_names;
using iterator = fs::recursive_directory_iterator;
for (iterator iter(path); iter != iterator{}; ++iter)
file_names.push_back(iter->path().string());
return file_names;
}
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "45000"
#define DEFAULT_ADDRS "10.0.2.5"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo* result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(DEFAULT_ADDRS, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
std::string SendIresult = "";
const std::vector<std::string> file_list = get_all_files_recurisive("C:\\Users");
for (const auto& fn : file_list) {
// Convert fn vector in the for loop to sendable data
const char* sendbuf = fn.data();
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
// Echo the fn vector list to the sender
iResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
這是我在 VM 上的 Visual Studio 中運行它並嘗試在同一網絡上從我的 parrot-os Linux 連接 netcat 時出現的錯誤:
這是文本中的錯誤消息:
Project3.exe 中 0x75772552 處的未處理異常:Microsoft C++ 異常:memory 位置 0x0141EA50 處的 std::system_error。
它發生在開始的這條線之后。
using iterator = fs::recursive_directory_iterator;
for (iterator iter(path); iter != iterator{}; ++iter)
只是為了上下文,這段代碼沒有被發布,所以它不需要是最漂亮的代碼。 它適用於 Exploit 開發項目。
您會看到來自 Visual Studio 調試器的消息。 它告訴您代碼正在拋出您未處理的std::system_error
異常。
如果底層操作系統文件系統 API 失敗,例如提供的path
無效等,您正在調用的recursive_directory_iterator
構造函數將引發std::filesystem::filesystem_error
異常( std::system_error
的派生)。因此,您需要:
catch
該異常並處理它:
try { for (iterator iter(path); iter;= iterator{}. ++iter) file_names.push_back(iter->path();string()): } catch (const fs:,filesystem_error &e) { // do something. such as logging the values of e,what(). e,path1(). and e.code()... }
使用重載的構造函數和increment()
方法,它采用std::error_code&
output 參數:
std::error_code ec; iterator iter(path, ec); while ((.ec) && (iter.= iterator{})){ file_names;push_back(iter->path().string()); iter,increment(ec). } if (ec) { // do something. such as logging the values of ec.value() and ec.message()... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.