CSCI2020U - Week 7 - Network Programming - Sockets, TCP, UDP
CSCI2020U - Week 7 - Network Programming - Sockets, TCP, UDP
Hrim Mehta
Acknowledgment: Some of the content in this course is derived from materials created by Mariana
Shimabukuro
Outline
● TCP
○ Ports
○ Handshake
○ Windowing
○ Checksum
○ Packet Format
● UDP
○ Packet Format
● Socket Programming
○ Stream Sockets
○ Datagram Sockets
Network Communication Protocols
TCP/IP Model
Internet Protocol (IP)
● Addressing
● Routing
IP
● IPv4
○ 4 bytes (32 bits)
○ Written in decimal, separated by dots
○ e.g. 192.197.54.136
● IPv6
○ 8 double bytes (128 bits)
○ Written in hexadecimal, separated by colons
○ e.g. FE80:0000:0000:0000:0000:0000:AC1E:43FE
○ Short form: FE80::AC1E:43FE
Transmission Control Protocol
Transmission Control Protocol (TCP)
Adding reliability to packet delivery
Transmission Control Protocol (TCP)
● Reliable
○ Streams
○ Connections
○ Sequence numbers
○ Acknowledgements (ACKs)
○ Error checking (Checksums)
Streams
● In most networks, data is sent in finite quanta, called packets
● Output streams are implemented with packets in a similar way to saving data
to files via blocks:
○ Data to be sent is collected in a buffer
○ When enough data to fill an entire packet is collected, a packet is transmitted
● Input streams are implemented with packets in a similar way to reading data
from files via blocks:
○ When you want to read a single character/byte, the buffer is checked
○ As packets arrive, their data is added to the buffer
TCP Packet Format
● Every TCP packet has a header (meta-data):
1. Flags
2. Sequence Number
3. Source and Destination Port Number
Flags
● Single-bit binary values:
1. SYN - attempting to establish a connection
2. FIN - attempting to tear down a connection
3. ACK - this packet contains an acknowledgement
4. RST - attempting to reset the connection
Connection Handshake
● Before communication can take place, a connection is established
● This is a three-step process:
1. Client sends a connection request (SYN)
2. Server acknowledges connection request (SYN+ACK)
3. Client acknowledges receipt of server's response (ACK)
Disconnection Handshake
● To disconnect, another three-way handshake occurs:
1. Client sends a connection finished request (FIN)
2. Server acknowledges connection finished request (FIN+ACK)
3. Client acknowledges receipt of server's response (ACK)
Sequence Numbers
● Each packet has a randomized, hard-to-predict sequence number
● This sequence number is used when acknowledging a packet
Acknowledgements
● Each packet is acknowledged (by its sequence number) by sending a
message back to the sender
● Without any improvements:
Acknowledgements
● Improvements:
○ Piggybacked acknowledgements
■ Acknowledgements are sent in messages that were already going to be sent back in
response
○ Sliding window
■ Up to N unacknowledged packets can be out at once:
Port Numbers
● A destination port number serves a similar purpose as an IP address
○ IP address
■ Which computer on the network should receive this packet?
○ Destination port
■ Which application on that computer should receive this packet?
● A source port number is to give the receiving computer a way to contact the
source application with its next message
Well-Known Port Numbers
● There are many standard port numbers:
○ 22: SSH
○ 25: Sending E-Mail
○ 53: DNS
○ 80: Unencrypted HTTP
○ 110/143: Receiving E-Mail
○ 443: Encrypted HTTP (HTTPS)
User Datagram Protocol
User Datagram Protocol (UDP)
Transport without the reliability baggage
UDP
● Pros:
○ No acknowledgements
○ No connection set up/tear down
○ Supports multicast/broadcast
● Cons:
○ Lost packets are not identified and resent
○ Packets may arrive out of order
○ Does not implement streams
UDP Packet Format
● UDP packets have no flags, acknowledgements, sequence numbers:
TCP Socket Programming
TCP Sockets in Java
Listens for incoming connections (server, passive open):
System.out.println(url);
Ref_Date,GEO,VAR,SEX,AGE,STATS,CHAR,Vector,Coordinate,Value
2009,Canada,Body Mass Index (BMI) - Center for Disease Control (CDC) classification,Both sexes,Ages 3 to
17,Underweight,Estimate,v88847602,1.1.1.1.1.1,..
2011,Canada,Body Mass Index (BMI) - Center for Disease Control (CDC) classification,Both sexes,Ages 3 to
17,Underweight,Estimate,v88847602,1.1.1.1.1.1,4.5
2013,Canada,Body Mass Index (BMI) - Center for Disease Control (CDC) classification,Both sexes,Ages 3 to
17,Underweight,Estimate,v88847602,1.1.1.1.1.1,5.0
...
CSV
/*{"bpi":{
...
"CAD":{
"code":"CAD",
"rate":"553.1846",
"description":"Canadian Dollar",
"rate_float":553.1846}
}
}*/
JSONObject bpi = (JSONObject)obj.get("bpi");
JSONObject cad = (JSONObject)bpi.get("CAD");
double rate = (Double)cad.get("rate_float");
XML / HTML DOM
<stations lastUpdate="1452772137445" version="2.0">
<station>
<id>1</id>
<name>Jarvis St / Carlton St</name>
<terminalName>7055</terminalName>
<lastCommWithServer>1452771534819</lastCommWithServer>
<lat>43.66207</lat>
<long>-79.37617</long>
<installed>true</installed>
<locked>false</locked>
...
</station>
</stations>
XML / HTML DOM
InputStream inStream = conn.getInputStream();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
Document document = docBuilder.parse(inStream);
document.getDocumentElement().normalize();