CSE 501N Fall ‘06 02: Machine Overview + Fundamental Types
Download
Report
Transcript CSE 501N Fall ‘06 02: Machine Overview + Fundamental Types
CSE 501N
Fall ‘09
22: Introduction to Networking
November 24 2009
Nick Leidenfrost
Lecture Outline
Networking
Java resources for networking
Client-server architecture
Protocols
2
Networking Overview
Java Program
Java Program
Networking
Infrastructure
Networking
Infrastructure
Hardware
wire
Hardware
3
Networking Overview
Sending data from A to B is not easy
The wire is not reliable
Due to all kinds of interference
Have to develop protocols
Agree on a format for the data
Agree on who talks when
Agree on what additional information needs to be
supplied
2 Kinds of protocols:
Network Protocols
Application Protocols
What data your application sends across the network, what it expects
to receive as a result, etc.
Specific to each application
4
Packets
Data that is sent between two computers
is sent in bundles called packets
In addition to the data being sent, packets
also carry information about the sender
and the recipient needed by low level
protocols
5
Protocols
Common protocols
Internet Protocol (“IP”)
Unreliable data transmission protocol
Cannot guarantee packets will arrive in order, or at all
Transmission Control Protocol “TCP”
Reliable data transmission protocol
Ordered delivery
Operates over IP
Uses a system of acknowledgements, or “ACKs”
Datagram Protocol “UDP”
Unreliable (loss / duplication possible)
Operates over IP
Streaming media, Multiplayer online games, VOIP
Applications willing to sacrifice reliability for speed
6
Seems like a lot of work!
We are essentially trying to send data
reliably over an unreliable channel
…But there is help for programmers
TCP/IP
makes the channel reliable
Abstraction of the network means we don’t
have to deal with low level things like packets
and can write at a high level
Java provides resources that allow you to
send data easily
Contained in the java.net package
7
What do we need to send data?
The destination of the data:
IP
address
Port
The actual data being sent:
Has
to be Serializable
Capable of being sent over a stream
Primitives (ints, doubles, etc)
Objects of classes that implement
java.io.Serializable
8
IP Address
The IP address is a sequence of 4 numbers
Each
number is a byte
E.g. 128.252.19.34
The special address 127.0.0.1 always points to your
own machine
To send data to B, A needs to know B's Internet
address
(Relatively) Recently, IPv6 was introduced
Uses
16 bytes instead of 4
(Many, many) More addresses
We will focus on IPv4
9
Port Numbers
One computer can offer multiple services
over the Internet
For
example, both a web server program and
an email server program
When data packets are sent to that
computer, they need to indicate which
program is meant to receive and handle the
data
10
Port Numbers
We use port numbers to differentiate networking
applications on a computer
In
TCP, a port number is an integer between 0 and
65,535
The sending program must know the port number of
the receiving program
Ports numbers are divided into 3 categories
Well Known (Reserved): 0 - 1023
21: FTP
22: SSH
23: Telnet
80: HTTP
Registered (registered with IANA): 1024 - 49151
Dynamic / Private: 49,152 – 65,535
11
A Client Program – Sockets
A socket is an object that encapsulates a TCP/IP
connection
There is a socket on both ends of a connection
Syntax to create a socket in a Java program
Socket s = new Socket(hostname, portnumber);
If it can't find the host, the Socket constructor
throws an UnknownHostException
12
A Client Program – Input and Output
Streams
Use the input and output streams attached
to the socket to communicate with the other
endpoint
Code to obtain the input and output
streams
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
13
A Client Program – Input and Output
Streams
When you send data to outstream, the
socket forwards them to the server
The socket catches the server's response
and you can read it through instream
When you are done communicating with
the server, close the socket
s.close();
14
Client and Server Sockets
Differentiating Input and Output streams with
Sockets can be a bit confusing
Remember: We read from an input stream and
write to an output stream
15
More friendly Stream Interaction
Scanners and Writers
InputStream and OutputStream send
and receive bytes
To send and receive text, or primitive data
use a Scanner and a Writer
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
16
More friendly Stream Interaction
A PrintWriter buffers the characters
and only sends when the buffer is full
Buffering
increases performance
When sending a command, you want the
whole command to be sent immediately
Flush
the buffer manually:
out.print(command);
out.flush();
17
A Server Program
Sample server program: enables clients to
manage bank accounts in a bank
When you develop a server application,
you need some application-level protocol
The client can use this protocol to interact
with the server
A simple bank access protocol is described
on the next slide
18
Simple Bank Access Protocol
Client
Request
Server
Response
Meaning
BALANCE
int n
The balance as a
double
Get the balance of
account n
DEPOSIT
int n
double a
The new balance as
a double
Deposit amount a into
account n
WITHDRAW
int n
double a
The new balance as
a double
Withdraw amount a from
account n
QUIT
none
Quit the connection
19
A Server Program
The server waits for clients to connect on a
certain port
Use
a number in the Dynamic / Private range
…say, 50,000
To listen for incoming connections, use a
ServerSocket (java.net)
To construct a ServerSocket, just
provide the port number you want to accept
connections on
ServerSocket server = new ServerSocket(50000);
20
A Server Program
Use the accept method to wait for client
connection and obtain a socket
ServerSocket server = new ServerSocket(portNum);
Socket s = server.accept();
BankService service = new BankService(s, bank);
service.run();
accept blocks until a program tries to connect to
the ServerSocket
Blocking methods: Methods which halt control
flow until some external cue allows them to
proceed
External cue:
Connection from remote client
Signal from another Thread (More on this next time)
21
A Server Program – BankService
BankService carries out the service
requested by the client
Implements the Runnable interface
Its run method will be executed in each
thread that serves a client connection
22
A Server Program – BankService
run gets a scanner and writer from
the socket, then executes:
public void doService() throws IOException {
while (true) {
if (!in.hasNext()) return;
String command = in.next();
if (command.equals("QUIT")) return;
executeCommand(command);
}
}
23
A Server Program – executeCommand
Processes a single command
If the command is DEPOSIT, it carries out
the deposit
int account = in.nextInt();
double amount = in.nextDouble();
bank.deposit(account, amount);
WITHDRAW is handled in the same way
24
A Server Program – executeCommand
After each command, the new balance is
sent to the client:
out.println(bank.getBalance(account));
25
A Server Program
The run method executes until the client closes
the connection or the command equals QUIT
How
can we support multiple simultaneous
clients?
Spawn a new thread whenever a client
connects
Each thread is responsible for serving one
client
26
A Server Program – Threads
BankService implements Runnable; so,
it can start a thread using start() (of
class Thread)
The thread dies when the client quits or
disconnects and the run method exits
27
A Server Program – Threads
In the meantime, BankServer loops back to
accept the next connection
while (true) {
Socket s = server.accept();
BankService service = new BankService(s, bank);
Thread t = new Thread(service);
t.start();
}
The server program never stops
When you are done running the server, you need
to kill it
28
Sending Objects
Sometimes, you may want to send actual
Java objects across the wire
Easier
than sending textual representations of
all data
Once the object is received at the other end,
methods can be called and state accessed
To do this, wrap the socket’s streams with
object streams instead of using Scanner
and PrintWriter
29
Object Stream
Creating an object stream - client side
Socket s = new Socket(“128.252.19.34”, 20000);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
LinkedList list = new LinkedList();
oos.writeObject(list);
30
Object Stream
Creating an object stream - server side
ServerSocket ss = new ServerSocket(20000);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
LinkedList list = (LinkedList)ois.readObject();
readObject() returns an Object
Must
know what type of object you are expecting to
perform the correct cast
Protocol definition becomes important
31
Conclusion
Questions?
Quiz #6 Now
In Lab Afterwards!
36