Expert help required for C++ newbie

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::bi­nary);
if (Resource.is_open())
{
ResponseBuffer = (char
*)malloc(MAX_RESPONSE_SIZE);

Resource.read(ResponseBuffer,M­AX_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,Resour­cePathLength,0);
HeapFree(GetProcessHeap(),0,re­sponse);
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);
}
 
M

Mike Wahler

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.

Perhaps because that code is not really 'simple'. It uses many
nonstandard platform-specific features which are not part of the
C++ language. I suggest you start with something much simpler.
If some (clever) person could explain
each of the funtions I would appreciate it. Thanks for having a look.

Virtually all of your code is nonstandard (Microsoft Windows specific).
We only discuss the ISO standard C++ language here. You can get help
with that code at newsgroup comp.os.ms-windows.programmer.win32
If your news server does not offer that group, you can access it by
connecting to Microsoft's public news server at msnews.microsoft.com

Also note that all of the Microsoft-specific stuff is well-documented
by Microsoft's developer support site, www.msdn.microsoft.com

Purpose of comp.lang.c++:
http://www.slack.net/~shiva/welcome.txt

-Mike
 
H

Howard

That code refers to threads and sockets, both of which are off-topic here,
where only "standard" C++ language issues are discussed. Sockets and
threads are not part of the Standard, so they're not discussed here.

You'll need to ask in a newsgroup where they discuss that stuff.
Presumably, one of those on the news.microsoft.com server.

-Howard
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top