主题:C语言达人来帮忙吧!
[code]#include <stdio.h>
#include <Winsock2.H>
#pragma comment(lib,"ws2_32.lib")
#define LEN 1024 //接收数据的大小,字节
#define URL "http://localhost/wave.php"
#define _IP "127.0.0.1"
#define LOGFILE "G:\\DOWN\\zlog.txt"
int main(){
socketAccess();
sleep(999999999);
}
int socketAccess(){
WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws);//初始化Socket动态连接库
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//建立socket
if (sock == INVALID_SOCKET){
printf("ERROR:create socket error\n");
//WriteToLog("ERROR:create socket error");
return 0;
}
SOCKADDR_IN hostadd;
//hostent* host = gethostbyname(URL);//取得主机的IP地址
//memcpy(&hostadd,host->h_addr,sizeof(hostadd));//将返回的IP信息Copy到地址结构
hostadd.sin_addr.S_un.S_addr=inet_addr( _IP );
hostadd.sin_family = AF_INET;
hostadd.sin_port = htons(80);
//构造Http请求数据包
char buf[LEN]="GET ";
strcat(buf,URL);
strcat(buf," HTTP/1.1\r\nHost: ");
strcat(buf,inet_ntoa(hostadd.sin_addr));
//strcat(buf,"\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)\r\n");
strcat(buf,"\r\nContent-Length: 10\r\n");
strcat(buf,"\r\nConnection:close\r\n\r\n");
int time = 1000; //超时时间
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&time,sizeof(time));
if (connect(sock,(SOCKADDR*)&hostadd,sizeof(hostadd)) == SOCKET_ERROR){//连接请求
WriteToLog("Socket ERROR:connect failed,maybe apache has not boot");
//WriteToLog(GetLastError());
return 0;
}
if (SOCKET_ERROR == send(sock,buf,strlen(buf)+1,0)){//发送构造好的Http请求包
WriteToLog("SOCKET ERROR:send fail!");
return 0;
}
//接收返回的数据
memset(buf,0,LEN);
recv(sock,buf,LEN,0);
printf("\n%s\n",buf);
WriteToLog(buf);
//接收返回的数据end
closesocket(sock);
WSACleanup();
return 0;
}
int WriteToLog(char* str){
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}[/code]上面的代码是利用socket来访问本机上的wave.php这个页面。本机wamp环境已配置好。
问题:
第58行,printf("\n%s\n",buf);这一行,输出的内容是http://localhost/wave.php页面和http://localhost/页面两个页面的内容。即wave.php加上index.php,而我只是要访问wave.php。程序把网站根目录的默认页面也访问了。错在哪里?
第59行,WriteToLog(buf);这一行,没有写入日志文件,也就是写入出错。为什么。
#include <Winsock2.H>
#pragma comment(lib,"ws2_32.lib")
#define LEN 1024 //接收数据的大小,字节
#define URL "http://localhost/wave.php"
#define _IP "127.0.0.1"
#define LOGFILE "G:\\DOWN\\zlog.txt"
int main(){
socketAccess();
sleep(999999999);
}
int socketAccess(){
WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws);//初始化Socket动态连接库
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//建立socket
if (sock == INVALID_SOCKET){
printf("ERROR:create socket error\n");
//WriteToLog("ERROR:create socket error");
return 0;
}
SOCKADDR_IN hostadd;
//hostent* host = gethostbyname(URL);//取得主机的IP地址
//memcpy(&hostadd,host->h_addr,sizeof(hostadd));//将返回的IP信息Copy到地址结构
hostadd.sin_addr.S_un.S_addr=inet_addr( _IP );
hostadd.sin_family = AF_INET;
hostadd.sin_port = htons(80);
//构造Http请求数据包
char buf[LEN]="GET ";
strcat(buf,URL);
strcat(buf," HTTP/1.1\r\nHost: ");
strcat(buf,inet_ntoa(hostadd.sin_addr));
//strcat(buf,"\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)\r\n");
strcat(buf,"\r\nContent-Length: 10\r\n");
strcat(buf,"\r\nConnection:close\r\n\r\n");
int time = 1000; //超时时间
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&time,sizeof(time));
if (connect(sock,(SOCKADDR*)&hostadd,sizeof(hostadd)) == SOCKET_ERROR){//连接请求
WriteToLog("Socket ERROR:connect failed,maybe apache has not boot");
//WriteToLog(GetLastError());
return 0;
}
if (SOCKET_ERROR == send(sock,buf,strlen(buf)+1,0)){//发送构造好的Http请求包
WriteToLog("SOCKET ERROR:send fail!");
return 0;
}
//接收返回的数据
memset(buf,0,LEN);
recv(sock,buf,LEN,0);
printf("\n%s\n",buf);
WriteToLog(buf);
//接收返回的数据end
closesocket(sock);
WSACleanup();
return 0;
}
int WriteToLog(char* str){
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}[/code]上面的代码是利用socket来访问本机上的wave.php这个页面。本机wamp环境已配置好。
问题:
第58行,printf("\n%s\n",buf);这一行,输出的内容是http://localhost/wave.php页面和http://localhost/页面两个页面的内容。即wave.php加上index.php,而我只是要访问wave.php。程序把网站根目录的默认页面也访问了。错在哪里?
第59行,WriteToLog(buf);这一行,没有写入日志文件,也就是写入出错。为什么。