Networking

Networking
COMP 112 2017
School of Engineering and Computer Science
Victoria University of Wellington
Copyright: Peter Andreae & David Streader, VUW
Basic Networking
• Network comunication
• Vast basket of things that different users want
• Design metaphore to give:
• Different interfacces for different users
• And an understandable code structure
• Admin
COMP112 21: 2
Networking
COMP112 21: 3
• Computers are (mostly) networked
i.e. connected to bits of hardware some being computers:
•
•
•
•
•
•
desktops,
laptops
embedded controllers
servers
phones
sensors
• Users expect to be able communicate with the world
• Computers increasingly require networking to operate
• How does networking function?
• How can we write programs that use the network?
What’s required to communicate?
COMP112 21: 4
Users do not
need to
understand what
goes on inside
this bubble
Java application
programmers only
need a shallow
understanding of
the bubble
Shopping basket of networking wants
COMP112 21: 5
•
•
•
•
•
•
•
•
Transmit a character over a specific wire or fibre
Send data to machine where no direct connection exists
Split a message into “packets” and rebuild
Deal with multiple packets from multiple computers on the
same wire at the same time
Correct transmission errors
Real time data transmission
Manage a “conversation” with a distant server.
Software reconfiguration around hardware failure
• Design metaphor to structure networking software
Levels of communication
COMP112 21: 6
• Different users appear to communicate at different levels
1.
2.
3.
4.
5.
Transmit a character over a specific wire or fibre
Send email
Simultaneous multiple packets from multiple computers
Send Movie
Manage a “conversation” with a distant server.
Protocol Stack – design metaphor
•
•
•
•
•
COMP112 21: 7
Layers of programs that build on each other.
Each layer calls methods provided by the layer below
Applications only appear to have direct communication
Actual communication occurs at the lowest layer
It only appears that a mail program sends an email.
• Application Layer
Application
• Transport Layer
• Internet Layer
• Network Access Layer
Network
TCP/IP Stack
COMP112 21: 8
• Network Access Layer (eg, Ethernet protocol)
• Part of the operating system
• Encoding a packet into signals to go over a wire/fibre/wireless to the
computer at the other end of the wire, and decoding it.
• Dealing with collisions – two computers sending at the same time
TCP/IP Stack
COMP112 21: 9
• Internet Layer (eg, IP protocol)
• Part of operating system
• Encoding information into a packet with address information
• Dealing with addresses and routing a packet to the target computer at
the other side of the network.
• Network Access Layer
TCP/IP Stack
COMP112 21: 10
There are other
• Transport Layer (eg TCP protocol)
protocols that work
• Part of operating system
differently
• Ensuring sequence of packets in a message all arrive in order
• Protocol to acknowledge, recover, resend, reorder.
• Internet Layer
• Network Access Layer
TCP/IP Stack
• Application Layer
• Programs that need to communicate - Clients and Servers
• Only need to understand part of the Transport layer
• Transport Layer
• Internet Layer
• Network Access Layer
COMP112 21: 11
Application Layer view
COMP112 21: 12
• Applications that want to communication with other
applications on another computer can use TCP scokets.
Scoket
• TCP (Transport Layer) provides “Sockets”:
• A socket is one end of a TCP connection
• A scotet has two streams associated with it:
• Input stream (from the other end of the connection)
Application can read from it
• Output stream (to the other end of the connection)
Application can write/print to it
Getting a Socket
COMP112 21: 13
• Connection requires each computer to set up a socket
connected to the other
• How do you specify where you want the connection to go to?
• Which program running on which computer is going to server you a
web page?
• Which computer sets up a socket first?
• Do you need a socket at the other end before you set up yours?
Addressing a connection
COMP112 21: 14
• Two parts to the address:
• host computer: IP address, or Host name
• “port” (an integer)
• Each computer on the network must have an IP address.
130.195.5.9 or “debretts.ecs.vuw.ac.nz”
• Operating system provides a set of “ports” that connections
can be associated with.
1
2
:
80
:
6667
1
2
:
80
:
6667
• Type echo $HOSTNAME in a terminal to find your name and
use this to connect to a service on your own computer.
Who goes first?
COMP112 21: 15
• Client – Server model:
• Server applications can run on a remote host
• Server application listens on a known port
• Client application requests a connection to host : port
• Server applications accept the request
• Operating systems will set up a new socket on each end with a
connection between them.
• A Java ServerSocket only needs a port number;
• A Java Socket needs a port number and the IP or host name
of the computer the server is running on.
• Setup the Server before the Client
Using sockets in Java
COMP112 21: 16
• Sockets provide streames that are easy to use!
• Streams are sequences of characters
• Like files, or UI text pane, or System.in/System.out
try {
Socket socket = get a socket somehow ;
Scanner inputFromTarget = new Scanner(socket.getInputStream());
PrintStream outputToTarget = new Scanner(socket.getOutputStream());
while (inputFromTarget.hasNext()){
String line = inputFromTarget.nextLine();
String response = work out a response ;
outputToTarget.println(response);
flush(response);
}
} catch (IOException e){System.out.println(“connection failure ”+e); }
Addressing a connection
Host1
1
2
:
80
xx
:
6667
COMP112 21: 17
Host2
1
2
:
80
:
6667
Server Listens on port 6667
Client request connection to
port 6667 on Host2
port xx is not specified
Server accepts connection
OS creates socket connected to
port xx on Host1
OS creates socket connected to
port 6667 on Host2
Server is still listening!
Setting up the sockets
COMP112 21: 18
Server
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket socket = serverSocket.accept();
Server
port
… set up Scanner and PrintStream and communicate via socket
}
} catch (IOException e){System.out.println("Failed connection "+ e); }
Server
try {
port
Socket socket = new Socket(("irc.ecs.vuw.ac.nz", PORT);
Client
…set up Scanner and PrintStream and communicate via socket
} catch (IOException e){System.out.println("Failed connection "+ e); }