Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Http winsock problem

Reply
Thread Tools

Http winsock problem

 
 
raj.rr7@gmail.com
Guest
Posts: n/a
 
      02-13-2007
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

 
Reply With Quote
 
 
 
 
Michael K. O'Neill
Guest
Posts: n/a
 
      02-13-2007
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:"


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP multicast problem with winsock Gary C C++ 2 05-19-2010 12:18 PM
Winsock & Winsock 2 spears list Computer Support 3 10-16-2007 03:28 AM
winsock/mingw compile problem (undefined reference) Dom C++ 1 08-18-2005 12:05 PM
Winsock and HTTP Christian Drescher C++ 6 09-07-2004 03:50 PM
WINSOCK problem??? ANON Computer Support 3 10-05-2003 09:57 PM



Advertisments