Release version doesn't output

I'm making a program to test how often an IP changes, as I can't get a definitive answer online. It starts by opening iplog.txt, connecting to whatismyip.org, receiving the data on the site every 3 hours, and logging it. While it works fine in debug mode, the release mode version doesn't output anything to the log. What do you suppose is wrong?

The code (windows):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <windows.h>
#include <winsock2.h>
#include <cstdio>
#include <ctime>

FILE* file=fopen("iplog.txt","a");
time_t rawtime;
struct tm* timeinfo;
char buffer[256];
char spf_buffer[256];

void log(const char* msg){
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	strcpy(spf_buffer,asctime(timeinfo));
	spf_buffer[strlen(spf_buffer)-1]=0;
	fprintf(file,"%s: %s\n",spf_buffer,msg);}

struct cleanup{
	~cleanup(){
		if(file!=NULL){
			log("Program exiting");
			fclose(file);}
		WSACleanup();}}trash;

bool getip(SOCKET& client){
	send(client,"GET / HTTP/1.0\r\n\r\n",18,0);
	int x=recv(client,buffer,256,0);
	if(!strstr(buffer,"200 OK") || x<=0){
		return false;}
	for(x=0;recv(client,buffer+x,1,0)>0;++x){}
	buffer[x]=0;
	return true;}

int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hprev,LPSTR cmdline,int cmdshow){
	if(file==NULL){
		return 1;}
	log("Program starting");
	WSADATA wsa;
	WSAStartup(MAKEWORD(1,1),&wsa);
	LPHOSTENT hostentry=gethostbyname("www.whatismyip.org");
	if(!hostentry){
		log("");
		fprintf(file,"ERROR %i: Failed to connect to host www.whatismyip.org\n",WSAGetLastError());
		return 2;}
	SOCKET client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(client==INVALID_SOCKET){
		log("");
		fprintf(file,"ERROR %i: Socket initialization failed\n",WSAGetLastError());
		return 3;}
	SOCKADDR_IN serverinfo;
	serverinfo.sin_family=AF_INET;
	serverinfo.sin_addr=*((LPIN_ADDR)*hostentry->h_addr_list);
	serverinfo.sin_port=htons(80);
	if(connect(client,(SOCKADDR*)&serverinfo,sizeof(struct sockaddr_in))==SOCKET_ERROR){
		log("");
		fprintf(file,"ERROR: %i: Failed to connect to host www.whatismyip.org",WSAGetLastError());
		return 4;}
	while(1){
		if(!getip(client)){
			break;}
		//for debug mode to make sure the IP is correct
		printf("IP: %s",buffer);
		log(buffer);
		Sleep(1000*60*60*3);}
	return 0;}
TBH - it didn't work for me in debug or release mode.

One problem is that after the first GET request, the website will close the connection - so subsequent send/receives on the socket will fail.
So how do you request the data?
research DNS and DHCP they are the main reasons IP address change.
If not in the above research NAT.

Tim S.
Admittedly, the OP is really going the long way around to find the IP address of the computer he is on

((although if if he connected via a router or gateway - i.e his own pc might be 192.xxx.yyy.zzz , the
www.whatismyip.org website would reply with the address of the router rather than his own pc)

But if he absolutely wanted the IP of his pc regardless of whether it was a direct connection or
through a router , I wouldnt do it that way.
you declare
time_t rawtime;

and only use it here

1
2
3
4
5
6
7
void log(const char* msg){
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	strcpy(spf_buffer,asctime(timeinfo));
	spf_buffer[strlen(spf_buffer)-1]=0;
	fprintf(file,"%s: %s\n",spf_buffer,msg);}


I had a problem with variables not being initialized once where it worked fine in debug and when I switched to release I had some strange errors. Probably worth looking into.
The release version usually isn't just the same as debug, minus the debugging symbols; it often has optimisations turned on. It is possible to compile a version identical to the release version (i.e. with the optimisations) but also with debugging symbols, which will make debugging the code much easier.
Topic archived. No new replies allowed.