30.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
COEN 445 Communication Networks and Protocols Lab 4
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
ISP – 9 th Recitation Socket programming – Client side.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
CS3212 計算機網路概論 Winsock Tutorial
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.
Assignment 3 A Client/Server Application: Chatroom.
LWIP TCP/IP Stack 김백규.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
LECTURE 10 Networking. NETWORKING IN PYTHON Many Python applications include networking – the ability to communicate between multiple machines. We are.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
CLIENT (Browser) socket accept C1 C2 recv C2 recv send C2 send end_thread recv C3 send bind connect Web Server Proxy recv close C3 close C2 end_thread.
SOCKET PROGRAMMING Presented By : Divya Sharma.
1 Issues in Client/Server Refs: Chapter 27 Case Studies RFCs.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Tiny http client and server
Assignment 3 A Client/Server Application: Chatroom
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Operating Systems Review ENCE 360.
WWW and HTTP King Fahd University of Petroleum & Minerals
HTTP – An overview.
Sockets and Beginning Network Programming
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Hypertext Transport Protocol
CS 3305 System Calls Lecture 7.
Week 13 - Friday CS222.
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
The Internet and HTTP and DNS Examples
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
HTTP Hypertext Transfer Protocol
28.
Network Programming Chapter 12
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Issues in Client/Server Programming
.Net Sockets.
Lecture 5: Functions and Parameters
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Server-side Programming CSE 333 Winter 2019
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Project 1 Professor Breecher
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Presentation transcript:

30

Review of Last Lecture RFCs HTTP protocol MIME types Server Architecture Threads

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

HTTP Web Server Application Initialise Windows Sockets Library if(WSAStartup(MAKEWORD(1,1), &wsaData)) { … … … return 1; }

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));

HTTP Web Server Application 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)))

HTTP Web Server Application Put the socket in listening mode if(listen(serverSocket, MAX_PENDING_CONNECTIONS)) { … … … WSACleanup(); return 1; } Here is the time to accept client connections

HTTP Web Server Application Create a thread that will call accept() in a loop to accept multiple client connections hAcceptingThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) acceptClientConnections, CREATE_SUSPENDED, &dwAcceptingThread);

HTTP Web Server Application Create a thread to do termination house-keeping when some communication thread terminates hTerminatingThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) terminateCommunicationThreads, CREATE_SUSPENDED, &dwTerminatingTThread);

acceptClientConnections thread routine Client Socket Descriptor accept(); Create a new communication thread: serveClient The newly generated “Communication Thread” (discussed later)

terminateCommunicationThreads thread routine Wait for some thread termination event Wait for thread object go signalled Close thread handle; Destroy its relevant stored data;

Application Variables #define MAX_CLIENTS 5 SOCKET clientSockets[MAX_CLIENTS]; HANDLE hCommunicationThreads[MAX_CLIENTS]; DWORD dwCommunicationThreads[MAX_CLIENTS]; HANDLE hAcceptingThread; DWORD dwAcceptingThread; HANDLE hTerminatingThread; DWORD dwTerminatingTThread;

acceptClientConnections thread routine Wait on semaphore until its count > 0 accept(); Client Socket Descriptor The newly generated “Communication Thread” (discussed later) Create a new communication thread: serveClient serveClient

terminateCommunicationThreads thread routine Wait for some thread termination event Close thread handle; ReleaseSemaphore(); Wait for thread object go signalled

serveClient Communication Thread routine Communicate with client to receive/serve its HTTP request Use recv() / send() blocking WinSock API calls HTTP request served going to disconnect the client Set an Event object to indicate termination Gracefully shutdown and Close client socket

Application Variables #define MAX_CLIENTS 5 HANDLE hEventsThreadTermination[MAX_CLIENTS];

terminateCommunicationThreads thread routine Wait for ANY thread termination event WaitForMultipleObjects(…, hEventsThreadTermination,…); At least one thread sets its termination event Wait for thread routine to finish (its object will get signalled) WaitForSingleObject(hCommunicationThreads[i], …); The thread function has actually finished Close thread handle; Make it NULL; Set its socket to invalid ReleaseSemaphore();

Thread Procedures Summary acceptClientConnections - to accept client connection terminateCommunicationThreads - to do housekeeping when communication threads terminate serveClient - to do actual communication to receive and serve an HTTP request

User Interface

Variable Initialisation for(i=0; i<MAX_CLIENTS; ++i) { clientSockets[i] = INVALID_SOCKET; hCommunicationThreads[i] = NULL; dwCommunicationThreads[i] = 0; hEventsThreadTermination[i] = NULL; }

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

- get error code for the last unsuccessful Windows Sockets operation 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

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));

HTTP Web Server Application 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)))

HTTP Web Server Application Put the socket in listening mode if(listen(serverSocket, MAX_PENDING_CONNECTIONS)) { … … … WSACleanup(); 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, NULL);

“I am dying…”, the thread said create an array of non-signalled event objects for(i=0; i<MAX_CLIENTS; i++) hEventsThreadTermination[i] = CreateEvent(NULL, FALSE, FALSE, NULL);

HTTP Web Server Application Create the connection-accepting thread hAcceptingThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) acceptClientConnections, CREATE_SUSPENDED, &dwAcceptingThread); Create the termination house-keeping thread hTerminatingThread = CreateThread(… … …); Display the dialog DialogBox(…, …, …, mainDialogProc);

mainDialogProc dialog procedure Resume the threads case WM_INITDIALOG: ResumeThread(hAcceptingThread); ResumeThread(hTerminatingThread); return TRUE; break; Handling the server shut-down button case IDC_BUTTON_SHUTDOWN: Perform any shut-down tasks that my be necessary EndDialog(hDlg, 0);

acceptClientConnections thread routine Start of the loop to accept client connections Wait for semaphore count to go non-zero dwWaitResult = WaitForSingleObject( hSemaphoreMaxClients, INFINITE); switch(dwWaitResult) { case WAIT_OBJECT_0: We can accept more connections here because semaphore object is signalled clientSocket = accept(… … …);

acceptClientConnections thread routine 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;

acceptClientConnections thread routine 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, …);

acceptClientConnections thread routine Wait on semaphore until its count > 0 accept(); Client Socket Descriptor The newly generated “Communication Thread” (discussed later) Create a new communication thread: serveClient serveClient

serveClient thread routine 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 ); Available HTTP request data will be received in this buffer

acceptClientConnections thread routine 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.0 Assume F:\ is your server’s home directory, and \courses\is not a virtual directory, server should return the file F:\courses\win32.html

Server returns the requested file Try to open the file F:\courses\win32.html using fopen() etc. Return error status to the browser HTTP 404 Not Found etc. File could not be opened Return status line: HTTP 200 OK Return other headers, e.g. Content-Length: file-size in bytes Read whole of the file using fread() etc. and send it to the browser Disconnect HTTP client (i.e. the broswser)

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); Status line

Sending a request to our Web Server

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 an HTML document The string in the #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 send the whole file using character I/O of standard C runtime ch = fgetc(fptr); while(!feof(fptr)) { send(clientSockets[(UINT)clientNumber], &ch, 1, 0); } File a.htm is 1325 bytes long

A Flawed Web Server Fixed sized arrays waste memory and lack run-time flexibility One event per thread to signify termination: WaitForMultipleObjects() can not wait on more than a certain number of objects e.g. 64 on x86 under NT.

Dynamic Web Content Server blindly dumps HTML files to the clients. This is ‘static content’. If server reads the file and modifies its output e.g. %%time%% replaced with current system time Every 2 clients connected at different instants of time will receive different content. This is ‘dynamic content’. %%time%% may be called a tag

Dynamic Web Content Microsoft Active Server Pages Macromedia ColdFusion Tags are not sent to the client. These are processed by the server and the resulting output is sent to the browser.

CGI Common Gateway Interface Win32 EXE executable is executed at the server All browser request data is available at stdin (read using scanf() etc.) All output sent to stdout (output using printf etc.) is sent to the browser instead of the server screen.