Chenyi's notes

advertisement
ORF 401
Lab 5
Beyond HTTP
Chenyi Chen
Today
 Background
needed for doing the next
assignment (Lab 5):

Guide through the examples provided in the
website
http://orfe.princeton.edu/courses/orf401/handouts/
handout6.pdf
START EARLY!
Background


Everything we have done to-date uses HTTP (Hypertext
Transfer Protocol, one possible type of communication
protocol between client and server)
It works like this:
Client
Server
“Please Send me index.html”
Client
Server
Client
Server
<Twiddle Thumbs>
Drawbacks of HTTP
 Stateless
protocol: each request is treated as
an independent transaction that is unrelated
to any previous request.
 This means:


Forgets who the client was;
Have to re-initiate the transaction for each
“batch” that is sent back and forth.
 We
tried some work around: see PHP/mySQL,
cookies.

BUT: Consider a complex transaction state… (see
also the assignment for Lab5)
How to go beyond this?
want to establish a persistent connection
that allows data to be sent back and forth.
 For example, something like this:
 We
Client
Server
Client
Server
“Hey, my name is Jimmy and I am at
IP 32.45.1.21, can I be your friend?
Hit me up at port 6957.”
“If you’ve got the cash I’ve got the trash”
6957
Client
Server
<Send stuff back and forth all day until
the connection breaks somehow.>
Two Ways of Connecting

UDP (User Datagram Protocol)

Connectionless protocol (same as stateless protocol)
Just send the data fast, no hand-shaking
Good for: Streaming Video, Music, Games

Connection-oriented protocol (same as stateful protocol)



TCP (Transmission Control Protocol)


Send, wait for acknowledgement, send, etc.
Good for: File Transfer, Most things where you need 100% reliability
Source: http://www.skullbox.net/tcpudp.php
For your own information
 Difference
between stateful and stateless
protocols:
http://ironracer.hubpages.com/hub/Is-UDPStateful
 What is a PORT?
http://whatismyipaddress.com/port
UDP: Example


GOAL: implement a UDP transmission in your own
computer. Both sender and receiver are java
programs running in your machine.
Two files provided:



First of all, compile them:





Server side: vmReporter.java;
Client side: vmListener.java.
javac vmReporter.java
javac vmListener.java
To execute them, you need to find what your IP
address is! Google ‘my ip address’.
Choose a port number. Ex: 55555
Run simultaneously in two different terminals:


java vmReporter 128.112.224.122 55555
java vmListener 55555
TCP: Example


GOAL: implement a TCP transmission in your own computer.
Both sender and receiver are java programs running in your
machine.
Files provided:





Server side: LenseServer.java, LenseRequestHandler.java,
contacts.dat;
Client side: LenseBuyer.java.
First of all, compile them.
NOTE: In this case the IP address (same for server and client, of
course) is determined by the LenseBuyer program at runtime,
and the port number is fixed to 8190 by both programs.
Run simultaneously in two different terminals:


java LenseServer STORE (STORE can be any name, e.g., CVS,
Walmart, ORFE)
java LenseBuyer "New Vues",
where, instead of "New Vues", you can use any name of a contact
lens brand stored in the file contacts.dat (e.g., "New Vues", Acuvue).
Real Task
 Assume
there is a big autonomous taxi
(aTaxi) station, we need to build a system
to get gate information (assigned
according to destination) from the
fictitious aTaxi service providers,
CarmonCar and VandeRides, then
display on a screen.
 Not
as easy as previous assignments!
Give it plenty of time please!
IP address: 128.112.224.129
e.g. 51000
e.g. 51001
IP address: 128.112.226.14
Your Computer
CarmonCar aTaxi Company
TCP:
• Register/de-Register yourself
TCPSocket #1
TCPSocket #2
UDP:
• Listen on a socket and receive
data from each of the servers.
UDPSocket #1
UDPSocket #2
VandeRides aTaxi Company
The Above in Words
 You
are going to use TCP to send and
receive registration information to the
servers.
 You
setup UDP sockets on your computer
to receive data directly from the servers.
 The
TCP and UDP connections are
independent of one another.
Which means that you should..
 Setup
your own UDP sockets to receive data
from the servers.
 Use TCP to tell the server your own IP address
and the UDP ports it should connect to.
 Collect and process the UDP data from the
sockets, display it on the board.
 De-register with TCP when you are ready to.
Sockets and Data Transfer

Java has lots of built in functions and objects for
handling TCP and UDP, you just need to create the
right ones and know what to pass to send and receive
data. One example:
Type
Socket Constructor
Sends
Receives
TCP
Socket(address, port)
String
println()
String
readLine()
UDP
DataGramSocket (receive_port)
DataGramPacket
send()
DataGramPacket
receive()
myTCPSocket = new Socket(serverAddress, TCPport);
out = new PrintStream(myTCPSocket.getOutputStream(), true);
out.println(“XXXXXX”);
in = new DataInputStream(myTCPSocket.getInputStream())
reply = in.readLine();

Refer to the TCP/UDP examples for details on how to
use those Java classes and functions
Setting up your Java
Environment

Build on your own PC, not need to use cPanel.

You need the following files:






DisposableFrame.java (provided)
Logo.java (provided)
GateKeeper.java (framework provided)
GateListener.java (framework provided)
GateInformationBoard.java (framework provided)
Be sure to import the required Java libraries:
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
Java Files to Create

GateKeeper



GateInformationBoard





Get IP Addresses of Your machine and the Servers
Setup the Object for GateInformationBoard
Draw the Windows
Setup the Object for GateListener
Setup TCP Connection to Negotiate with the Server
Display the Information that is Received
GateListener


Listen for UDP input from the Servers
Process the UDP input for Display
GateKeeper.java

Determine your own IP address.
String myAddress = InetAddress.getLocalHost().getHostAddress();
make sure it returns the real IP, otherwise, you need to type in your IP manually

Know the IP Addresses of the Servers.
CarmonCar: String ipAddressc = "128.112.226.14";
VandeRides: String ipAddressv = "128.112.224.129";

Create GateInformationBoard objects
GateInformationBoard boardv = new GateInformationBoard(….)
GateInformationBoard boardc = new GateInformationBoard(….)

Start the objects
boardc.StartUp();
GateInformationBoard.java
 Create
the UDP Listeners with GateListener
 Establish the TCP connection:

Register yourself with the server at startup
 Tell
it your IP
 Tell it where your UDP port is

Deregister yourself on close
 Close
the TCP connection and clean-up
Specific Tips: Server Messaging
(in TCP connection)
 You
communicate with the server through a few
key messages…

ADD [your IP] [your UPD port]
 You

should receive HELLO [your IP] [your UPD port]
DROP [your IP] [your UPD port]
 You
should receive BYE [your IP][your UPD port]
e.g. ADD 128.112.224.122 51000
GateListener.Java
 Open
a UDP port to receive the data.
 Receive the data.
 Process the data.
 Display the data on the GateInformationBoard
GateListener.Java
 aTaxi
message format:
“company,destination,SEAT,number,GATE,number”
 company:
VandeRides or CarmonCar
 destination: name of a NJ town
 SEAT and GATE are fixed key words followed by
the corresponding numbers
e.g. VandeRides,Princeton,SEAT,4,GATE,12
Specific Tips: Ports/IPs
 Make
sure you use your own IP (actual one,
not 127.0.0.1).
 Try switch to a cable Ethernet if the wireless
does not work.
 Remember to use different incoming ports for
the different servers (in UDP listening).
e.g. 51000 and 51001
Download