Network Programming - Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

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

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 30
30.1 SERVER ARCHITECTURE 2
30.2 HTTP WEB SERVER APPLICATION 2
30.3 VARIABLE INITIALIZATION 7
30.4 INITIALIZE WINSOCK LIBRARY 7
30.5 WIN32 ERROR CODES 7
30.6 HTTP WEB SERVER APPLICATION 7
SUMMARY 13
EXERCISES 13
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Network Programming - Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

  • Chapter
  • 30.1 S ERVER ARCHITECTURE
  • 30.2 HTTP W EB S ERVER APPLICATION
  • 30.3 VARIABLE I NITIALIZATION
  • 30.4 INITIALIZE W INS OCK LIBRARY
  • 30.5 WIN 32 ERROR CODES
  • 30.6 HTTP W EB S ERVER APPLICATION
  • S UMMARY
  • EXERCISES

30.1 Server Architecture

Server architecture will be based on:

  • Dialog-based GUI application
  • Most of the processing is at back-end
  • Running on TCP port 5432 decimal

30.2 HTTP Web Server Application

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)

accept()

Create a new communication thread: serveClient

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

30.3 Variable Initialization

for(i=0; i<MAX_CLIENTS; ++i) { clientSockets[i] = INVALID_SOCKET;

hCommunicationThreads[i] = NULL; dwCommunicationThreads[i] = 0;

hEventsThreadTermination[i] = NULL; }

30.4 Initialize WinSock Library

if(WSAStartup(MAKEWORD(1,1), &wsaData)) { MessageBox(NULL, "Error initialising sockets library.", "WinSock Error", MB_OK | MB_ICONSTOP); return 1; }

30.5 Win32 Error Codes

int WSAGetLastError(void);

  • get error code for the last unsuccessful Windows Sockets operation

DWORD GetLastError(VOID);

  • retrieve calling threads last-error code

30.6 HTTP Web Server Application

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

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.

Summary

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.

Exercises

  1. Practise to create such applications as explained in this lecture and in previous lectures with different ideas.