首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

报错求解决解决方案

2012-04-27 
报错求解决C/C++ code#include stdafx.h#include stdio.h#include winsock2.h#pragmacomment(lib,w

报错求解决

C/C++ code
#include "stdafx.h"   #include "stdio.h"    #include "winsock2.h"      #pragma   comment(lib,"ws2_32.lib")int main(int argc, char* argv[])  {      SOCKADDR_IN saServer;      LPHOSTENT   lphostent;      WSADATA     wsadata;      SOCKET      hsocket;            int     nRet;      char*   host_name="www.******.com";      char*   req=        "POST /blog/comment.asp?act=save&logid=3560 HTTP/1.0/r/n"          "Accept:   image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*/r/n"          "Referer:   http://www.******.com/blog/article.asp?id=3560/r/n"          "Accept-Language:   zh-cn/r/n"          "Content-Type:   application/x-www-form-urlencoded/r/n"          "Accept-Encoding:   gzip, deflate/r/n"          "User-Agent:   Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; POTU(RR:28031409:0:5497353); SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; CIBA)/r/n"             "Host:   www.******.com/r/n"          "Content-Length:   114/r/n"          "Connection:   Keep-Alive/r/n"          "Cookie:   babyhashkey=; babyuserid=; ASPSESSIONIDACBRSQBC=AFHPMPGBBCGPDDDNEDKGJHEJ/r/n/r/n"          "e_ubb=true&e_autourl=true&e_smilies=true&comm_username=%E5%A4%A9%E4%BD%BF&comm_password=&message=www.******.net.cn";          // 初始化套接字       if(WSAStartup(MAKEWORD(2,2),&wsadata))             printf("初始化SOCKET出错!");        lphostent=gethostbyname(host_name);         if(lphostent==NULL)             printf("lphostent为空!");      hsocket   =   socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);         saServer.sin_family = AF_INET;         saServer.sin_port = htons(80);         saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);        // 利用SOCKET连接       nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));         if(nRet == SOCKET_ERROR)         {             printf("建立连接时出错!");             closesocket(hsocket);             return 0;         }      // 利用SOCKET发送       nRet = send(hsocket,req,strlen(req),0);      if(nRet==SOCKET_ERROR)      {          printf("发送数据包时出错!");          closesocket(hsocket);      }    char Dest[3000];         nRet=1;    while(nRet>0)         {          // 接收返回数据包           nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);             if(nRet>0)                 Dest[nRet]=0;             else                 Dest[0]=0;            // 显示返回数据包的大小、内容           printf("/nReceived   bytes:%d/n",nRet);             printf("Result:/n%s",Dest);         }      return 0;  }  


[解决办法]
输入的时候把这句话移到main函数最前面你定义其他的时候一起定义就可以了,这样:
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;


char Dest[3000]; /*定义在这里*/
int nRet;
char* host_name="www.******.com";
至于为什么,我没想通。。。。

热点排行