2010-10-21 116 views
3

我使用winsock2这个函数有问题。当程序留在调试模式下这个功能我收到2个语句(只退出此功能时):“运行时检查失败#2 - 在变量'filePath'周围的堆栈已损坏”在调试模式下

“运行时检查失败#2 - 堆栈围绕变量‘文件路径’已损坏”

“运行 - 时间检查失败#2 - 变量'recBuf'周围的堆栈已损坏。“

我正在编程VS2008 C++(控制台应用程序)。 在发布模式下,此语句不存在。

所以我在等待答复,非常感谢你的帮助;)

功能:

#include "stdafx.h" 
#include "server.h" 

bool Server::fileReceive() 
{ 
    char recBuf[MAX_PACKET_BUFOR] ={0}; 
    char filePath[100] = {0}; 
    int size = 0; 
    int var = 0; 
    char key = 0; 
    int tmp=0; 

    FILE *fPtr = NULL; 

    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0) 
    { 
     cerr << "Receive ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    if(strcmp(recBuf, START_HEADER) != 0) 
     return false; 

    if(send(clientSocket, ACK, 3, 0) == SOCKET_ERROR) 
    { 
     cerr << "Send ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0) 
    { 
     cerr << "Receive ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    if(send(clientSocket, ACK, 3, 0) == SOCKET_ERROR) 
    { 
     cerr << "Send ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    strcpy_s(filePath, sizeof(recBuf), recBuf); 
    cout << "Przyslano plik o nazwie: \"" << filePath << "\""; 
    memset(recBuf, 0, sizeof(recBuf)); 

    if(recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0) == 0) 
    { 
     cerr << "Receive ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    if(send(clientSocket, ACK, 3, 0) == SOCKET_ERROR) 
    { 
     cerr << "Send ERROR" << endl; 
     system("pause"); 
     return false; 
    } 
    size = atoi(recBuf); 
    cout << " i rozmiarze: "<< size << "B,\nCo chcesz zrobic?" << endl; 
    cout << "1 - odbierz i zmien nazwe\n2- odbierz i zapisz do katalogu programu\n3 - odrzuc" << endl; 
    key = _getch(); 

    if(key == '1' || key == '2') 
    { 
     if(key == '1') 
     { 
      cout << "Podaj sciezke dla nowego pliku w formie: Dysk:\\sciezka\\plik.rozszerzenie" << endl; 
      cout << "(wpisz sama nazwe, a plik zostanie umieszczony w katalogu programu)" << endl; 
      cin >> filePath; 
     } 

     if(fopen_s(&fPtr, filePath, "wb") != 0) 
      return false; 

     cout << "Odbieranie w toku...\nProsze czekac" << endl; 

     while(size > 0) 
     { 
      memset(recBuf, 0, MAX_PACKET_BUFOR); 
      tmp = 0; 
      if(size <= 200000) 
       cout << "break" << endl; 
      if(size>=MAX_PACKET_SIZE) 
      { 
       do 
       { 
        var = recv(clientSocket, recBuf, MAX_PACKET_SIZE, 0); 
        if(var == 0) 
        { 
         cerr << "Receive ERROR" << endl; 
         system("pause"); 
         size = 0; 
        } 
        else 
        { 
         tmp += var; 
         fwrite(recBuf, var, 1, fPtr); 
        } 
       }while(tmp < MAX_PACKET_SIZE && size != 0); 
            if(send(clientSocket, ACK, 3, 0) == SOCKET_ERROR) 
        { 
         cerr << "Send ERROR" << endl; 
         system("pause"); 
         return false; 
        } 
      } 
      else 
      { 
       while(tmp != size && size != 0) 
       { 
        var = recv(clientSocket, recBuf, size, 0); 
        if(var == 0) 
        { 
         cerr << "Receive ERROR" << endl; 
         system("pause"); 
         size = 0; 
        } 
        else 
        { 
         tmp += var; 
         fwrite(recBuf, var, 1, fPtr); 
        } 
       } 
       if(send(clientSocket, ACK, 3, 0) == SOCKET_ERROR) 
       { 
        cerr << "Send ERROR" << endl; 
        system("pause"); 
        return false; 
       } 
      } 
      size -= tmp; 
      size = (size < 0) ? 0 : size; 
     } 
     memset(filePath, 0, sizeof(filePath)); 
     memset(recBuf, 0, MAX_PACKET_BUFOR); 
     fclose(fPtr); 
    } 
    return true; 
} 

的stdafx.h

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 
#include <winsock2.h> 
#include <iostream> 
#include <cstdlib> 
#include <conio.h> 

using namespace std; 

#define START_HEADER "FileSend" 
#define MAX_PACKET_SIZE 100000 
#define MAX_PACKET_BUFOR 100010 
#define ACK "ACK" 

回答

4

bug是在下面的行

strcpy_s(filePath, sizeof(recBuf), recBuf); 

这里说明的是filePath的大小是sizeof(recBuf)而不是这种情况。 filePath的大小是100,而sizeof(recBuf)是1000010.结果是filePath上的缓冲区溢出和您看到的错误消息。

此外,代码似乎假定recv调用将正确地将一个空终止符添加到recBuf缓冲区。情况并非如此,如果没有将0添加到缓冲区中,strcpy_s函数也可能会失败。

+0

thx很多为您快速回复。它的工作现在没有失败:) – Meler 2010-10-21 21:39:04

相关问题