0

我的以下代码给出了错误“运行时检查失败 #2 - 围绕变量‘线程号’的堆栈已损坏。” 如果我在 if 语句中添加“中断”。我还在代码中强调了这个“中断”。如果我删除这个“break”,错误就会被删除。

谁能解释一下为什么会这样?:(

PS:对不起,代码太长了。

代码:

DWORD WINAPI ThreadProc(LPVOID param)
    {
       int threadNumber= (int)param;
       int PORT = 8888+threadNumber; //so that each thread bind()s its socket to a different Port number.
       WSADATA wsa; 

       //Initialise winsock//
       if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
          {

            //"WinSock Initialization FAILED",

          }

       //Create a socket//

      SOCKET newSocketIdentifier;
      SOCKADDR_IN newSocket;

      if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
          {                 

            //Socket Creation Failed

          }
       //Socket Created//

       //Prepare the sockaddr_in structure//
      newSocket.sin_family = AF_INET;
      newSocket.sin_addr.s_addr = INADDR_ANY;
      newSocket.sin_port = htons(PORT);

       //Bind//
       if( bind(newSocketIdentifier ,(struct sockaddr *)&newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
          { 
           //Bind Failed
          }

       //Bind Done//   char threadNumberBuffer[32] = "Thread Number : ";
       char buff[12];
       itoa(threadNumber,buff,10);
       strcat(threadNumberBuffer,buff);
       MessageBox( NULL,
                   threadNumberBuffer,
                   "Thread Created :)",
                   MB_ICONINFORMATION);


    char data[256];
    int bytes, waitRet;

    WSAEVENT hEvent = WSACreateEvent();
    WSANETWORKEVENTS events;
    WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE);

    SOCKADDR_IN clientSocket;
    int clientSocketLength = sizeof(SOCKADDR_IN);
    char receiveBuffer[8192]={0};
    int recv_len=0;
    char file[12]="Client";
    char threadno[2];
    itoa(threadNumber,threadno,10);
    strcat(threadno,".txt");
    strcat(file,threadno);
    //FILE *fprecv;
    FILE *fprecv = fopen(TEXT(file),"w+b");
    //rewind(fprecv);
    int receiveCount=0;

       while(1)
          {   //while(1) starts
             waitRet = WSAWaitForMultipleEvents(1, &hEvent, FALSE, WSA_INFINITE, FALSE);
             //WSAResetEvent(hEvent);
             if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&events) == SOCKET_ERROR)
                {
                   MessageBox( NULL,
                               threadNumberBuffer,
                               "FAILURE",
                               MB_ICONINFORMATION);
                }
            else
                {   //else event occurred starts
                   if(events.lNetworkEvents & FD_READ)
                      {   //check for network event starts
                         /*MessageBox( NULL,
                                     buf,
                                     "FD_READ",
                                     MB_ICONINFORMATION);*/

                         if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
                            {
                               MessageBox( NULL,
                                           "ERROR",
                                           "Could not Receive Data",
                                           MB_ICONINFORMATION);
                               exit(EXIT_FAILURE);
                               return FALSE;
                            }
                         receiveCount = receiveCount+1;
                         char display[2000] = "Number of Receives = ";

                        if(memcmp(receiveBuffer,"EXIT",4) == 0)
                            {
                                char receiveCountBuffer[128];
                                itoa(receiveCount,receiveCountBuffer,10);
                                strcat(display,receiveCountBuffer);
                                MessageBox( NULL,
                                            display,
                                            threadNumberBuffer,
                                            MB_ICONINFORMATION);
                                **break;** //Main Problem is here. if I remove this break statement, the error is removed. Else I get Run-Time Check Failure # 2.
                            }
                    /*MessageBox( NULL,
                                receiveBuffer,
                                "File Read",
                                MB_ICONINFORMATION);*/
                        else
                        {
                              fprecv= freopen(TEXT(file),"w+b",stdout);
                               if(fwrite(receiveBuffer, 1, recv_len, fprecv)<0)
                                  {
                                     MessageBox( NULL,
                                                 "problem while writing file",
                                                 "Error!",
                                                 MB_ICONINFORMATION);

                                     exit(1);
                                  }
                               fclose(fprecv);
                        }
                              /* MessageBox( NULL,
                                           "Written",
                                           "Success",
                                           MB_ICONINFORMATION);*/
                               //rewind(fprecv);
                   }   //check for network event ends
                }   //else event occurred ends
          }   //while(1) ends

        WSACloseEvent(hEvent);
        return 0;
    }
4

1 回答 1

2

threadno太小!

char threadno[2];
itoa(threadNumber,threadno,10);
strcat(threadno,".txt");            // Buffer overrun! threadno can only fit 2 chars!
strcat(file,threadno);
于 2013-05-08T11:47:51.120 回答