CSCE 790: Computer Network Security
Download
Report
Transcript CSCE 790: Computer Network Security
CSCE 515:
Computer Network Programming
Chin-Tser Huang
[email protected]
University of South Carolina
Write a Multithreaded Server
Benefits of multithreaded server
Exceptions and problems with a connection
is limited to corresponding thread
Implementation is cleaner
Two ways of making your server
multithreaded
Extend Thread class
Implement Runnable interface
1/27/2005
2
Class Thread
Constructors
Thread(String name)
Thread(Runnable target)
Static Methods
static
static
static
static
1/27/2005
int activeCount()
Thread currentThread()
void sleep(long millis) throws InterruptedException
void yield()
3
Class Thread
Instance Methods
void run()
void setPriority(int newPriority)
int getPriority()
void start()
void stop()
1/27/2005
4
Interface Runnable
Method
void run()
1/27/2005
5
A Multithreaded Echo Server Example
Main thread accepts connections and
launches a new handler thread for each
connection
Each handler thread echoes received
data back to sender
1/27/2005
6
MTEchoServer.java
/* * Java Network Programming, Second Edition * Merlin Hughes,
Michael Shoffner, Derek Hamner * Manning Publications
Company; ISBN 188477749X * * http://nitric.com/jnp/ * *
Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek
Hamner; * all rights reserved; see license.txt for details. */
import java.net.*;
import java.io.*;
public class MTEchoServer extends Thread {
// MTEchoServer (Socket socket) …
// public void run () …
// public static void main (String[] args) throws IOException …
}
1/27/2005
7
Constructor MTEchoServer
protected Socket socket;
MTEchoServer (Socket socket) {
this.socket = socket;
}
1/27/2005
8
Method run
public void run () {
try {
InputStream in = socket.getInputStream ();
OutputStream out = socket.getOutputStream ();
byte[] buffer = new byte[1024];
int read;
while ((read = in.read (buffer)) >= 0)
out.write (buffer, 0, read);
} catch (IOException ex) {
ex.printStackTrace ();
} finally {
try {
socket.close ();
} catch (IOException ignored) {
}
}
}
1/27/2005
9
Method main
public static void main (String[] args) throws IOException {
if (args.length != 1)
throw new IllegalArgumentException ("Syntax: MTEchoServer <port>");
System.out.println ("Starting on port " + args[0]);
ServerSocket server = new ServerSocket (Integer.parseInt (args[0]));
while (true) {
Socket client = server.accept ();
MTEchoServer echo = new MTEchoServer (client);
echo.start ();
}
}
1/27/2005
10
Link Layer
Three purposes of link layer
Send and receive IP datagrams for IP
module
Send and receive ARP requests and replies
for ARP module
Send and receive RARP requests and
replies for RARP module
1/27/2005
11
Link Layer Channel
Two types of link layer channels
Broadcast: e.g. LAN, wireless LAN
Point-to-point: e.g. between router and
router or between dialup modem and ISP
router
Ethernet for broadcast channel
SLIP and PPP for point-to-point link
1/27/2005
12
Ethernet
Most popular LAN technology because
of its simplicity
Different flavors of Ethernet
Bus topology, star topology
Coaxial cable, twisted-pair copper wire,
fiber optics
10Mbps, 100Mbps, 1Gbps, 10Gbps
Use 48-bit addresses
1/27/2005
13
Ethernet Frame
Preamble
8
1/27/2005
Dest.
Source
address
address
6
6
Type
Data
CRC
2
46-1500
4
14
Ethernet Frame Demultiplexing
IP
ARP
RARP
Ethernet
driver
incoming frame
1/27/2005
15
CSMA/CD
Carrier Sense: an adapter never transmits a
frame when it senses that other adapter is
transmitting
Multiple access: any adapter can transmit
at any time
Collision detection: an adapter aborts its
transmission as soon as it detects other
adapter is also transmitting, and waits a
random time to retransmit
1/27/2005
16
Serial Line IP (SLIP)
A simple form of encapsulation for IP
datagrams on serial lines
Put delimiter bytes around both end of
datagram, and use escape bytes to replace
occurrences of delimiter bytes in datagram
Some deficiencies of SLIP
No negotiation of IP addresses
No type field
No checksum
1/27/2005
17
Point-to-Point Protocol (PPP)
Three components
Framing mechanism
Link control protocol (LCP)
Network control protocol (NCP)
Fix deficiencies in SLIP
1/27/2005
18
Requirements of PPP
Packet framing
Transparency
Multiple network-layer protocols
Multiple types of links
Error detection
Connection liveness
Network-layer address negotiation
Simplicity
1/27/2005
19
PPP Data Framing
Flag
Address
Control
01111110 11111111 00000011
1
1
1/27/2005
1
Flag
Protocol
Data
1 or 2
Variable
length
Check
2 or 4
01111110
1
20
PPP Byte Stuffing
When flag pattern appears in data, PPP stuffs
a control escape byte before it
b5
b4
01111110
b2
b1
PPP
b1
b2
01111110
b4
b5
PPP
b5 b4 01111110 01111101 b2 b1
1/27/2005
21
Loopback Interface
Used for communication between client and server
on the same host, and for functionality testing
Class A network ID 127 reserved for lookback
interface
Assigned the address 127.0.0.1 and the name
localhost
Loopback interface puts every received datagram on
IP input queue
Make a copy of every datagram sent to multicast or
broadcast addresses to loopback interface
Forward any IP datagram sent to one of host’s own
IP addresses to loopback interface
1/27/2005
22
Maximum Transmission Unit (MTU)
Limit on Ethernet frame size
If IP datagram is larger than MTU, IP
needs to perform fragmentation
Need to discover path MTU if
communicating across networks
1/27/2005
23
Next Class
ARP
RARP
IP
Read TI Ch. 3, 4, 5
1/27/2005
24