为啥请求回来的是乱码呢?codeblocks里面编译的
/*****************************************************************/
/*********** 程序名称:get_http.c ********************************/
/*********** 功能:向固定地址发起请求得到返回页面字符 ************/
/*********** 作者:YZW ******************************************/
/*********** 时间:2010-10-28 ************************************/
/*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int htconnect(char *host, int port){
int white_sock;
struct hostent * site;
struct sockaddr_in me;
site = gethostbyname(host);
if (site==NULL) return -2;
white_sock = socket(AF_INET,SOCK_STREAM,0);
if (white_sock <0) return -1;
memset(&me, 0, sizeof(struct sockaddr_in));
memcpy(&me.sin_addr, site-> h_addr_list[0], site-> h_length);
me.sin_family = AF_INET;
me.sin_port = htons(port);
return (connect(white_sock, (struct sockaddr *)&me,sizeof(struct sockaddr)) <0) ? -1 : white_sock;
}
int htsend(int sock, char *fmt, ...){
char BUF[1024];
va_list argptr;
va_start(argptr,fmt);
vsprintf(BUF,fmt,argptr);
va_end(argptr);
//printf(BUF); //回显
//printf("-->%s<--\n",BUF);
return send(sock,BUF,strlen(BUF),0);
}
int main(int argc,char **argv){
int black_sock;
int M,N;
char bugs_bunny[3];
// if (argc<2){
// printf( "Please input the hostName. Example:\n %s www.sohu.com\n ",argv[0]);
// return;
// }
char *host_name = "www.sohu.com";
black_sock = htconnect(host_name,80);
if (black_sock <1) return;
printf("循环发送请求开始\n");
printf("......\n");
M=1;N=1;
while (N==1){
if(M % 1000 == 0){printf("当前循环次数:%d \n",M); }
if(M == 10000){break;}
htsend(black_sock, "GET / HTTP/1.0\n");
htsend(black_sock, "Host: %s\n", host_name);
htsend(black_sock, "\n");
//htsend(black_sock, "%c ", 10);
M++;
}
printf("循环发送请求 %d 次\n",M);
printf("收到信息Start:---->\n");
while (read(black_sock, bugs_bunny, 1)> 0){
printf( "%c",bugs_bunny[0]);
}
printf( "收到信息End.<----\n ");
close(black_sock);
}
...全文
相关推荐
Codeblocks中文乱码解决方法.docx
如果源代码文件的编码与CodeBlocks设置的默认编码不匹配,或者编译器处理编码的方式不正确,就可能导致中文字符显示为乱码。解决CodeBlocks中文乱码问题主要分为两个步骤:1.
CodeBlocks汉字出现乱码问题解决方案.pdf
最后,编译完成后,在Windows环境下运行时出现的乱码,是因为系统默认使用GBK字符集,而编译出的程序默认使用UTF-8编码。
CodeBlocks配置文档
- 保存设置后重启CodeBlocks或者重新创建项目。- **原理**:通过调整文本文件的编码方式,使编译器能够正确解析中文字符,从而避免乱码现象的发生。
CodeBlocks环境配置
由于所提供的【部分内容】中存在大量乱码字符,无法从中直接提取有用信息。
CodeBlocks 16.01 汉化语言包
16.01是CodeBlocks的一个版本,它提供了丰富的特性来支持开发人员进行高效的代码编写、编译和调试。汉化语言包则是为了将CodeBlocks的默认英文界面翻译成中文,方便中国用户使用。1.


