//得到网址的IP并建立与Server的连接
char* pAddr="www.baidu.com";
HOSTENT* test=gethostbyname(pAddr);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr= *(DWORD*)test->h_addr_list[0];
//printf("%s\n\n",inet_ntoa(addrSrv.sin_addr));
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(80);
connect(socketClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//准备消息头
     //GET / HTTP/1.1
 //Accept: */*
 //Accept-Language: zh-cn 
 //Accept-Encoding: gzip, deflate
 //Connection: Keep-Alive
char* sendBuf="GET / HTTP/1.1\n\
Accept: */*\n\
Accept-Language: zh-cn\n\
Accept-Encoding: gzip, deflate\n\
Connection: Keep-Alive\n\n";
send(socketClient,sendBuf,strlen(sendBuf),0); const int len=2000;
char recvBuf[len];
recvBuf[len-1]='\0';
recv(socketClient,recvBuf,len-1,0);//暂且接收len-1个字节
printf("%s\n",recvBuf);//打印的是乱码,并非页面内容

解决方案 »

  1.   

    send recv返回值都不判断么?你确认调一次send一定能把内容都发送出去?
      

  2.   

    char recvBuf[len] = {};
    查看下recv的返回值,这里判断接收了多少数据的
      

  3.   


    send返回值等于strlen(sendBuf),表明确实都发送出去了。乱码也表明未收到任何数据,recv的返回值确实为0,可是这是为什么呢?
      

  4.   

    send返回值等于strlen(sendBuf),表明确实都发送出去了。乱码也表明未收到任何数据,recv的返回值确实为0,可是这是为什么呢?
      

  5.   

    send返回值等于strlen(sendBuf),表明确实都发送出去了。乱码也表明未收到任何数据,recv的返回值确实为0,可是这是为什么呢?
    用WSAGetLastError看看是什么错误代码吗
      

  6.   

    send返回值等于strlen(sendBuf),表明确实都发送出去了。乱码也表明未收到任何数据,recv的返回值确实为0,可是这是为什么呢?摘自msdn:
    recv function
    ...
    Return valueIf no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.

    Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
      

  7.   

    是什么编码 utf-8 还是 ???