CSE331�Lecture 19.1 - 19.2
HTTP Servers Basics
Ardi Madadi �Summer 2021�(Based on slides by Kevin Zatloukal, Mike Ernst, Dan Grossman, and many others)
1
Event-driven programming
An event-driven program is designed to wait for events:
do {
e = getNextEvent();
process event e;
} while (e != quit);
CSE 331 Summer 2021
Server Programming
while (true) {
wait for a client to connect
process the request; send a response back
}
CSE 331 Summer 2021
Example: Chat Server
ChatServer.java
CSE 331 Summer 2021
Server Sockets & Ports
ServerSocket ssock = new ServerSocket(80);
Socket sock = new Socket(“attu”, 80);
CSE 331 Summer 2021
Ports & Protocols
CSE 331 Summer 2021
Protocols
CSE 331 Summer 2021
HTTP
8
HTTP Request 1
GET /index.html HTTP/1.1
Name: Value
CSE 331 Summer 2021
HTTP Response 1
HTTP/1.1 200 OK
content-length: 124
content-type: text/html; charset=UTF-8
Date: Wed, 27 May 2020 18:30:00 GMT
Connection: close
<html>
…
CSE 331 Summer 2021
Demo
(command-line HTTP request)
CSE 331 Summer 2021
HTTP Request 2
POST /register HTTP/1.1
content-type: application/x-www-form-urlencoded
content-length: 25
fname=Kevin&userid=kevinz
CSE 331 Summer 2021
HTTP
CSE 331 Summer 2021
Uniform Resource Locators (URLs)
http://attu:8080/index.html
Connect to server attu on port 8080
Send GET request
GET /index.html HTTP/1.1
…
CSE 331 Summer 2021
Uniform Resource Locators (URLs)
CSE 331 Summer 2021
http://attu:8080/cse331/test?a=b&c=d#whatever
protocol
hostname
port
path
query string
fragment