c++. Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted.

Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted.

char cc[1024];   //此处如果索引值设置小了,就会出问题。比如:char cc[1];是由于建立的数组下标溢出造成的

 

 1 // vc2_2_4UDPserver_Txwtech.cpp : 定义控制台应用程序的入口点。
 2 //
 3 #include "StdAfx.h"
 4 #include <WinSock2.h>
 5 #include<stdio.h>
 6 #include<Windows.h>
 7 #pragma comment(lib,"WS2_32.lib")
 8 int main()
 9 {
10     WSADATA data;
11     WORD w=MAKEWORD(2,0);
12     char sztext[]="欢迎您\r\n";
13     ::WSAStartup(w,&data);
14     SOCKET s;
15     s=::socket(AF_INET, SOCK_DGRAM,0);
16     sockaddr_in addr,addr2;
17     int n=sizeof(addr2);
18     char buff[10]={0};
19     addr.sin_family=AF_INET;
20     addr.sin_port=htons(75);
21     addr.sin_addr.S_un.S_addr=INADDR_ANY;
22     ::bind(s,(sockaddr*)&addr,sizeof(addr));
23     printf("UDP服务器已经启动\r\n");
24     while(1)
25     {
26         char cc[1024];   //此处如果索引值设置小了,就会出问题。比如:char cc[1];是由于建立的数组下标溢出造成的
27             //c=getchar();
28     //    c='';
29         printf("请输入C:");
30         scanf("%s",cc);
31         //sztext=c;
32     //    strcpy(sztext,c);
33         if (strcmp(cc,"qq")==0)
34 
35             {
36                 printf("即将退出%s",cc);
37                 ::Sleep(1000);
38                 ::closesocket(s);
39                 ::WSACleanup();
40             //    strcpy(cc,"");
41                 return 0;
42             }
43         if(::recvfrom(s,buff,10,0,(sockaddr*)&addr2,&n)!=0)
44         {
45             printf("%s已经连接上\r\n",inet_ntoa(addr2.sin_addr));
46             printf("%s\r\n",buff);
47             
48             
49             ::sendto(s,sztext,sizeof(sztext),0,(sockaddr*)&addr2,n);
50         //    break;
51         }
52         
53     }
54     ::closesocket(s);
55     ::WSACleanup();
56     
57     if(getchar())
58     {
59         return 0;
60         //::sendto(s,sztext,sizeof(sztext),0,(sockaddr*)&addr2,n);
61     }
62     else
63     {
64         ::Sleep(100);
65     }
66 }

 

posted @ 2019-06-19 15:18  txwtech  阅读(1387)  评论(0编辑  收藏  举报