cse524: lecture 3 network programming physical layer
TRANSCRIPT
CSE524: Lecture 3
Network programming
Physical layer
Administrative
● CSE524 e-mail list created– E-mail TA if you have not received the introductory
message
● Homework assignments– http://www.cse.ogi.edu/class/cse524/
– Homework #1 due Monday, Oct. 13th
– Programming homework coming soon
● Access to machine resources– Get yourself an account
– Use class laboratory
Where we're at....
● Internet architecture and history● Internet in a nutshell
– Packet trace example of protocols in action
– Programming Internet applications (this class)
● Physical, Data-link, Network, Transport, Application layers (rest of the class)
Network programming
● How do I program all of this?– Layering allows programmers to write programs without
understanding underlying functions of TCP/IP
– You will be evaluating this for yourselves...
– API to transport layer● BSD socket API● java.net API
Network programming
● How can applications programmatically access TCP/UDP/IP?– Many possible interfaces– Too many to teach adequately in detail– Socket APIs
● BSD socket API● Java socket API
– Other APIs● Client-side Java URLconnections● Server-side Java servlets● RPC, CORBA, Java RMI (not covered)
Socket programming
Socket API● introduced in BSD4.1 UNIX, 1981● client/server paradigm ● socket API supports three main usages
– unreliable datagram (UDP)– reliable, byte stream (TCP)– raw IP datagrams (IP)
a host-local, application-created/owned,
OS-controlled interface (a “door”) into which
application process can both send and
receive messages to/from another (remote or
local) application process
socket
Socket-programming using TCP
Socket: a door between application process and end-end-transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one process to another
process
TCP withbuffers,
variables
socket
controlled byapplicationdeveloper
controlled byoperating
system
host orserver
process
TCP withbuffers,
variables
socket
controlled byapplicationdeveloper
controlled byoperatingsystem
host orserver
internet
Socket programming with TCP
Client must contact server● server process must first be
running● server must have created a
“listening” socket that welcomes client’s initial contact
Client contacts server by:● creating client-local TCP
socket● specifying server IP address
and port number of server process
● When client creates socket: client TCP establishes connection to server TCP
● When contacted by client, server TCP creates new socket for server process to communicate with client
– allows server to talk with multiple clients (using multiple sockets)
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
application viewpoint
Sockets in action
*,SIP:80
CIP:1099,SIP:80
*,SIP:80
CIP:1099,SIP:80
CIP:1099,SIP:80
CIP:1100,SIP:80
*,SIP:80
CIP:1099,SIP:80
CIP:1099,SIP:80
CIP:1100,SIP:80
CIP:1100,SIP:80
BSD sockets in C/Unix
● Socket API (socket.h)● socket(): create unnamed socket (data structure)
– UDP (SOCK_DGRAM), TCP (SOCK_STREAM)– IP (SOCK_RAW)
● bind(): name socket (bind local address to socket)● listen(): enable socket to accept connections● accept(): get connect() request from listen queue, allocate file descriptor for
new socket● connect(): initiate connection on a socket (TCP handshake)● send(), sendto(), sendmsg(), writev(), write(): send data● recv(), recvfrom(), recvmsg(), readv(), read(): receive data● setsockopt(), getsockopt(): set socket options (such as buffer sizes, flag
fields)● close(), shutdown(): teardown connection
BSD sockets in action
serverclient
bind()
socket() socket()
recvfrom()
sendto()
bind()
recvfrom()
UDP example TCP example
sendto()
connect()
socket()
socket()
bind()
serverclient
listen()
accept()
write()
read()
write()
read()
BSD example
● For reference only...– http://www.cse.ogi.edu/class/cse524/socket_example
Java network programming
Java network applications
java.net package
System-dependent implementations
Java installation on church/state
● J2SE– javac
● java compiler– java
● java interpreter
• http://www.ibiblio.org/javafaq/javatutorial.html
java.net classes● Low-level networking classes (Sockets and Packets)● High-level URL networking classes
• http://java.sun.com/j2se/1.3/docs/api/index.html● java.lang.Object
– java.net.Socket– java.net.ServerSocket– java.net.DatagramSocket
● java.net.MulticastSocket– java.net.DatagramPacket– java.net.URL– java.net.URLConnection
● java.net.HttpURLConnection– java.net.URLencoder– java.net.InetAddress
java.net.Socket● Constructors
– Socket(InetAddress, int) – Socket(String, int)– Socket(InetAddress, int, InetAddress, int)
● Some methods– getInputStream()– getOutputStream– getInetAddress()– getPort()– getLocalAddress()– getLocalPort()– get/set individual socket options
java.net.ServerSocket● Constructors
– ServerSocket(int) – ServerSocket(int, int) // backlog specified– ServerSocket(int, int, InetAddress) // local address and backlog specified
● Some methods– accept()– getInetAddress()– getLocalPort()
Socket programming with TCP
Example client-server app:● client reads line from standard input
(inFromUser stream) , sends to server via socket (outToServer stream)
● server reads line from socket● server converts line to uppercase,
sends back to client● client reads, prints modified line from
socket (inFromServer stream)
Input stream: sequence of bytes into process
Output stream: sequence of bytes out of process
client socket
inFromUser outToServer
iinFromServer
Java client/server socket interaction: TCP
wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()
create socket,port=x, forincoming request:welcomeSocket =
ServerSocket()
create socket,connect to hostid, port=xclientSocket =
Socket()
closeconnectionSocket
read reply fromclientSocket
closeclientSocket
Server (running on hostid) Client
send request usingclientSocketread request from
connectionSocket
write reply toconnectionSocket
TCP connection setup
Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient {
public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Createinput stream
Create client socket,
connect to server
Createoutput stream
attached to socket
Example: Java client (TCP), cont.
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close(); } }
Createinput stream
attached to socket
Send lineto server
Read linefrom server
Example: Java server (TCP)
import java.io.*; import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
Createwelcoming socket
at port 6789
Wait, on welcomingsocket for contact
by client
Create inputstream, attached
to socket
Example: Java server (TCP), cont
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence); } } }
Read in linefrom socket
Create outputstream, attached
to socket
Write out lineto socket
End of while loop,loop back and wait foranother client connection
Socket programming with UDP
UDP: no “connection” between client and server
● no handshaking● sender explicitly attaches IP
address and port of destination● server must extract IP address,
port of sender from received datagram
UDP: transmitted data may be received out of order, or lost
application viewpoint
UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server
java.net.DatagramSocket● Constructors
– DatagramSocket() – DatagramSocket(int) // bind to specific port– DatagramSocket(int, InetAddress) // specify local address
● Some methods– getLocalAddress()– getLocalPort()– receive(DatagramPacket)– send(DatagramPacket)– get/set individual socket options
java.net.DatagramPacket● Constructors
– DatagramPacket(byte[], int) // receiving packets – DatagramPacket(byte[], int, InetAddress, int) // sending packets
● Some methods– getAddress() // remote address– getPort() // remote port– getLength() // get packet length– getData() // return data received or to be sent– setAddress(InetAddress) // set remote address– setData(byte[]) // set packet data– setLength(int) // set packet length– setPort(int) // set remote port
Client/server socket interaction: UDP
Server (running on hostid)
create socket,clientSocket = DatagramSocket()
Client
Create, address (hostid, port=x,send datagram request using clientSocket
closeclientSocket
read reply fromclientSocket
create socket,port=x, forincoming request:serverSocket = DatagramSocket()
read request fromserverSocket
write reply toserverSocketspecifying clienthost address,port umber
Example: Java client (UDP)
import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Createinput stream
Create client socket
Translate hostname to IP
address using DNS
Example: Java client (UDP), cont.
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }
}
Create datagram with data-to-send,
length, IP addr, port
Send datagramto server
Read datagramfrom server
Example: Java server (UDP)
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Createdatagram socket
at port 9876
Create space forreceived datagram
Receivedatagram
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } }
}
Get IP addrport #, of
sender
Write out datagramto socket
End of while loop,loop back and wait foranother datagram
Create datagramto send to client
High-level Java networking classes
● Socket/Packet– Low level building blocks– Must implement all application-level logic
● Many protocols based on URLs and/or tunneled in HTTP– Program at a higher-level to hide underlying protocol
details– Do not re-implement HTTP, URL parsing, MIME
handling for each application
High-level client-side Java networking classes
● java.net.URL– Represent a URL object
● java.net.URLConnection– Represent a connection to a URL which can be read and
written from● java.net.HttpURLConnection
– Subclass of URLConnection for http:// URLs● Example
– http://www.cse.ogi.edu/class/cse524/java_example
High-level server-side Java networking classes
● Servlets– Dynamically generate content– Implement common protocol header logic
● Example http servlets– Cookies– Content-type– Content-length
● Servlet classes– javax.servlet.Servlet javax.servlet.HttpServlet
● init()● service()● destroy()
– javax.servlet.ServletRequest javax.servlet.ServletResponse– javax.servlet.HttpServletRequest javax.servlet.HttpServletResponse
The rest of the course
● From birds-eye view, we will now focus on specific components
● Review these lectures for perspective when looking at the components
● Mostly classical material with some references to newer technologies
Physical Layer
● Plethora of physical media– Fiber, copper, air– Specifies the characteristics of transmission media– Too many to cover in detail, not the focus of the course– Many data-link layer protocols (i.e. Ethernet, Token-Ring,
FDDI. ATM run across multiple physical layers)– Physical characteristics dictate suitability of data-link layer
protocol and bandwidth limits
PL: Good URLs
● Get ‘em while they last….– ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/answers/LANs/cabling-
faq– http://fcit.coedu.usf.edu/network/
PL: Common Cabling
● Copper– Twisted Pair
● Unshielded (UTP)– CAT-1, CAT-2, CAT-3, CAT-4, CAT-5, CAT-5e
● Shielded (STP)
– Coaxial Cable● Fiber
– Single-mode– Multi-mode
PL: Twisted Pair
● Most common LAN interconnection● Multiple pairs of twisted wires● Twisting to eliminate interference
– More twisting = Higher data rates, higher cost
● Standards specify twisting, resistance, and maximum cable length for use with particular data-link layer
PL: Twisted pair
● 5 categories– Category 1
● Voice only (telephone wire)
– Category 2● Data to 4Mbs (LocalTalk)
– Category 3● Data to 10Mbs (Ethernet)
– Category 4● Data to 20Mbs (16Mbs Token Ring)
– Category 5 (100 MHz)● Data to 100Mbs (Fast Ethernet)
– Category 5e (350 MHz)● Data to 1000Mbs (Gigabit Ethernet)
PL: Twisted Pair
● Common connectors for Twisted Pair– RJ11 (6 pairs)– RJ45 (8 pairs)
● Allows both data and phone connections● (1,2) and (3,6) for data, (4,5) for voice● Crossover cables for NIC-NIC, Hub-Hub connection
(Data pairs swapped)
PL: UTP
● Unshielded Twisted Pair– Limited amount of protection from interference– Commonly used for voice and ethernet
● Voice: multipair 100-ohm UTP
PL: STP
● Shielded Twisted Pair– Not as common at UTP– UTP susceptible to radio and electrical interference– Extra shielding material added– Cables heavier, bulkier, and more costly– Often used in token ring topologies
● 150 ohm STP two pair (IEEE 802.5 Token Ring)
PL: Coaxial cable
● Single copper conductor at center● Plastic insulation layer● Highly resistant to interference
– Braided metal shield – Support longer connectivity distances over UTP
PL: Coaxial cable
● Thick (10Base5) – Large diameter 50-ohm cable– N connectors
● Thin (10Base2) cables– Small diameter 50-ohm cable– BNC, RJ-58 connector
● Video cable– 75-ohm cable– BNC, RJ-59 connector– Not compatible with RJ-58
PL: Fiber
● Center core made of glass or plastic fiber● Transmit light versus electronic signals
– Protects from electronic interference, moisture● Plastic coating to cushion core● Kevlar fiber for strength● Teflon or PVC outer insulating jacket
PL: Fiber● Single-mode fiber
– Smaller diameter (12.5 microns)– One mode only– Preserves signal better over longer distances– Typically used for SONET or SDH– Lasers used to signal– More expensive
● Multi-mode fiber– Larger diameter (62.5 microns)– Multiple modes– LEDs used to signal– WDM and DWDM
● Photodiodes at receivers
PL: Fiber connectors
● ESCON
● Duplex SC
● ST
● MT-RJ (multimode)
● Duplex LC
PL: Wireless
● Entire spectrum of transmission frequency ranges– Radio– Infrared– Lasers– Cellular telephone – Microwave – Satellite– Acoustic (see ESE sensors)– Ultra-wide band
● http://www.ntia.doc.gov/osmhome/allochrt.html
PL: What runs on them?
Protocol Summary
Protocol Cable Speed Topology
Ethernet Twisted Pair, Coaxial, Fiber 10 Mbps Linear Bus, Star, Tree
Fast Ethernet Twisted Pair, Fiber 100 Mbps Star
LocalTalk Twisted Pair .23 Mbps Linear Bus or Star
Token Ring Twisted Pair 4 Mbps - 16 Mbps Star-Wired Ring
FDDI Fiber 100 Mbps Dual ring
ATM Twisted Pair, Fiber 155-2488 Mbps Linear Bus, Star, Tree
PL: Physical-link lingo
● Specifies capacities over physical media● Electronic
– T1/DS1=1.54 Mbps – T3/DS3=45Mbps
● Optical (OC=optical carrier)– OC1=52 Mbps– OC3/STM1=156 Mbps– OC12=622 Mbps– OC48=2488 Mbps– OC192=10 Gbps – OC768=40 Gbps