Hej jeg har fået lavet et lille checker program som checker om en hjemmeside er opdateret siden sidst man kørte programmet men jeg har nogle fejl programmet og koden kan hentes på: www.mytestpage.1go.dk/checker.rar
kildekoden:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <fstream>
#define TIMEOUT 15 /* Sec */
using namespace std;
HINSTANCE InstanceHandle;
HWND MainWindow;
enum numbers
{
Edit1 = 1000,
Edit2,
Edit3,
Edit4,
Edit5,
Edit6,
CheckCmd,
ExitCmd,
AddCmd,
UpdateCmd
};
char adresse1[5024];
char adresse2[5024];
char adresse3[5024];
char adresse4[5024];
char adresse5[5024];
char adresse6[5024];
char cmd[1024]; /* Kommando buffer */
char resp[9999999]; /* Vores side havner i denne buffer */
int htmlstorrelse;
int tempvar1, tempvar2, tempvar3, tempvar4, tempvar5, tempvar6;
int tom;
// Hente funktion
void get(char *hostname, short port, char *path)
{
int sd, status, len, ix, tmp;
struct sockaddr local,remote;
struct hostent *hostinfo;
fd_set ReadSet;
struct timeval Time;
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd == INVALID_SOCKET)
{
MessageBox(NULL, "Fejl ved oprettning af socket", "Error", MB_OK);
return;
}
/* bind socket */
local.sa_family = AF_INET;
memset(local.sa_data, 0, sizeof(local.sa_data));
status = bind(sd, &local, sizeof(local));
if(status == INVALID_SOCKET)
{
MessageBox(NULL, "Fejl ved bindning af socket", "Error", MB_OK);
closesocket(sd);
return;
}
/* lookup host */
hostinfo = gethostbyname(hostname);
if(!hostinfo)
{
MessageBox(NULL, "Fejl ved hostname", "Error", MB_OK);
closesocket(sd);
return;
}
/* connect to host */
remote.sa_family = hostinfo->h_addrtype;
memcpy(remote.sa_data + 2, hostinfo->h_addr_list[0], hostinfo->h_length);
*((short *)remote.sa_data) = port;
tmp = remote.sa_data[0];
remote.sa_data[0] = remote.sa_data[1];
remote.sa_data[1] = (char )tmp;
status = connect(sd, &remote, sizeof(remote));
if(status != 0)
{
MessageBox(NULL, "Error connection on port", "Error", MB_OK);
closesocket(sd);
return;
}
/* send GET request */
sprintf(cmd, "GET %s HTTP/1.1\\r\\nHost: %s\\r\\n\\r\\n", path, hostname);
status = send(sd, cmd, strlen(cmd), 0);
if(status < 0)
{
MessageBox(NULL, "Error sending POST request", "Error", MB_OK);
closesocket(sd);
return;
}
/* read response */
ix = 0;
memset(&ReadSet, 0, sizeof(ReadSet));
FD_SET(sd, &ReadSet);
Time.tv_sec = TIMEOUT;
Time.tv_usec = 0;
len = 1;
while(len > 0 && select(sd + 1, &ReadSet, 0, 0, &Time) > 0)
{
len = recv(sd, resp + ix, sizeof(resp) - ix - 1, 0);
ix += len;
Time.tv_sec = TIMEOUT;
Time.tv_usec = 0;
}
resp[ix] = 0;
htmlstorrelse = strlen(resp);
closesocket(sd);
return;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 20, 600, 20,
hwnd,
(HMENU) (Edit1),
InstanceHandle,
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 50, 600, 20,
hwnd,
(HMENU) (Edit2),
InstanceHandle,
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 80, 600, 20,
hwnd,
(HMENU) (Edit3),
InstanceHandle,
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 110, 600, 20,
hwnd,
(HMENU) (Edit4),
InstanceHandle,
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 140, 600, 20,
hwnd,
(HMENU) (Edit5),
InstanceHandle,
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP,
50, 170, 600, 20,
hwnd,
(HMENU) (Edit6),
InstanceHandle,
0);
CreateWindow("BUTTON",
"Check",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
50, 400, 80, 30,
hwnd,
(HMENU) (CheckCmd),
InstanceHandle,
0);
CreateWindow("BUTTON",
"Exit",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
350, 400, 80, 30,
hwnd,
(HMENU) (ExitCmd),
InstanceHandle,
0);
CreateWindow("BUTTON",
"Update",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
150, 400, 80, 30,
hwnd,
(HMENU) (UpdateCmd),
InstanceHandle,
0);
CreateWindow("BUTTON",
"Add",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
250, 400, 80, 30,
hwnd,
(HMENU) (AddCmd),
InstanceHandle,
0);
break;
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == CheckCmd)
{
GetDlgItemText(hwnd, Edit1, adresse1, sizeof(adresse1));
GetDlgItemText(hwnd, Edit2, adresse2, sizeof(adresse2));
GetDlgItemText(hwnd, Edit3, adresse3, sizeof(adresse3));
GetDlgItemText(hwnd, Edit4, adresse4, sizeof(adresse4));
GetDlgItemText(hwnd, Edit5, adresse5, sizeof(adresse5));
GetDlgItemText(hwnd, Edit6, adresse6, sizeof(adresse6));
WORD version = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup(version, &wsaData);
ifstream Input("temp.txt");
if(Input) {
Input >> tempvar1;
Input >> tempvar2;
Input >> tempvar3;
Input >> tempvar4;
Input >> tempvar5;
Input >> tempvar6;
}
Input.close();
ofstream File("temp.txt");
get(adresse1, 80, "/");
if(tempvar1 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar1) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 1 er opdateret!", "Update", MB_OK);
}
}
get(adresse2, 80, "/");
if(tempvar2 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar2) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 2 er opdateret!", "Update", MB_OK);
}
}
get(adresse3, 80, "/");
if(tempvar3 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar3) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 3 er opdateret!", "Update", MB_OK);
}
}
get(adresse4, 80, "/");
if(tempvar4 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar4) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 4 er opdateret!", "Update", MB_OK);
}
}
get(adresse5, 80, "/");
if(tempvar5 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar5) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 5 er opdateret!", "Update", MB_OK);
}
}
get(adresse6, 80, "/");
if(tempvar6 = tom) {
File << htmlstorrelse << endl;
}
else {
if(htmlstorrelse > tempvar6) {
File << htmlstorrelse << endl;
MessageBox(NULL, "Link 6 er opdateret!", "Update", MB_OK);
}
}
File.close();
WSACleanup();
}
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ExitCmd)
{
DestroyWindow(hwnd);
}
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == UpdateCmd)
{
ifstream input("URLs.txt");
input >> adresse1;
input >> adresse2;
input >> adresse3;
input >> adresse4;
input >> adresse5;
input >> adresse6;
SetDlgItemText(hwnd, Edit1, adresse1);
SetDlgItemText(hwnd, Edit2, adresse2);
SetDlgItemText(hwnd, Edit3, adresse3);
SetDlgItemText(hwnd, Edit4, adresse4);
SetDlgItemText(hwnd, Edit5, adresse5);
SetDlgItemText(hwnd, Edit6, adresse6);
}
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == AddCmd)
{
GetDlgItemText(hwnd, Edit1, adresse1, sizeof(adresse1));
GetDlgItemText(hwnd, Edit2, adresse2, sizeof(adresse2));
GetDlgItemText(hwnd, Edit3, adresse3, sizeof(adresse3));
GetDlgItemText(hwnd, Edit4, adresse4, sizeof(adresse4));
GetDlgItemText(hwnd, Edit5, adresse5, sizeof(adresse5));
GetDlgItemText(hwnd, Edit6, adresse6, sizeof(adresse6));
ofstream File("URLs.txt");
if (File)
{
File << adresse1 << endl;
File << adresse2 << endl;
File << adresse3 << endl;
File << adresse4 << endl;
File << adresse5 << endl;
File << adresse6 << endl;
File.close();
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
HWND CreateMainWindow()
{
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC )MainWndProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "SimpleWinWndClass";
wc.lpszMenuName = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClass(&wc))
return 0;
return CreateWindow("SimpleWinWndClass",
"URL Checker",
WS_MINIMIZEBOX | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX |
WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
0,
0,
InstanceHandle,
0);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow)
{
InstanceHandle = hInstance;
if((MainWindow = CreateMainWindow()) == (HWND )0)
{
MessageBox(0, "Failed to create MainWindow!", "Warning", MB_OK);
return 0;
}
ShowWindow(MainWindow, SW_SHOW);
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Programmet virker ved at den kontakter en hjemmeside henter dens html finder ud af hvor mange tegn der er i html koden og gemmer antallet af tegn i et tekstdokument temp.txt næste gang man kører programmet henter den så den gamle htmls antal tegn ind og så henter den den nye hjemmeside og tjekker dens tegn! Men her kommer problemmet så for så skal tegnene tjekkes og den tjekker om de gamle tegn er mindre end de nye hvis de er det så skal den opdatere i tekstdokumentet og skrive med en messagebox at hjemmesiden er opdateret ellers skal den bare beholde den gamle værdi temp filen! men problemmet ligger i der hvor den tjekker om der er flere tegn i den nye end i den gamle for hvis jeg nu går ind og retter i den gamle og skriver et kæmpe tal og så tjekker så siger den alligevel at de nye tegn er større end det kæmpe tal jeg har skrevet selv om det ikke passer der må være en fejl et sted håber i forstod og håber nogle vil hjælpe