







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Server Architecture, HTTP Web Server Application, Server Shut down user interface, Variable Initialization, Initialize WinSock Library, Win32 Error Codes, Common Gateway Interface. As you can see in this file, how descriptive above mentioned points are explained in this lecture of computer programming. VU is one of best university for computer science in our country.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Server architecture will be based on:
Initialize Windows Sockets Library
if(WSAStartup(MAKEWORD(1,1), &wsaData)) { … … … return 1; }
//Get machine’s hostname and IP address
gethostname(hostName, sizeof(hostName)); ptrHostEnt = gethostbyname(hostName);
//Fill the socket address with appropriate values
serverSocketAddress.sin_family = AF_INET; serverSocketAddress.sin_port = htons(SERVER_PORT); … … … memcpy(&serverSocketAddress.sin_addr.S_un.S_addr, ptrHostEnt->h_addr_list[0], sizeof(unsigned long));
Create the server socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(serverSocket == INVALID_SOCKET) { … … … WSACleanup(); return 1; }
Bind the socket
Accept Client Connection ( Thread Routine )
Terminate communication threads (thread routine)
Create a new communication thread: serveClient
Client Socket Descriptor The newly generated “Communication Thread” (discussed later)
Wait for some thread termination event
Wait for thread object to go signalled
Close thread handle; Destroy its relevant stored data;
Application Variables and constants
#define MAX_CLIENTS 5 SOCKET clientSockets[MAX_CLIENTS];
HANDLE hCommunicationThreads[MAX_CLIENTS]; DWORD dwCommunicationThreads[MAX_CLIENTS];
HANDLE hAcceptingThread; DWORD dwAcceptingThread;
HANDLE hTerminatingThread; DWORD dwTerminatingTThread;
servClient Communication thread routine
HTTP request served going to disconnect the client
Set an Event object to indicate termination
Communicate with client to receive/serve its HTTP request Use recv() / send() blocking WinSock API calls
Gracefully shutdown and Close client socket
for(i=0; i<MAX_CLIENTS; ++i) { clientSockets[i] = INVALID_SOCKET;
hCommunicationThreads[i] = NULL; dwCommunicationThreads[i] = 0;
hEventsThreadTermination[i] = NULL; }
if(WSAStartup(MAKEWORD(1,1), &wsaData)) { MessageBox(NULL, "Error initialising sockets library.", "WinSock Error", MB_OK | MB_ICONSTOP); return 1; }
int WSAGetLastError(void);
DWORD GetLastError(VOID);
Get machine’s hostname and IP address
gethostname(hostName, sizeof(hostName)); ptrHostEnt = gethostbyname(hostName); Fill the socket address with appropriate values serverSocketAddress.sin_family = AF_INET; serverSocketAddress.sin_port = htons(SERVER_PORT);
memcpy(&serverSocketAddress.sin_addr.S_un.S_addr, ptrHostEnt->h_addr_list[0], sizeof(unsigned long));
Create the server socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(serverSocket == INVALID_SOCKET) { … … … WSACleanup(); return 1; }
Bind the socket
if(bind(serverSocket,(struct sockaddr *)&serverSocketAddress, sizeof(serverSocketAddress))) { … … … WSACleanup(); return 1; }
Put the socket in listening mode
if(listen(serverSocket, MAX_PENDING_CONNECTIONS)) { … … … SACleanup(); return 1; }
Here is the time to accept client connections
Limiting Maximum Concurrent connections
Create an unnamed semaphore object with MAX_CLIENTS as initial/maximum count
hSemaphoreMaxClients = CreateSemaphore(NULL, MAX_CLIENTS, MAX_CLIENTS, NULL );
We can accept more connections here because semaphore object is signaled clientSocket = accept(… … …);
clientSocket = accept(… … …);
Connection accepted! Look for the first empty slot to save the new socket descriptor
for(i=0; i<MAX_CLIENTS; i++) { if(clientSockets[i] == INVALID_SOCKET) break; }
nextClientIndex = i; clientSockets[nextClientIndex]=clientSocket;
nextClientIndex is used as an index in ALL arrays to store information relevant to this new client connection
clientSockets[nextClientIndex]=clientSocket;
hCommunicationThreads[nextClientIndex] = CreateThread(…, …, serveClient, //thread procedure (LPVOID)nextClientIndex, thread parameter CREATE_SUSPENDED, …);
Index for this client in all arrays is passed to this thread routine
DWORD WINAPI serveClient(LPVOID clientNumber) { char msg[2046] = "";
Receiving an HTTP request from browser recv( clientSockets[(UINT)clientNumber], msg,2046,0);
//nextClientIndex is used as an index in ALL arrays to store information relevant to this //new client connection
clientSockets[nextClientIndex]=clientSocket;
hCommunicationThreads[nextClientIndex] = CreateThread(…, …, serveClient, (LPVOID)nextClientIndex,thread parameter, CREATE_SUSPENDED,…);
Sample Request
Request parsing: understanding what the client has demanded GET /courses/win32.html HTTP/1. Assume F:\ is your server’s home directory, and \courses\is not a virtual directory, server should return the file F:\courses\win32.html
HTTP Redirection Redirecting the client irrespective of the HTTP request!
The string in the #define directive is assumed to be on a single line #define RESPONSE "HTTP/1.1 302 Object Moved\r\n Location: http://www.vu.edu.pk\r\n\r\n"
Sending the hard-coded HTTP response back to browser
send(clientSockets[(UINT)clientNumber], RESPONSE, sizeof(RESPONSE), 0);
Using Port Numbers
There is no compulsion to build all HTTP Web Servers to run on port 80. These are ‘suggested’ port numbers for a Win32 developer Standard servers do run on port 80. Our HTTP Web Server may also need to run on port 80 if put it to public use
Returning HTML Document
#define directive is assumed to be on a single line #define RESPONSE "HTTP/1.0 200 OK\r\n
Content-type: text/html\r\n Content-length: 1325\r\n\r\n" Send the hard-coded HTTP status and headers
send(clientSockets[(UINT)clientNumber], RESPONSE, sizeof(RESPONSE), 0); //Now sends the whole file using character I/O of standard C runtime ch = fgetc(fptr); while(!feof(fptr)) { send(clientSockets[(UINT)clientNumber], &ch, 1, 0); ch = fgetc(fptr);
CGI is Common Gateway Interface. Win32 executable execute by the server. All browser request data is available at stdin (read using scanf() etc.) and all output sent to stdout (output using printf etc.) is sent to the browser instead of the server screen.
In this lecture, we designed a web server which listens on port 80 and can receive requests from the clients and send message to the client. Our server supports maximum five clients at a time.
Note: For more on Windows Programming, connect to the Virtual University resource online. Examples, source codes can be found online.