Help with multithread server

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 1
Reputation: eilidh is an unknown quantity at this point 
Solved Threads: 0
eilidh eilidh is offline Offline
Newbie Poster

Help with multithread server

 
0
  #1
Apr 27th, 2005
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.

  1. /********************************************************************
  2. * Systems Programming (COMU311) *
  3. * *
  4. * StreamServer: *
  5. * Creates a Stream Socket and displays any information sent by *
  6. * a client application. *
  7. * *
  8. * *
  9. * Brian Hainey, Glasgow Caledonian University *
  10. ********************************************************************/
  11.  
  12. #include <windows.h>
  13. #include <iostream>
  14. #include <assert.h>
  15. #include <winsock.h>
  16. #include "Assignment2Util.h"
  17.  
  18. using namespace std;
  19.  
  20.  
  21. #define BUFF_SIZE 4096
  22. #define SERVER_PORT 8080 /* The servers port, clients will use this */
  23.  
  24. //*****************************************************************
  25. //add a prototype for the thread function to be used by all threads
  26. //*****************************************************************
  27. DWORD WINAPI SPThreadFunction(LPVOID lpParam);
  28.  
  29. void main()
  30. {
  31. cout << "Server process running" << endl;
  32.  
  33. //********************************
  34. //Initialise the Sockets Library
  35. //version 1.1 is all that's needed
  36. //********************************
  37.  
  38. WSADATA wsaData;
  39. WORD wVersionRequired = MAKEWORD(1, 1);
  40. int nWSStatus = WSAStartup( wVersionRequired, &wsaData );
  41. assert(nWSStatus == 0);
  42.  
  43.  
  44. //*********************************************************
  45. //create a Stream Socket that will listen for connections
  46. //*********************************************************
  47.  
  48. SOCKET listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
  49. assert(listeningSocket != INVALID_SOCKET);
  50.  
  51. //*********************************************************
  52. //bind so that we have a connection point i.e. IP plus port
  53. //use port SERVER_PORT and assign any valid IP address
  54. //*********************************************************
  55.  
  56. SOCKADDR_IN serverAddress;
  57. serverAddress.sin_family = AF_INET;
  58. serverAddress.sin_port = htons(SERVER_PORT);
  59. serverAddress.sin_addr.s_addr = INADDR_ANY;
  60. nWSStatus = bind( listeningSocket,
  61. (SOCKADDR*) &serverAddress,
  62. sizeof(serverAddress));
  63. assert (nWSStatus != SOCKET_ERROR);
  64.  
  65.  
  66. //*********************************************************
  67. //place socket in state where it can listen for connections
  68. //*********************************************************
  69.  
  70. nWSStatus = listen(listeningSocket, SOMAXCONN);
  71. assert(nWSStatus != SOCKET_ERROR);
  72.  
  73. //************************************************************
  74. //accept any connection, will block until a connection is made
  75. //************************************************************
  76.  
  77. SOCKADDR_IN clientAddress; //will hold the address details of the client
  78. //who has connected to this socket
  79. int clientAddressLength = sizeof(clientAddress);
  80. SOCKET communicationSocket = accept( listeningSocket,
  81. (SOCKADDR*) &clientAddress,
  82. &clientAddressLength);
  83. assert(communicationSocket != INVALID_SOCKET);
  84.  
  85.  
  86. //************************************************
  87. //declare variables to hold the ids of two threads
  88. //and their handles
  89. //************************************************
  90.  
  91. DWORD dwMyThreadId1;
  92. HANDLE hMyThread1;
  93.  
  94. int myInt1 = 2001;
  95. //**********************************************************************
  96. //indicate, on the console, that the two threads are about to be created
  97. //and wait for the key entry before continuing
  98. //**********************************************************************
  99. cout << "About to create threads." << endl;
  100.  
  101. //**********************
  102. //create the two threads
  103. //**********************
  104. hMyThread1 = CreateThread( NULL, // security attributes
  105. 0, // use default stack size
  106. SPThreadFunction, // pointer to thread function
  107. &myInt1, // pointer to thread function parameter
  108. 0, // use default creation flags
  109. &dwMyThreadId1 // pointer to returned thread identifier
  110. );
  111. assert(hMyThread1 != NULL);
  112.  
  113.  
  114. //*************************************
  115. //Read a single message from the client
  116. //and display it
  117. //*************************************
  118.  
  119. char messageData[500];
  120.  
  121. nWSStatus = recv( communicationSocket,
  122. messageData,
  123. sizeof(messageData),
  124. 0);
  125.  
  126. assert(nWSStatus != SOCKET_ERROR);
  127.  
  128.  
  129. cout << "The message is : " <<
  130. messageData <<
  131. endl;
  132.  
  133. char pathname[256];
  134. int pathsize = 256;
  135. bool validrequest = ParseHttpRequest(messageData,pathname, &pathsize);
  136.  
  137. int bufferSize;
  138. //char messageBody;
  139. int bodySize = 256;
  140. char * pResponse;
  141.  
  142.  
  143. //pResponse = CreateHttpResponse(NOT_IMPLEMENTED_STATUS, &bufferSize, NULL, 0);
  144.  
  145.  
  146. if(validrequest == false){
  147.  
  148. pResponse = CreateHttpResponse(NOT_IMPLEMENTED_STATUS, &bufferSize, NULL, 0);
  149. cout << pResponse << endl;
  150. }
  151.  
  152. //cout << "response is" << pResponse << endl;
  153.  
  154. //cout << " size of presponse is" << sizeof(pResponse) << endl;
  155.  
  156. //cout << "pathname of the GET command is " << pathname << endl;
  157.  
  158.  
  159.  
  160. if(validrequest == true){
  161.  
  162. //ADDRESS********************
  163. char absoloutePathname [256];
  164. sprintf(absoloutePathname, "C:/Students/%s", pathname);
  165.  
  166. cout << absoloutePathname << endl;
  167.  
  168. //************************************
  169. //open the file called comu311demo.txt
  170. //It must be possible to read the file
  171. //************************************
  172.  
  173. HANDLE hRequiredFile = CreateFile(
  174. absoloutePathname, //the file name
  175. GENERIC_READ, // open for reading
  176. 0, // no sharing
  177. NULL, // no security
  178. OPEN_EXISTING, // open file, fail if does not exist
  179. FILE_ATTRIBUTE_NORMAL, // no special attributes
  180. NULL); // no template file
  181.  
  182.  
  183. if (hRequiredFile == INVALID_HANDLE_VALUE)
  184. pResponse = CreateHttpResponse(NOT_FOUND_STATUS, &bufferSize, NULL, 0);
  185.  
  186. cout << pResponse << endl;
  187. //cout << "File doesnt exsist" << endl;
  188.  
  189. ///**************************************
  190. //read all the information from the file
  191. //**************************************
  192.  
  193. char pstrBuffer[BUFF_SIZE];
  194. DWORD dwNumberOfBytesRead;
  195.  
  196. ReadFile( hRequiredFile,
  197. pstrBuffer,
  198. BUFF_SIZE,
  199. &dwNumberOfBytesRead,
  200. NULL);
  201.  
  202.  
  203. pResponse = CreateHttpResponse(OK_STATUS, &bufferSize, pstrBuffer, dwNumberOfBytesRead);
  204. cout << pResponse << endl;
  205.  
  206. //**************
  207. //close the file
  208. //**************
  209.  
  210. BOOL bCloseStatus = CloseHandle(hRequiredFile);
  211. assert(bCloseStatus != 0);
  212.  
  213.  
  214.  
  215. }//END OF IF**************
  216.  
  217.  
  218.  
  219. //SEND RESPONSE BACK***********
  220.  
  221. nWSStatus = send( communicationSocket,
  222. pResponse,
  223. bufferSize,
  224. 0);
  225. assert(nWSStatus != SOCKET_ERROR);
  226.  
  227.  
  228. //******************************************************
  229. //close the communication socket, it is no longer needed
  230. //******************************************************
  231.  
  232. closesocket(communicationSocket);
  233.  
  234. //**************************************************
  235. //close the listening socket, it is no longer needed
  236. //**************************************************
  237.  
  238. closesocket(listeningSocket);
  239. char temp;
  240. cin >> temp;
  241.  
  242. DWORD WINAPI SPThreadFunction(LPVOID lpParam)
  243. {
  244. MessageBox(NULL, "Systems Programming Thread", "Systems Programming", MB_OK);
  245. return 0;
  246. }
  247.  
  248. //****************************
  249. //Terminate use of Sockets Lib
  250. //****************************
  251.  
  252. WSACleanup();
  253.  
  254. cout << "Done" << endl;
  255.  
  256. } //end of main
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC