winsock doesn't send "GET"

hi
i'm writing a winsock2 program to send a simple http get and receive the server's answer. anything i send to the server is sent properly exept when i use HTTP commands.
for example i tried sending "JET / HTTP/1.1" to the server and it's sent. but when i send "GET / HTTP/1.1" it is not sent. the connection is stablished and then it is shut down by the client.(i used a packet analyzer, nothing is sent exept the 3 handshake messages)
i have tried other http methods like POST but the result is the same. i don't know if i'm doing something wrong, because only http commands are not sent.
i even copy/pasted the sample basic winsock program from microsoft msdn. but http commands are not sent though.

thanks for you responses
Last edited on
Please post your full source code and tell us what windows version are you using. Also check with a known working webserver, not your own.
system: winxp sp3
i have tried google,some other websites and my own webserver. none of wich worked.
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
#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 80
int main() {

    //----------------------
    // Declare and initialize variables.
    int iResult;
    WSADATA wsaData;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;

    int recvbuflen = DEFAULT_BUFLEN;
    char *sendbuf = "GET / HTTP/1.1\r\n"; 

when i change sendbuf to "blablabla" it is sent over the network, but now nothing happens. no error though.
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
68
69
70
71
72
73
    char recvbuf[DEFAULT_BUFLEN] = "";

    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "209.85.148.105" );
    clientService.sin_port = htons( DEFAULT_PORT );

    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if (iResult == SOCKET_ERROR) {
        wprintf(L"connect failed with error: %d\n", WSAGetLastError() );
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
  }

    //----------------------
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        wprintf(L"send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf("Bytes Sent: %d\n", iResult);

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            wprintf(L"Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            wprintf(L"Connection closed\n");
        else
            wprintf(L"recv failed with error: %d\n", WSAGetLastError());

    } while( iResult > 0 );


    // close the socket
    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"close failed with error: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    WSACleanup();
    return 0;
}
Last edited on
You'll find you need to send a bit more in HTTP 1.1, why not test with 1.0?

I think you need a blank line to indicate you're done. The server is waiting for more while you're waiting for it to respond.
Yes, the server is waiting for two carriage return line feeds as it indicates the end of the HTTP header/command (as far as I know).
Last edited on
i actually send the carriage returns: "GET / HTTP/1.1\r\n" and also tried without returns"GET / HTTP/1.1". neither requests are sent over the network.
and as i said the problem is not with the server, the request is not been sent at all. with a packet sniffer, i can see the the packets are sent, for example "asdfasdfasdf" is sent but server doesn't respond of course, but when i change "asdfasdfasdf" to "GET / HTTP/1.1" the program doesn't send it to the server. nothing happens and connection is terminated by my program.

things i have already tried:
turning off firewall, using another computer to compile and run the program, changing the send buffer size, using an official source code (the above code is actually copy/pasted from microsoft msdn), smacking the keyboard, swearing programing languages.
You need to send "GET / HTTP/1.1\r\n\r\n"
HTTP 1.1 also requires the "Host: " header to be set.
const char* headers = "GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n";

Send this over the network and it MUST work.
Last edited on
Why don't use Wininet?
Thanks alot
the \r\n\r\n worked. i sent and received data.
however it seems a little strange to me. blabla is sent but a wrong GET command is not!
Like I said, the server knows that JET is an invalid command so terminates the evaluation immediately. When it sees GET for HTTP 1.1, it knows that you can send all kinds of stuff, but the request terminates with a blank line, so it waits for the blank line that you never send.

It's not strange at all.
Topic archived. No new replies allowed.