Download these notes (in PPT format).

Document related concepts

AppleTalk wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Wireless security wikipedia , lookup

Asynchronous Transfer Mode wikipedia , lookup

TCP congestion control wikipedia , lookup

Distributed firewall wikipedia , lookup

Net bias wikipedia , lookup

CAN bus wikipedia , lookup

Wake-on-LAN wikipedia , lookup

Deep packet inspection wikipedia , lookup

Computer network wikipedia , lookup

Lag wikipedia , lookup

Network tap wikipedia , lookup

List of wireless community networks by region wikipedia , lookup

Piggybacking (Internet access) wikipedia , lookup

Airborne Networking wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Internet protocol suite wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Real-Time Messaging Protocol wikipedia , lookup

Cracking of wireless networks wikipedia , lookup

IEEE 1355 wikipedia , lookup

UniPro protocol stack wikipedia , lookup

Transcript
Communications
Most modern control involves interactions between many microprocessors
Examples:
PLC:
Programming done on PC; uploaded to PLC
Complex PLC control requires interaction between many PLC’s
e.g. automation controls in an MTR station
Microprocessor:
Phone   Phone (e.g. to exchange phone numbers)
Phone   PC (to upload address book)
Networked video games with multiple players
Computers:
Internet, EDI
Communications
For devices to exchange data, several things must work together:
Assume: Data  controlled change of voltage in a wire.
Bit-Streams:
message = “Wow”
Example:
Wow
ASCII
1010 111
1101 111
1110 111
V
mark
1 1 1
0
time
1 stop bits
1
0
1 1 1
1
1
0
start
bit
W: least significant bit  most significant bit
1
1 1
1
1 1 1 idle
0
Communications
For devices to exchange data, several things must work together:
Wow
1010 111
mark
1 1 1
1 stop bits
1
0
0
1101 111
1 1 1
1110 111
1
1
0
1
1 1
1
1 1 1 idle
0
start
bit
- A wire must connect the devices
- The voltage levels must match:
sender sets “voltage of 5V”  ‘1’
=> receiver must have a 5V sensor; (must interpret same way)
- The duration for each bit (communication frequency, baud rate)
- Start bit(s), stop bit(s)
- Both must be using ASCII code
…
Asynchronous vs Synchronous communications
Why do we need start/stop bits ?
Prepares the receiving device to start recording data
 communication is ASYNCHRONOUS
Idle
Start
bit
seven
data
asynchronous 7-bit character + parity bit
bits
Parity
Stop bits
bit
LSB
Idle/next caracter
Coordinated connection between the devices: SYNCHRONOUS communication
- Receiver continually hunts for sync character (in ASCII: 1001 0110)
- [Copies data into the data register; char available flag ON; data read] x CT
- Check against CT;
- end sync bit  hunt mode.
synchronous block of data
SYNCSYNC
DATA
ETX CT SYNC
Synchronization of communication
data
(a) Transmitter generates
the synchronization reference
Transmitter
and
Synchronization
generator
Receiver
synchro
data
(b) External synchronization clock unit
Transmitter
Receiver
synchro
clock
data + synchro
(c) Transmitter superposes clock and data
Transmitter
and
Synchronization
generator
Receiver
Serial vs Parallel communication
Serial: one signal carrying wire
Parallel: 8, 16, or 32 signal carrying wires
 Parallel: faster, more expensive, used for short-distances only
examples: Data bus, Control bus in a microprocessor; parallel port in PC
Bit coding
1
NOT Good;
constant HIGH voltage
 delays [due to Capacitance/Inductance]
0
1
0
0
0
1
1
Binary Direct
NRZ (nonreturn to zero
RZ (return
to zero)
manchester
coding
0V
Signal during half-cycle
then return to zero
High-to-Low  ‘1’
Low-to-High  ‘0’
Very commonly used
differential
manchester
coding
Voltage change at start of cycle  ‘0’
No change at start of cycle  ‘1’
Voltage flips at mid-cycle
Communication: Handling errors
Problem: some data sent from A  B may get ‘corrupted’
Why is this a big problem?
- How will the receiver know they received ‘bad data’?
Probability of communication error is reduced by:
Error detection and Correction
Communications: Error detection and correction schemes
Importance of EDC (error detection and correction):
Assume error rate of 0.1%
Average sentence of text: 125 characters = 125 x 8 = 1000 bits
0.1% error  1 error per sentence!
Techniques for EDC:
1. IF receiver detects error  requests sender to re-transmit
2. Receiver detects and corrects error without re-transmission
Error detection: Parity
Error detection:
some information is added to message, that allows checking for errors.
PARITY
1 ASCII char = 7bits;
1 extra bit is added to each character, called parity bit:
Even parity: value of parity bit is set to make total number of 1’s even.
Odd parity: value of parity bit is set to make total number of 1’s odd.
Example:
(ASCII) W: 1010 111
(odd parity) W: 0 1010 111
(even parity) W: 1 1010 111
Error Detection: Checksums
Let: message = “Hello, world” == 12 bytes == 128 bits (including parity bit)
*assuming: parity bit = 0
Hello, world
72
101
108
108
111
44
32
119
111
114
108
100
Sum = 72 + 101 + 108 + … + 100 = 1128
Checksum [16-bits] = 1128
Message:
72
101
108
108
111
Checksum [8-bits] = 1128 mod 256 = 104
44
32
119
111
114
108
1 checksum/12 bytes  too much overhead
 typical use: 1 checksum byte per 128 Bytes of data
100
104
Error Detection: Cyclic Redundancy Check (CRC)
Basic idea of CRC:
Pre-agreed between sender/receiver
Number: 629
Divisor: 25
mod( Number, Divisor): mod(629, 25) = 4
Transmit: (629,4)
Receiver: mod( 629, 25) == 4 ?  Transmission probably OK
Error Detection: Cyclic Redundancy Check (CRC)
Implementation of CRC:
Number: Bitstream of 1 Block (e.g. 128 Bytes)
Divisor: common CRC schemes are:
CRC12
1100000001011
CRC16
11000000000000101
CRC-CCITT
10001000000100001
CRC32
100000100110000010001110110110111
M = mod( Number, Divisor): computed very efficiently with simple circuits
FRAME: Number MOD
Receiver: mod(Number MOD, CRC) = = M ?
Yes  Transmission probably OK
Error detection and Correction
Parity, Checksum, CRC: detect error, request re-transmit on error
Redundant data transmission: detect error, and correct automatically!
Common methods:
Repeating
Hamming codes
Reed-Muller codes, etc…
Majority coding:
Data: 010
Transmit: 000111000
Receive (with error): 001011000
select majority from each group of 3
0 1
0
Accepted data: 010
Hamming codes
Problem of Majority coding:
(1) too much redundancy
(2) burst errors can’t be handled
Hamming code: main idea of a (7, 4) Hamming code
Data: 0011
Add 1-bit to each circle,
total of each circle  even
(7, 4) Hamming code
transmit
Data: 0011
Error must be
in bit shared by
red and green
red-circle: parity ERROR
green circle: parity ERROR
blue circle: parity OK
 receiver
(7, 4) Hamming code…
transmit
Data: 0011
Error must be
in unshared bit
of Blue circle!
red-circle: parity OK
green circle: parity OK
blue circle: parity ERROR
 receiver
HW: verify that ALL 1-bit errors can be detected and corrected!
Hamming code…
Data: bit-stream
write data in particular sequence
compute, add hamming code bits to data
transmit
Corrected data
receiver checks Hamming codes, corrects errors
Common Hamming code used: (12, 8) Hamming code
Extending Hamming code for longer bit-stream
1. All bit positions that are powers of two are used as parity bits.
(positions 1, 2, 4, 8, 16, 32, 64, etc.)
2. All other bit positions are for the data to be encoded.
(positions 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, etc.)
3. Each parity bit stores the parity for assigned bits in the code word:
The position of the parity bit determines the sequence of bits that it
alternately checks and skips.
Position 1: skip 0 bits, check 1 bit, skip 1 bit, check 1 bit, skip 1 bit, …
Position 2: skip 1 bit, check 2 bits, skip 2 bits, check 2 bits, skip 2 bits, …
Position 4: skip 3 bits, check 4 bits, skip 4 bits, check 4 bits, skip 4 bits, …
Position 8: skip 7 bits, check 8 bits, skip 8 bits, check 8 bits, skip 8 bits, …
Position 16: skip 15 bits, check 16 bits, skip 16 bits, check 16 bits, skip 16 bits, …
Position 32: skip 31 bits, check 32 bits, skip 32 bits, check 32 bits, skip 32 bits, …
Burst errors
Problem with Hamming:
burst errors: several contiguous data bits in error)
Handling burst errors:
- Interleaving
- Reed-Solomon coding
Examples of usage: storage media (CDROM’s), …
Packets
Long message  p(error) is high  Need to re-transmit [part] of message
Solution: Break message into small “packets”  send packets 1-by-1
To address
From address
Long message
part 1 of 3
part 2 of 3
part 3 of 3
packets
To address From address 1/3
To address From address 2/3
To address From address 3/3
data (part 1 of 3)
data (part 2 of 3)
data (part 3 of 3)
EDC
EDC
EDC
transmit
Receiver
Re-constructs
Message from
three parts received
Typical packet size: 2048 - 4096 Bytes
Note: later we’ll see structure of packet in more detail
Question: Why do some web pages load in non-sequential fashion (some pictures load first, others later)
Network terminology
LAN: Local Area Network
A network of communicating devices in a small area (e.g. a building,
a factory, etc.)
Common ways of physically connecting computers in a LAN:
Cables (wires), Bluetooth, Wi-Fi…
WAN: Wide Area Network
Two or more LAN’s connected to each other, over a large area,
e.g. international communication networks.
Common ways of connecting between LAN’s in a WAN:
Telephone networks, Long-distance cables, Satellites
Network topologies
Suppose N computers need to communicate with each other
Pairwise connections:
How many ?
Problems ?
Network topologies
Network topology describes how different devices are (physically) connected to each other.
1
1
6
2
6
2
Central Hub
3
5
3
5
4
4
(a) Ring topology
(b) Star topology
6
5
1
1
Terminator
3
2
•• •
5
Stub
3
2
Bus
••
•
4
4
(c) Mesh topology
Tap
(d) Bus topology
6
Network communication basics
The following slides, based largely on
the those provided by Kurose and Ross,
will be used to get an introduction to real-world
network communications.
Computer Networking:
A Top Down Approach
Featuring the Internet,
3rd edition.
Jim Kurose, Keith Ross
Addison-Wesley, July
2004.
What’s the Internet ?
• millions of connected computing
devices:
hosts = end systems
• running network applications
• communication links
– fiber, copper, radio, satellite
– transmission rate = bandwidth
• routers: forward packets
router
server
workstation
mobile
local ISP
regional ISP
UST
network
What’s the Internet..
router
• protocols control sending, receiving
of msgs
– e.g., TCP, IP, HTTP, FTP, PPP
• Internet: “network of networks”
– public: Internet
– private: Intranet
• Internet standards
– RFC: Request for comments
– IETF: Internet Engineering Task
Force
server
workstation
mobile
local ISP
regional ISP
UST
network
What’s a protocol?
protocols define format, order of msgs sent and received among network entities,
and actions taken on msg transmission, receipt
a human protocol and a computer network protocol:
Hi
TCP connection
req
Hi
TCP connection
response
What’s the
time?
Get http://www.awl.com/kurose-ross
2pm
<file>
time
A closer look at network structure
• network edge: applications and hosts
• network core:
– routers
– network of networks
• access networks, physical media:
communication links
The network edge
• end systems (hosts):
– run application programs
– e.g. Web, email
– at “edge of network”
• client/server model
– client host requests, receives
service from always-on server
– e.g. Web browser/server; email
client/server
Network edge: connection-oriented service
Goal: data transfer between end
systems
• handshaking: setup (prepare
for) data transfer ahead of time
– Hello, hello back human
protocol
– set up “state” in two
communicating hosts
e.g.
TCP service [RFC 793]
• reliable, in-order byte-stream data
transfer
– loss: acknowledgements and
retransmissions
• flow control:
– sender won’t overwhelm
receiver
• congestion control:
– senders “slow down sending
rate” when network congested
Network edge: connectionless service
Goal: data transfer between
end systems
e.g.
UDP - User Datagram Protocol:
connectionless
unreliable data transfer
no flow control
no congestion control
App’s using TCP:
• HTTP (Web), FTP (file transfer), Telnet (remote login), SMTP (email)
App’s using UDP:
• streaming media, teleconferencing, DNS, Internet telephony
The Network Core
• mesh of interconnected routers
• the fundamental question: how is
data transferred through net?
– circuit switching: dedicated
circuit per call: telephone net
– packet-switching: data sent
through net in discrete “chunks”
Network Core: Circuit Switching
End-end resources reserved for
“call”
• link bandwidth, switch
capacity
• dedicated resources: no sharing
• circuit-like (guaranteed)
performance
• call setup required
Network Core: Packet Switching
Each end-end data stream divided into
packets
• user A, B packets share network
resources
• each packet uses full link bandwidth
• resources used as needed
Bandwidth division into “pieces”
Dedicated allocation
Resource reservation
Resource allocation:
• total resource demand can
exceed amount available
• congestion: packets queue, wait
for link use
• store and forward: packets move
one hop at a time
– Node receives complete
packet before forwarding
Packet-switching: store-and-forward
L
R
•
•
•
•
R
R
Packet Length: L bits
Baud rate: R bps
Time to push packet on link: L/R sec
Entire packet must arrive at router before
it can be transmitted on next link: store
and forward
• delay = 3L/R
Example:
• L = 7.5 Mbits
• R = 1.5 Mbps
• delay = 15 sec
Access networks and physical media
Q: How to connect end systems to
edge router?
• residential access nets
• institutional access networks
(school, company)
• mobile access networks
Residential access: point to point access
• Phone modem
– up to 56Kbps direct access to router
(often less)
– Can’t surf and phone at same time: can’t
be “always on”
• ADSL: asymmetric digital subscriber line [similar to NOW Broadband]
– up to 1 Mbps upstream
– up to 8 Mbps downstream
Residential access… Cable modems
cable headend
cable distribution
network (simplified)
home
Residential access: cable modems
Diagram: http://www.cabledatacomnews.com/cmic/diagram.html
Company access: local area networks
• company/univ local area network (LAN)
connects end system to edge router
• Ethernet:
– shared or dedicated link connects end
system and router
– 10 Mbs, 100Mbps, Gigabit Ethernet
Wireless access networks
router
base
station
•
•
shared wireless access network connects end system
to router
– via base station aka “access point”
wireless LANs:
– 802.11b (WiFi): 11 Mbps (good for networks)
– bluetooth: 720Kbps (good for device-to-device)
mobile
hosts
Home networks
Typical home network components:
• ADSL or cable modem
• router/firewall/NAT
• Ethernet
• wireless access point
to/from
cable
headend
cable
modem
router/
firewall
Ethernet
wireless
laptops
wireless
access
point
Internet structure: network of networks
• a packet passes through many networks!
local
ISP
Tier 3
ISP
Tier-2 ISP
local
ISP
local
ISP
local
ISP
Tier-2 ISP
Tier 1 ISP
Tier 1 ISP
Tier-2 ISP
local
local
ISP
ISP
Network Access Point
Tier 1 ISP
Tier-2 ISP
local
ISP
Tier-2 ISP
local
ISP
Protocol “Layers”
Networks are complex!
• many “pieces”:
– hosts
– routers
– links of various media
– applications
– protocols
– hardware, software
Analogy: Organization of air travel
ticket (purchase)
ticket (complain)
baggage (check)
baggage (claim)
gates (load)
gates (unload)
runway takeoff
runway landing
airplane routing
airplane routing
airplane routing
Layering of airline functionality
ticket (purchase)
ticket (complain)
ticket
baggage (check)
baggage (claim
baggage
gates (load)
gates (unload)
gate
runway (takeoff)
runway (land)
takeoff/landing
airplane routing
airplane routing
airplane routing
departure
airport
airplane routing
airplane routing
intermediate air-traffic
control centers
Layers: each layer implements a service
– via its own internal-layer actions
– relying on services provided by layer below
arrival
airport
Why layering?
Dealing with complex systems:
• explicit structure allows identification, relationship of complex
system’s pieces
– layered reference model for discussion
• modularization eases maintenance, updating of system
– change of implementation of layer’s service transparent to rest of
system
– e.g., change in gate procedure doesn’t affect rest of system
• layering considered harmful?
Internet protocol stack
• application: supporting network applications
– FTP, SMTP, STTP
• transport: host-host data transfer
– TCP, UDP
• network: routing of datagrams from source to
destination
– IP, routing protocols
• link: data transfer between neighboring network
elements
– PPP, Ethernet
• physical: bits “on the wire”
application
transport
network
link
physical
source
message
segment Ht
datagram Hn Ht
frame
Hl Hn Ht
M
M
M
M
Encapsulation
application
transport
network
link
physical
Hl Hn Ht
M
link
physical
Hl Hn Ht
M
switch
destination
M
Ht
M
Hn Ht
Hl Hn Ht
M
M
application
transport
network
link
physical
Hn Ht
Hl Hn Ht
M
M
network
link
physical
Hn Ht
Hl Hn Ht
M
M
router
The Network Layer: Internet Protocol
• What’s inside a router
• Internet Protocol and IP addresses
• How packets are routed
Internet Protocol (IP)
The Internet Protocol (IP) is a network-layer (Layer 3) protocol that contains
addressing information and some control information that enables packets
to be routed.
Network layer functions: Forwarding and Routing
Forwarding: determines which link to
take at a specific router;
routing algorithm
local forwarding table
header value output link
0100
0101
0111
1001
Routing: plan of a series of forwarding
data that can take the packet from source
to destination
3
2
2
1
value in arriving
packet’s header
0111
1
3 2
DATAGRAM
IP datagram format
IP protocol version
number
header length
(bytes)
“type” of data
max number
remaining hops
(decremented at
each router)
upper layer protocol
to deliver payload to
how much overhead
with TCP?
• 20 bytes of TCP
• 20 bytes of IP
• = 40 bytes + app
layer overhead
32 bits
type of
ver head.
len service
length
fragment
16-bit identifier flgs
offset
upper
time to
Internet
layer
live
checksum
total datagram
length (bytes)
for
fragmentation/
reassembly
32 bit source IP address
32 bit destination IP address
Options (if any)
data
(variable length,
typically a TCP
or UDP segment)
E.g. timestamp,
record route
taken, specify
list of routers
to visit.
Datagram networks
• packets forwarded using destination host address
– packets between same source-dest pair may take different paths
application
transport
network
data link 1. Send data
physical
application
transport
2. Receive data network
data link
physical
Main router functions:
• run routing algorithms/protocol (RIP, OSPF, BGP)
• forwarding datagrams from incoming to outgoing link
IP Addressing: introduction
223.1.1.1
• IP address: 32-bit identifier for
host, router interface
• interface: connection between
host/router and physical link
– router’s typically have
multiple interfaces
– host may have multiple
interfaces
– IP addresses associated
with each interface
223.1.2.1
223.1.1.2
223.1.1.4
223.1.1.3
223.1.2.9
223.1.3.27
223.1.2.2
223.1.3.2
223.1.3.1
223.1.1.1 = 11011111 00000001 00000001 00000001
223
1
1
1
Subnets
• IP address:
– subnet part (high order
bits)
– host part (low order bits)
• What’s a subnet ?
– device interfaces with
same subnet part of IP
address
– can physically reach each
other without intervening
router
223.1.1.1
223.1.2.1
223.1.1.2
223.1.1.4
223.1.1.3
223.1.2.9
223.1.3.27
223.1.2.2
LAN
223.1.3.1
223.1.3.2
network consisting of 3 subnets
Subnets
223.1.1.0/24
223.1.2.0/24
• To determine the subnets,
detach each interface from its
host or router, creating islands
of isolated networks. Each
isolated network is called a
subnet.
223.1.3.0/24
Subnet mask: /24
223.1.1.2
Subnets
223.1.1.1
223.1.1.4
223.1.1.3
How many?
223.1.9.2
223.1.7.0
223.1.9.1
223.1.7.1
223.1.8.1
223.1.8.0
223.1.2.6
223.1.2.1
223.1.3.27
223.1.2.2
223.1.3.1
223.1.3.2
IP addresses: how to get one?
Q: How does host get IP address?
• hard-coded by system administrator in a file
– Wintel: control-panel->network->configuration->tcp/ip->properties
• DHCP: Dynamic Host Configuration Protocol: dynamically get address
from as server
– “plug-and-play”
DNS: Domain Name System
People: many identifiers:
– HKID, name, passport #
Internet hosts, routers:
– IP address (32 bit) - used
for addressing datagrams
– “name”, e.g.,
ww.yahoo.com - used by
humans
Q: map between IP addresses and
name ?
Domain Name System:
• distributed database implemented
in hierarchy of many name servers
• application-layer protocol host,
routers, name servers to
communicate to resolve names
(address/name translation)
Transport services and protocols
• provide logical communication
between app processes running on
different hosts
• Most common transport protocol:
TCP
application
transport
network
data link
physical
network
data link
physical
The TCP provides a reliable, continuous stream of data
- protocol for automatically requesting missing data
- reordering IP packets that arrive out of order
- converting IP datagrams to a streaming protocol
- routing data within a computer to the correct application.
network
data link
physical
network
data link
physical
network
data link
physical
network
data link
physical
application
transport
network
data link
physical
The Application layer
•
•
•
•
Principles of network applications
Web and HTTP
FTP
Electronic Mail
– SMTP, POP3, IMAP
• DNS
• Socket programming with TCP
• Building a Web server
Creating a network app
Write programs that
– run on different end systems
and
– communicate over a network.
– e.g., Web: Web server software
communicates with browser
software
No software written for devices in
network core
– Network core devices do not
function at app layer
– This design allows for rapid
app development
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
Client-server architecture
server:
– always-on host
– permanent IP address
clients:
– communicate with server
– may be intermittently
connected
– may have dynamic IP
addresses
– do not communicate
directly with each other
Sockets
• process sends/receives messages
to/from its socket
• socket analogous to door
– sending process shoves
message out door
– sending process relies on
transport infrastructure on other
side of door which brings
message to socket at receiving
process
host or
server
host or
server
process
controlled by
app developer
process
socket
socket
TCP with
buffers,
variables
Internet
controlled
by OS
TCP with
buffers,
variables
Addressing processes
• For a process to receive messages, it must have an identifier
• A host has a unique32-bit IP address
• Q: does the IP address of the host on which the process runs
suffice for identifying the process?
• Answer: No, many processes can be running on same host
• Identifier includes both the IP address and port numbers associated
with the process on the host.
• Example port numbers:
– HTTP server: 80
– Mail server: 25
Socket programming
Goal: learn how to build client/server application that communicate using sockets
Socket API
• introduced in BSD4.1 UNIX,
1981
• explicitly created, used, released
by apps
• client/server paradigm
• two types of transport service via
socket API:
– unreliable datagram
– reliable, byte stream-oriented
socket
a host-local,
application-created,
OS-controlled interface
(a “door”) into which
application process can
both send and
receive messages to/from
another application
process
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
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
host or
server
controlled by
application
developer
controlled by
operating
system
Socket programming with TCP
Client must contact server
• server process must first be
running
• server must have created socket
(door) that welcomes client’s
contact
Client contacts server by:
• creating client-local TCP socket
• specifying IP address, 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
– source port numbers used to
distinguish clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Stream terminology
• A stream is a sequence of characters that flow into or out of
a process.
• An input stream is attached to some input source for the
process, eg, keyboard or socket.
• An output stream is attached to an output source, eg,
monitor or socket.
Socket programming with TCP
output
stream
inFromServer
Client
Process
process
input
stream
outToServer
Example client-server app:
1) client reads line from standard input
(inFromUser stream) , sends to
server via socket (outToServer
stream)
2) server reads line from socket
3) server converts line to uppercase,
sends back to client
4) client reads, prints modified line
from socket (inFromServer
stream)
monitor
inFromUser
keyboard
input
stream
client
TCP
clientSocket
socket
to network
TCP
socket
from network
Client/server socket interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Example: Java client (TCP), cont.
Create
input stream
attached to socket
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
Send line
to server
outToServer.writeBytes(sentence + '\n');
Read line
from server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket
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()));
Example: Java server (TCP), cont
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
End of while loop,
loop back and wait for
another client connection