Http winsock problem

Discussion in 'C++' started by raj.rr7@gmail.com, Feb 13, 2007.

  1. Guest

    Hi,

    I am creating a Http client application using winsock, evc++ 4.0 and K-
    Jam (WIndows Mobile 5) that will get data from Http server. I am using
    the following code.

    WSADATA wsaData;
    CString csErr;

    memset(&wsaData, 0, sizeof(WSADATA));
    if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
    {
    AfxMessageBox(_T("WSAStartup Error"));
    return;
    //return FALSE;
    }


    // Create a connection-oriented socket
    SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // Check to see if we have a valid socket
    if(s == INVALID_SOCKET)
    {
    //AfxMessageBox(_T("Error Invalid Socket"));
    int iSocketError = WSAGetLastError();
    csErr.Format(_T("Socket Error :: %d"),iSocketError);
    AfxMessageBox(csErr);

    return;
    //return FALSE;
    }

    // Get the host information
    HOSTENT *hostServer = gethostbyname("www.xxxxxx.com");
    if(hostServer == NULL)
    {
    int iSocketError = WSAGetLastError();
    csErr.Format(_T("GetHostByName Error :: %d"),iSocketError);
    AfxMessageBox(csErr);
    //AfxMessageBox(_T("Error Get Host By Name"));
    return;
    //return FALSE;
    }


    // Set up the target device address structure
    SOCKADDR_IN sinServer;

    memset(&sinServer, 0, sizeof(SOCKADDR_IN));

    sinServer.sin_family = AF_INET;
    sinServer.sin_port = htons(80);
    sinServer.sin_addr = *((IN_ADDR *)hostServer->h_addr_list[0]);

    // Connect
    if(connect(s, (SOCKADDR *)&sinServer, sizeof(sinServer)) ==
    SOCKET_ERROR)
    {
    int iSocketError = WSAGetLastError();
    csErr.Format(_T("Connect Error :: %d"),iSocketError);
    AfxMessageBox(csErr);
    return;
    //return FALSE;
    }

    // Send a request to the server
    char cBuffer[1024] = "";
    int nBytesSent = 0;
    int nBytesIndex = 0;

    // Set up the buffer to send
    //sprintf(cBuffer, "GET / HTTP/1.0\r\n\r\n");


    sprintf(cBuffer, "GET /activation/activate.php?Version=3.0b HTTP/1.0\r
    \n\r\n");
    int nBytesLeft = strlen(cBuffer);

    // Send the entire buffer
    while(nBytesLeft > 0)
    {
    nBytesSent = send(s, &cBuffer[nBytesIndex], nBytesLeft, 0);
    if(nBytesSent == SOCKET_ERROR)
    {
    AfxMessageBox(_T("Error Sending Data"));
    break;
    }

    csErr.Format(_T("Data Send :: %d"),nBytesSent);
    AfxMessageBox(csErr);

    // See how many bytes are left. If we still need to send, loop
    nBytesLeft -= nBytesSent;
    csErr.Format(_T("Sending Bytes Left :: %d"),nBytesLeft);
    AfxMessageBox(csErr);
    nBytesIndex += nBytesSent;
    }

    // Get the response
    TCHAR tchResponseBuffer[1024] = TEXT("\0");
    char cResponseBuffer[1024] = "";
    BOOL fBreak = FALSE;
    int nBytesReceived = 0;

    while(!fBreak)
    {
    nBytesReceived = recv(s, &cResponseBuffer[0], 1024, 0);
    if(nBytesReceived == SOCKET_ERROR)
    {
    AfxMessageBox(_T("Error Receiving Data"));
    break;
    }

    csErr.Format(_T("Bytes Received :: %d"),nBytesReceived);
    AfxMessageBox(csErr);

    //AfxMessageBox(_T("Data Received Successfully"));
    // Convert the data from ANSI to Unicode
    mbstowcs(tchResponseBuffer, cResponseBuffer, nBytesReceived);

    // Show the MessageBox
    AfxMessageBox(tchResponseBuffer);

    // Check to see if this is the end of the HTTP response by
    // looking for \r\n\r\n
    if(_tcsstr(tchResponseBuffer, TEXT("\r\n\r\n")))
    fBreak = TRUE;

    // Clear the buffers
    memset(tchResponseBuffer, 0, 1024);
    memset(cResponseBuffer, 0, 1024);
    }

    closesocket(s);
    WSACleanup();

    Everything works fine but i get 500 Internal server error. Can anybody
    has faced the same issue? Is that due to the HTTP/1.0 version or is it
    a PHP proble?

    Regards
    Rajat
    , Feb 13, 2007
    #1
    1. Advertising

  2. Send the "Host: " header too. Multi-homed servers, that ordinarily expect
    HTTP/1.1 requests, often cannot figure out HTTP/1.0 requests unless the
    request also includes the "Host:"


    <> wrote in message
    news:...
    > Hi,
    >
    > I am creating a Http client application using winsock, evc++ 4.0 and K-
    > Jam (WIndows Mobile 5) that will get data from Http server. I am using
    > the following code.
    >
    > WSADATA wsaData;
    > CString csErr;
    >
    > memset(&wsaData, 0, sizeof(WSADATA));
    > if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
    > {
    > AfxMessageBox(_T("WSAStartup Error"));
    > return;
    > //return FALSE;
    > }
    >
    >
    > // Create a connection-oriented socket
    > SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    >
    > // Check to see if we have a valid socket
    > if(s == INVALID_SOCKET)
    > {
    > //AfxMessageBox(_T("Error Invalid Socket"));
    > int iSocketError = WSAGetLastError();
    > csErr.Format(_T("Socket Error :: %d"),iSocketError);
    > AfxMessageBox(csErr);
    >
    > return;
    > //return FALSE;
    > }
    >
    > // Get the host information
    > HOSTENT *hostServer = gethostbyname("www.xxxxxx.com");
    > if(hostServer == NULL)
    > {
    > int iSocketError = WSAGetLastError();
    > csErr.Format(_T("GetHostByName Error :: %d"),iSocketError);
    > AfxMessageBox(csErr);
    > //AfxMessageBox(_T("Error Get Host By Name"));
    > return;
    > //return FALSE;
    > }
    >
    >
    > // Set up the target device address structure
    > SOCKADDR_IN sinServer;
    >
    > memset(&sinServer, 0, sizeof(SOCKADDR_IN));
    >
    > sinServer.sin_family = AF_INET;
    > sinServer.sin_port = htons(80);
    > sinServer.sin_addr = *((IN_ADDR *)hostServer->h_addr_list[0]);
    >
    > // Connect
    > if(connect(s, (SOCKADDR *)&sinServer, sizeof(sinServer)) ==
    > SOCKET_ERROR)
    > {
    > int iSocketError = WSAGetLastError();
    > csErr.Format(_T("Connect Error :: %d"),iSocketError);
    > AfxMessageBox(csErr);
    > return;
    > //return FALSE;
    > }
    >
    > // Send a request to the server
    > char cBuffer[1024] = "";
    > int nBytesSent = 0;
    > int nBytesIndex = 0;
    >
    > // Set up the buffer to send
    > //sprintf(cBuffer, "GET / HTTP/1.0\r\n\r\n");
    >
    >
    > sprintf(cBuffer, "GET /activation/activate.php?Version=3.0b HTTP/1.0\r
    > \n\r\n");
    > int nBytesLeft = strlen(cBuffer);
    >
    > // Send the entire buffer
    > while(nBytesLeft > 0)
    > {
    > nBytesSent = send(s, &cBuffer[nBytesIndex], nBytesLeft, 0);
    > if(nBytesSent == SOCKET_ERROR)
    > {
    > AfxMessageBox(_T("Error Sending Data"));
    > break;
    > }
    >
    > csErr.Format(_T("Data Send :: %d"),nBytesSent);
    > AfxMessageBox(csErr);
    >
    > // See how many bytes are left. If we still need to send, loop
    > nBytesLeft -= nBytesSent;
    > csErr.Format(_T("Sending Bytes Left :: %d"),nBytesLeft);
    > AfxMessageBox(csErr);
    > nBytesIndex += nBytesSent;
    > }
    >
    > // Get the response
    > TCHAR tchResponseBuffer[1024] = TEXT("\0");
    > char cResponseBuffer[1024] = "";
    > BOOL fBreak = FALSE;
    > int nBytesReceived = 0;
    >
    > while(!fBreak)
    > {
    > nBytesReceived = recv(s, &cResponseBuffer[0], 1024, 0);
    > if(nBytesReceived == SOCKET_ERROR)
    > {
    > AfxMessageBox(_T("Error Receiving Data"));
    > break;
    > }
    >
    > csErr.Format(_T("Bytes Received :: %d"),nBytesReceived);
    > AfxMessageBox(csErr);
    >
    > //AfxMessageBox(_T("Data Received Successfully"));
    > // Convert the data from ANSI to Unicode
    > mbstowcs(tchResponseBuffer, cResponseBuffer, nBytesReceived);
    >
    > // Show the MessageBox
    > AfxMessageBox(tchResponseBuffer);
    >
    > // Check to see if this is the end of the HTTP response by
    > // looking for \r\n\r\n
    > if(_tcsstr(tchResponseBuffer, TEXT("\r\n\r\n")))
    > fBreak = TRUE;
    >
    > // Clear the buffers
    > memset(tchResponseBuffer, 0, 1024);
    > memset(cResponseBuffer, 0, 1024);
    > }
    >
    > closesocket(s);
    > WSACleanup();
    >
    > Everything works fine but i get 500 Internal server error. Can anybody
    > has faced the same issue? Is that due to the HTTP/1.0 version or is it
    > a PHP proble?
    >
    > Regards
    > Rajat
    >
    Michael K. O'Neill, Feb 13, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Thompson

    server using winsock

    Chris Thompson, Sep 2, 2003, in forum: C++
    Replies:
    1
    Views:
    425
    Jim Fischer
    Sep 2, 2003
  2. Christian Drescher

    Winsock and HTTP

    Christian Drescher, Sep 6, 2004, in forum: C++
    Replies:
    6
    Views:
    1,830
    John Harrison
    Sep 7, 2004
  3. Dom
    Replies:
    1
    Views:
    12,577
  4. Gary C
    Replies:
    2
    Views:
    2,182
    Jorgen Grahn
    May 19, 2010
  5. Xiangrong Fang

    winsock problem?

    Xiangrong Fang, Jul 28, 2003, in forum: Ruby
    Replies:
    9
    Views:
    147
    Rasputin
    Jul 29, 2003
Loading...

Share This Page