eilidh 0 Newbie Poster

This is an assignment where the server uses a socket to communicate with a web browser however it has to be multi threaded but I am not sure on how to add code for the threads, i have attempted it but got no where.

thanks.

/********************************************************************
*	Systems Programming (COMU311)									*
*																	*
*	StreamServer:													*
*	Creates a Stream Socket and displays any information sent by	*
*	a client application.											*
*																	*
*																	*
*	Brian Hainey, Glasgow Caledonian University						*
********************************************************************/
	
#include <windows.h>
#include <iostream>
#include <assert.h>
#include <winsock.h>
#include "Assignment2Util.h"

using namespace std;


#define BUFF_SIZE 4096
#define SERVER_PORT 8080  /* The servers port, clients will use this */

//*****************************************************************
//add a prototype for the thread function to be used by all threads
//*****************************************************************
DWORD WINAPI SPThreadFunction(LPVOID lpParam);

void main()
{
	cout << "Server process running" << endl;
	
//********************************
//Initialise the Sockets Library
//version 1.1 is all that's needed
//********************************
	
	WSADATA wsaData;
	WORD wVersionRequired = MAKEWORD(1, 1);
	int nWSStatus = WSAStartup( wVersionRequired, &wsaData );
	assert(nWSStatus == 0);


//*********************************************************
//create a Stream Socket that will listen for connections
//*********************************************************

    SOCKET listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
    assert(listeningSocket != INVALID_SOCKET);

//*********************************************************
//bind so that we have a connection point i.e. IP plus port
//use port SERVER_PORT and assign any valid IP address
//*********************************************************

	SOCKADDR_IN serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(SERVER_PORT);
    serverAddress.sin_addr.s_addr = INADDR_ANY;
    nWSStatus = bind(	listeningSocket, 
						(SOCKADDR*) &serverAddress, 
						sizeof(serverAddress));
    assert (nWSStatus != SOCKET_ERROR);
	

//*********************************************************
//place socket in state where it can listen for connections
//*********************************************************
    
	nWSStatus = listen(listeningSocket, SOMAXCONN);
    assert(nWSStatus != SOCKET_ERROR);

//************************************************************
//accept any connection, will block until a connection is made
//************************************************************

	SOCKADDR_IN clientAddress;	//will hold the address details of the client
								//who has connected to this socket
    int clientAddressLength = sizeof(clientAddress);
    SOCKET communicationSocket = accept(	listeningSocket, 
											(SOCKADDR*) &clientAddress, 
											&clientAddressLength);
    assert(communicationSocket !=  INVALID_SOCKET);

	
	//************************************************
	//declare variables to hold the ids of two threads
	//and their handles
	//************************************************

	DWORD	dwMyThreadId1;
	HANDLE	hMyThread1; 
	
	int	myInt1 = 2001;
	//**********************************************************************
	//indicate, on the console, that the two threads are about to be created
	//and wait for the key entry before continuing
	//**********************************************************************
	cout << "About to create threads." << endl;

	//**********************
	//create the two threads
	//**********************
   hMyThread1 = CreateThread(	NULL,				// security attributes
								0,					// use default stack size
								SPThreadFunction,	// pointer to thread function 
								&myInt1,			// pointer to thread function parameter 
								0,					// use default creation flags 
								&dwMyThreadId1		// pointer to returned thread identifier 
							);
	assert(hMyThread1 != NULL);
	

//*************************************
//Read a single message from the client
//and display it
//*************************************
	
	char        messageData[500];

	nWSStatus = recv(	communicationSocket, 
						messageData, 
						sizeof(messageData), 
						0);

    assert(nWSStatus != SOCKET_ERROR);


	cout << "The message is : " <<
			messageData <<
			endl;

	char pathname[256];
	int pathsize = 256;
	bool validrequest = ParseHttpRequest(messageData,pathname, &pathsize);

	int bufferSize;
	//char messageBody;
	int bodySize = 256;
	char * pResponse;

	
	//pResponse = CreateHttpResponse(NOT_IMPLEMENTED_STATUS,  &bufferSize, NULL, 0);


	if(validrequest == false){

	pResponse = CreateHttpResponse(NOT_IMPLEMENTED_STATUS,  &bufferSize, NULL, 0);
	cout << pResponse << endl;
	}

	//cout << "response is" << pResponse << endl;

	//cout << " size of presponse is" << sizeof(pResponse) << endl;
	
	//cout << "pathname of the GET command is " << pathname << endl;

	
	
	if(validrequest == true){

//ADDRESS********************
		char absoloutePathname [256];
		sprintf(absoloutePathname, "C:/Students/%s", pathname);
	
		cout << absoloutePathname << endl;

//************************************
//open the file called comu311demo.txt
//It must be possible to read the file
//************************************
	
	HANDLE hRequiredFile = CreateFile(	
									  absoloutePathname,		//the file name
									  GENERIC_READ,				// open for reading
									  0,						// no sharing
									  NULL,						// no security
									  OPEN_EXISTING,			// open file, fail if does not exist
									  FILE_ATTRIBUTE_NORMAL,	// no special attributes
									  NULL);					// no template file


	if (hRequiredFile == INVALID_HANDLE_VALUE)
		pResponse = CreateHttpResponse(NOT_FOUND_STATUS,  &bufferSize, NULL, 0);

		cout << pResponse << endl;
		//cout << "File doesnt exsist" << endl;

///**************************************
//read all the information from the file
//**************************************
	
	char pstrBuffer[BUFF_SIZE];
	DWORD dwNumberOfBytesRead;
		
		 ReadFile(	hRequiredFile,
					pstrBuffer,
					BUFF_SIZE,
					&dwNumberOfBytesRead,
								NULL);
		

	pResponse = CreateHttpResponse(OK_STATUS,  &bufferSize, pstrBuffer, dwNumberOfBytesRead);
	cout << pResponse << endl;
	
//**************
//close the file
//**************
	
	BOOL bCloseStatus = CloseHandle(hRequiredFile);
	assert(bCloseStatus != 0);

		

}//END OF IF**************

	

//SEND RESPONSE BACK***********

	nWSStatus = send(	communicationSocket, 
						pResponse, 
						bufferSize, 
						0);
    assert(nWSStatus != SOCKET_ERROR);

	
//******************************************************
//close the communication socket, it is no longer needed
//******************************************************
	
	closesocket(communicationSocket);

//**************************************************
//close the listening socket, it is no longer needed
//**************************************************
	
	closesocket(listeningSocket);
	char temp;
	cin >> temp;

DWORD WINAPI SPThreadFunction(LPVOID lpParam)
{
    MessageBox(NULL, "Systems Programming Thread", "Systems Programming", MB_OK);
    return 0;
}

//****************************
//Terminate use of Sockets Lib
//****************************
	
	WSACleanup();

	cout << "Done" << endl;

} //end of main

<< moderator edit: added [code][/code] tags >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.