.exe crashing on executing output !!

Reply

Join Date: May 2005
Posts: 12
Reputation: numerouno543 is an unknown quantity at this point 
Solved Threads: 0
numerouno543 numerouno543 is offline Offline
Newbie Poster

.exe crashing on executing output !!

 
0
  #1
Jun 16th, 2005
Hello.....
I've written this simple C program to return the IP of a hostname ... this program compiles perfectly as well the Output is built...however, on executing the output, the Output crashes..... Can someone tell me what's the problem?
Regards.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <winsock.h>
  6.  
  7. #pragma comment(lib, "winmm.lib")
  8. #pragma comment(lib, "wsock32.lib")
  9. #pragma comment(lib, "advapi32.lib")
  10.  
  11. void main()
  12. {
  13.  
  14. struct hostent *he;
  15. char *addr;
  16. addr = "www.google.com";
  17. he = gethostbyname(addr);
  18. printf("%s", he -> h_name);
  19. getchar();
  20. }
<< moderator edit: added code tags: [code][/code] >>
Edit/Delete Message
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: .exe crashing on executing output !!

 
0
  #2
Jun 16th, 2005
Call WSAStartup first.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 12
Reputation: numerouno543 is an unknown quantity at this point 
Solved Threads: 0
numerouno543 numerouno543 is offline Offline
Newbie Poster

Re: .exe crashing on executing output !!

 
0
  #3
Jun 21st, 2005
hi..
well i called the WSAStartup now...and its not giving any error at that stage ..but still i am not getting the Output..... I checked the program for various possible errors...but its returning some unknown error(have a look at the code....it returns the default error msg)... what could possibly be the problem now?? Someone told me that I need to configure DNS server on my machine for this .....is it so??
thanks.
  1. #include <afx.h>
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <winsock2.h>
  8. #include <ctype.h>
  9. #include <cstring>
  10.  
  11.  
  12. #pragma comment(lib, "winmm.lib")
  13. #pragma comment(lib, "wsock32.lib")
  14. #pragma comment(lib, "advapi32.lib")
  15. #pragma comment(lib, "Ws2_32.lib")
  16. #pragma comment(linker, "/delayload:Ws2_32.dll")
  17.  
  18.  
  19. void main()
  20. {
  21. WORD wVersionRequested;
  22. WSADATA wsaData;
  23. int err;
  24.  
  25.  
  26. struct hostent *he = NULL;
  27. char addr [] = "http://www.google.com";
  28.  
  29.  
  30. wVersionRequested = MAKEWORD( 2, 2 );
  31.  
  32. err = WSAStartup( wVersionRequested, &wsaData );
  33. if ( err != 0 ) {
  34. printf("\ncould not find a usable WinSock DLL.\n");
  35. return;
  36. }
  37.  
  38.  
  39. if ( LOBYTE( wsaData.wVersion ) != 2 ||
  40. HIBYTE( wsaData.wVersion ) != 2 ) {
  41. WSACleanup( );
  42. return;
  43. }
  44.  
  45.  
  46. he = gethostbyname(addr);
  47. if(he != NULL)
  48. printf("%s", he->h_name);
  49. else
  50. {
  51. printf("\nNULL....");
  52. int err = WSAGetLastError();
  53. CString cserr = _T("");
  54. switch(err)
  55. {
  56. case WSANOTINITIALISED:
  57. cserr.Format("A successful WSAStartup must occur before using this function.");
  58. printf("\n reached here1");
  59. break;
  60. case WSAENETDOWN:
  61. cserr.Format("The network subsystem has failed.");
  62. printf("\n reached here2");
  63. case WSAEADDRINUSE :
  64. cserr.Format("A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address re-use with SO_REUSEADDR. For example, IP address and port are bound in the af_inet case) . (See the SO_REUSEADDR socket option under setsockopt.)");
  65. printf("\n reached here3");
  66. break;
  67. case WSAEADDRNOTAVAIL:
  68. cserr.Format("The specified address is not a valid address for this machine");
  69. printf("\n reached here4");
  70. break;
  71. case WSAEFAULT:
  72. cserr.Format("The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor");
  73. printf("\n reached here5");
  74. break;
  75. case WSAEINPROGRESS:
  76. cserr.Format("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function");
  77. printf("\n reached here6");
  78. break;
  79. case WSAEINVAL:
  80. cserr.Format("The socket is already bound to an address. (accept case) The listen function was not invoked prior to accept.");
  81. printf("\n reached here7");
  82. break;
  83. case WSAENOBUFS:
  84. cserr.Format("Not enough buffers available, too many connections");
  85. printf("\n reached here8");
  86. break;
  87. case WSAENOTSOCK:
  88. cserr.Format("The descriptor is not a socket.");
  89. printf("\n reached here9");
  90. break;
  91. case WSAEISCONN:
  92. cserr.Format("The socket is already connected.");
  93. printf("\n reached here10");
  94. break;
  95. case WSAEMFILE:
  96. cserr.Format("No more socket descriptors are available or (accept case) The queue is nonempty upon entry to accept and there are no descriptors available.");
  97. printf("\n reached here11");
  98. break;
  99. case WSAEOPNOTSUPP:
  100. cserr.Format("The referenced socket is not of a type that supports the operation.");
  101. printf("\n reached here12");
  102. break;
  103. case WSAEINTR:
  104. cserr.Format("A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.");
  105. printf("\n reached here13");
  106. break;
  107. case WSAEWOULDBLOCK:
  108. cserr.Format("The socket is marked as nonblocking and no connections are present to be accepted.");
  109. printf("\n reached here14");
  110. break;
  111. case WSAEALREADY:
  112. cserr.Format("A nonblocking connect call is in progress on the specified socket");
  113. printf("\n reached here15");
  114. break;
  115. case WSAEAFNOSUPPORT:
  116. cserr.Format("Addresses in the specified family cannot be used with this socket.");
  117. printf("\n reached here16");
  118. break;
  119. case WSAECONNREFUSED:
  120. cserr.Format("The attempt to connect was forcefully rejected.");
  121. printf("\n reached here17");
  122. break;
  123. case WSAENETUNREACH:
  124. cserr.Format("The network cannot be reached from this host at this time.");
  125. printf("\n reached here18");
  126. break;
  127. case WSAETIMEDOUT:
  128. cserr.Format("Attempt to connect timed out without establishing a connection.");
  129. printf("\n reached here19");
  130. break;
  131. case WSAENOTCONN:
  132. cserr.Format("The socket is not connected");
  133. printf("\n reached here20");
  134. break;
  135. case WSAENETRESET:
  136. cserr.Format("The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress");
  137. printf("\n reached here21");
  138. break;
  139. case WSAESHUTDOWN:
  140. cserr.Format("The socket has been shut down; it is not possible to recv on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.");
  141. printf("\n reached here22");
  142. break;
  143. case WSAEMSGSIZE:
  144. cserr.Format("The message was too large to fit into the specified buffer and was truncated.");
  145. printf("\n reached here23");
  146. break;
  147. case WSAECONNABORTED:
  148. cserr.Format("The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.");
  149. printf("\n reached here24");
  150. break;
  151. case WSAECONNRESET:
  152. cserr.Format("The virtual circuit was reset by the remote side executing a \"hard\" or \"abortive\" close. The application should close the socket as it is no longer usable. On a UDP datagram socket this error would indicate that a previous send operation resulted in an ICMP \"Port Unreachable\" message.");
  153. printf("\n reached here25");
  154. break;
  155. default:
  156. cserr.Format("Unknown error");
  157. printf("\n reached here26");
  158.  
  159. }
  160. }
  161. getchar();
  162. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 12
Reputation: numerouno543 is an unknown quantity at this point 
Solved Threads: 0
numerouno543 numerouno543 is offline Offline
Newbie Poster

Re: .exe crashing on executing output !!

 
0
  #4
Jun 21st, 2005
I found out the error...... it is "Valid name, no data record of requested type" !!...Now what dies this mean and how to solve it??
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 12
Reputation: numerouno543 is an unknown quantity at this point 
Solved Threads: 0
numerouno543 numerouno543 is offline Offline
Newbie Poster

Re: .exe crashing on executing output !!

 
0
  #5
Jun 21st, 2005
Hey people....
I finally resolved all errors...and the program is working fine now.....thanks a lott for the help...
Now, I wish to make a DNS query to a web server, which depending upon my query will take some action... I have no idea how to go about doing it..... I think we'll need a DNS server on the web server computer...or something like that......please give me some idea how to proceed...
how does gethostbyname work? does it approach the website in question?
regards.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC