- 分享
- 0
- 人气
- 0
- 主题
- 8
- 帖子
- 22
- UID
- 107501
- 积分
- 11
- 阅读权限
- 11
- 注册时间
- 2007-10-30
- 最后登录
- 2011-3-21
- 在线时间
- 25 小时
|
- #include <io.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <windows.h>
- #include <time.h>
- #define FILENAME1 "C:\\start1.txt"
- #define FILENAME2 "C:\\start2.txt"
- #define URL1 "localhost/jacker1.html"
- #define URL2 "localhost/jacker2.html"
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- /*not optimized code version 2010/11/9*/
- void geturl (char *url);void geturl2 (char *url2)
- {
- while (1)
- {
- if( (_access( FILENAME1, 0 )) != -1 )
- {
- WSADATA WSAData={0};
- SOCKET sockfd;
- struct sockaddr_in addr;
- struct hostent *pURL;
- char myurl[BUFSIZ];
- char *pHost = 0, *pGET = 0;
- char host[BUFSIZ], GET[BUFSIZ];
- char header[BUFSIZ] = "";
- static char text[BUFSIZ];
- int i;
- if(WSAStartup(MAKEWORD(2,2), &WSAData))
- {
- printf("WSA failed\n");
- return;
- }
- strcpy(myurl, url);
- for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost);
- if ( (int)(pHost - myurl) == strlen(myurl) )
- strcpy(GET, "/");
- else
- strcpy(GET, pHost);
- *pHost = '\0';
- strcpy(host, myurl);
- sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- pURL = gethostbyname(host);
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
- addr.sin_port = htons(80);
- strcat(header, "GET ");
- strcat(header, GET);
- strcat(header, " HTTP/1.1\r\n");
- strcat(header, "HOST: ");
- strcat(header, host);
- strcat(header, "\r\nConnection: Close\r\n\r\n");
- connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));
- send(sockfd, header, strlen(header), 0);
- char htmlBuf[10000] = "";
- while ( recv(sockfd, text, BUFSIZ, 0) > 0)
- {
- strcat(htmlBuf, text);
- strnset(text, '\0', BUFSIZ);
- }
- closesocket(sockfd);
- WSACleanup();
- char *pf = htmlBuf;
- while (*pf != '\0')
- {
- if (*pf == '\r' && *(pf+1) == '\n' && *(pf+2) == '\r' && *(pf+3) == '\n')
- break;
- pf++;
- }
- printf(pf+4);
- }
- else if( (_access( FILENAME1, 0 )) != -1 )
- {
- WSADATA WSAData={0};
- SOCKET sockfd;
- struct sockaddr_in addr;
- struct hostent *pURL;
- char myurl[BUFSIZ];
- char *pHost = 0, *pGET = 0;
- char host[BUFSIZ], GET[BUFSIZ];
- char header[BUFSIZ] = "";
- static char text[BUFSIZ];
- int i;
- if(WSAStartup(MAKEWORD(2,2), &WSAData))
- {
- printf("WSA failed\n");
- return;
- }
- strcpy(myurl, url2);
- for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost);
- if ( (int)(pHost - myurl) == strlen(myurl) )
- strcpy(GET, "/");
- else
- strcpy(GET, pHost);
- *pHost = '\0';
- strcpy(host, myurl);
- sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- pURL = gethostbyname(host);
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
- addr.sin_port = htons(80);
- strcat(header, "GET ");
- strcat(header, GET);
- strcat(header, " HTTP/1.1\r\n");
- strcat(header, "HOST: ");
- strcat(header, host);
- strcat(header, "\r\nConnection: Close\r\n\r\n");
- connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));
- send(sockfd, header, strlen(header), 0);
- char htmlBuf[10000] = "";
- while ( recv(sockfd, text, BUFSIZ, 0) > 0)
- {
- strcat(htmlBuf, text);
- strnset(text, '\0', BUFSIZ);
- }
- closesocket(sockfd);
- WSACleanup();
- char *pf = htmlBuf;
- while (*pf != '\0')
- {
- if (*pf == '\r' && *(pf+1) == '\n' && *(pf+2) == '\r' && *(pf+3) == '\n')
- break;
- pf++;
- }
- printf(pf+4);
- }
- }
- }
- int main()
- {
- geturl(URL1);
- geturl2(URL2);
- return 0;
- }
复制代码 请问void geturl (char *url);void geturl2 (char *url2) 《这样写有问题吗? 我这样写后 代码就出错了 url 找不到东西 ,请问是出了什么问题? |
|