H
hoggmeister
Hi I'm new to C++ and have started reading some books with a view to
doing a course at the end of the year. I've mostly been looking at
simple examples and trying to figure them out but I've now got to this
and have no idea whats going on. If some (clever) person could explain
each of the funtions I would appreciate it. Thanks for having a look.
#include "Assignment2Util.h"
#include <assert.h>
#include <Winsock2.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//limit the max response to 10MB
const MAX_RESPONSE_SIZE = 10000000;
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
SOCKET *incoming = (SOCKET *)lpParam;
char RequestBuffer[5000];
char *ResponseBuffer;
char ResourcePath[300];
int ResourcePathLength=300;
char filename[500];
char *response;
int err,bytes_read=0;
recv(*incoming, RequestBuffer, 3000, 0);
err = ParseHttpRequest(RequestBuffer, ResourcePath,
&ResourcePathLength);
if (err==0)
{
response = CreateHttpResponse( NOT_IMPLEMENTED_STATUS,
&ResourcePathLength, NULL,0);
}
else
{
strcpy(filename,"c:\\Students\\");
strcat(filename,ResourcePath);
ifstream Resource;
Resource.open(filename,ios::binary);
if (Resource.is_open())
{
ResponseBuffer = (char
*)malloc(MAX_RESPONSE_SIZE);
Resource.read(ResponseBuffer,MAX_RESPONSE_SIZE);
bytes_read = Resource.gcount();
Resource.close();
response = CreateHttpResponse( OK_STATUS,
&ResourcePathLength,
ResponseBuffer,bytes_read);
free(ResponseBuffer);
}
else
response = CreateHttpResponse(
NOT_FOUND_STATUS,
&ResourcePathLength, NULL,0);
}
send(*incoming,response,ResourcePathLength,0);
HeapFree(GetProcessHeap(),0,response);
closesocket(*incoming);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int err;
WORD wVersion;
DWORD ThreadId;
wVersion = MAKEWORD( 2, 2 );
err = WSAStartup( wVersion, &wsaData );
SOCKET listeningSocket;
SOCKADDR_IN sinfo;
listeningSocket=socket(AF_INET,SOCK_STREAM,0);
sinfo.sin_family = AF_INET;
sinfo.sin_addr.s_addr = INADDR_ANY;
sinfo.sin_port = htons(8080);
err = bind(listeningSocket, (LPSOCKADDR)&sinfo, sizeof(struct
sockaddr));
if (err == SOCKET_ERROR)
{
cout<<"Error binding socket"<<endl;
closesocket(listeningSocket);
return -1;
}
err = listen(listeningSocket, 10);
if (err!=SOCKET_ERROR)
{
while (true)
{
SOCKET incoming= accept(listeningSocket, NULL, NULL);
if (incoming==INVALID_SOCKET)
cerr << "Problem with the incoming socket\n";
else
CreateThread(NULL,0,ThreadProc,&incoming,0,&ThreadId);
}
}
else
{
cout << "Problem setting up socket for listening\n";
closesocket(listeningSocket);
}
doing a course at the end of the year. I've mostly been looking at
simple examples and trying to figure them out but I've now got to this
and have no idea whats going on. If some (clever) person could explain
each of the funtions I would appreciate it. Thanks for having a look.
#include "Assignment2Util.h"
#include <assert.h>
#include <Winsock2.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//limit the max response to 10MB
const MAX_RESPONSE_SIZE = 10000000;
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
SOCKET *incoming = (SOCKET *)lpParam;
char RequestBuffer[5000];
char *ResponseBuffer;
char ResourcePath[300];
int ResourcePathLength=300;
char filename[500];
char *response;
int err,bytes_read=0;
recv(*incoming, RequestBuffer, 3000, 0);
err = ParseHttpRequest(RequestBuffer, ResourcePath,
&ResourcePathLength);
if (err==0)
{
response = CreateHttpResponse( NOT_IMPLEMENTED_STATUS,
&ResourcePathLength, NULL,0);
}
else
{
strcpy(filename,"c:\\Students\\");
strcat(filename,ResourcePath);
ifstream Resource;
Resource.open(filename,ios::binary);
if (Resource.is_open())
{
ResponseBuffer = (char
*)malloc(MAX_RESPONSE_SIZE);
Resource.read(ResponseBuffer,MAX_RESPONSE_SIZE);
bytes_read = Resource.gcount();
Resource.close();
response = CreateHttpResponse( OK_STATUS,
&ResourcePathLength,
ResponseBuffer,bytes_read);
free(ResponseBuffer);
}
else
response = CreateHttpResponse(
NOT_FOUND_STATUS,
&ResourcePathLength, NULL,0);
}
send(*incoming,response,ResourcePathLength,0);
HeapFree(GetProcessHeap(),0,response);
closesocket(*incoming);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int err;
WORD wVersion;
DWORD ThreadId;
wVersion = MAKEWORD( 2, 2 );
err = WSAStartup( wVersion, &wsaData );
SOCKET listeningSocket;
SOCKADDR_IN sinfo;
listeningSocket=socket(AF_INET,SOCK_STREAM,0);
sinfo.sin_family = AF_INET;
sinfo.sin_addr.s_addr = INADDR_ANY;
sinfo.sin_port = htons(8080);
err = bind(listeningSocket, (LPSOCKADDR)&sinfo, sizeof(struct
sockaddr));
if (err == SOCKET_ERROR)
{
cout<<"Error binding socket"<<endl;
closesocket(listeningSocket);
return -1;
}
err = listen(listeningSocket, 10);
if (err!=SOCKET_ERROR)
{
while (true)
{
SOCKET incoming= accept(listeningSocket, NULL, NULL);
if (incoming==INVALID_SOCKET)
cerr << "Problem with the incoming socket\n";
else
CreateThread(NULL,0,ThreadProc,&incoming,0,&ThreadId);
}
}
else
{
cout << "Problem setting up socket for listening\n";
closesocket(listeningSocket);
}