Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to query a http request from a vc++ project
    primarykey
    data
    text
    <p>I am writing a sample program to get a file from some site. but it is replying me file not found errors,i dont know how to make an http header. anyone help me in writing http headers.</p> <p>AND i am trying to print the ip in this way(printf("%s\n",ptr->ai_addr->sa_data);) but not getting it how to print ip address??,am i doing it right??</p> <p>my code is:-</p> <pre><code>#define WIN32_LEAN_AND_MEAN #include &lt;windows.h&gt; #include &lt;winsock2.h&gt; #include &lt;ws2tcpip.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #define WIN32_LEAN_AND_MEAN #include &lt;windows.h&gt; #include &lt;winsock2.h&gt; #include &lt;ws2tcpip.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "80" int __cdecl main(int argc, char **argv) { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; char *sendbuf = "GET / HTTP/1.1[CRLF] Host: www.espncricinfo.com[CRLF]Connection: close[CRLF]User-Agent: Web-sniffer/1.0.37 (+http://web-sniffer.net/)[CRLF]Accept-Encoding: gzip[CRLF]Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]Cache-Control: no-cache[CRLF]Accept-Language: de,en;q=0.7,en-us;q=0.3 \r\n"; char buff[DEFAULT_BUFLEN],recvbuf[512]; int iResult; int recvbuflen = DEFAULT_BUFLEN; // Validate the parameters if (argc != 1) { printf("usage: %s server-name\n", argv[0]); return 1; } // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } ZeroMemory( &amp;hints, sizeof(hints) ); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // Resolve the server address and port iResult = getaddrinfo("122.169.255.9",DEFAULT_PORT, &amp;hints, &amp;result); if ( iResult != 0 ) { printf("getaddrinfo failed with error: %d\n", iResult); WSACleanup(); return 1; } // Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr-&gt;ai_next) { // Create a SOCKET for connecting to server ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol); printf("inside loop %s ",ptr-&gt;ai_addr-&gt;sa_data); if (ConnectSocket == INVALID_SOCKET) { printf("socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } // Connect to server. iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen); printf("%d\n",ptr-&gt;ai_addrlen); // printf("%s\n",ptr-&gt;ai_addr-&gt;sa_family); printf("%s\n",ptr-&gt;ai_addr-&gt;sa_data); if (iResult == SOCKET_ERROR) { printf("socket error\n"); printf("failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { printf("Unable to connect to server!\n"); WSACleanup(); return 1; } // Send an initial buffer char filepath[]="pakistan/content/current/story/517271.html"; sprintf(buff,"GET %s\r\n",filepath); iResult = send( ConnectSocket,buff, (int)strlen(buff), 0 ); if (iResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } printf("Bytes Sent: %ld\n", iResult); // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); //if ( iResult &gt; 0 ) printf("Bytes received: %d\n", iResult); if ( iResult &gt; 0 ) { printf("Bytes received: %d\n", iResult); printf("%s",recvbuf); } else if ( iResult == 0 ) printf("Connection closed\n"); else printf("recv failed with error: %d\n", WSAGetLastError()); } while( iResult&gt;0 ); // cleanup closesocket(ConnectSocket); WSACleanup(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload