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/29/2004
2
Class Thread
Constructors
Thread(String name)
Thread(Runnable target)
Static Methods
static
static
static
static
1/29/2004
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/29/2004
4
Interface Runnable
Method
void run()
1/29/2004
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/29/2004
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/29/2004
7
Constructor MTEchoServer
protected Socket socket;
MTEchoServer (Socket socket) {
this.socket = socket;
}
1/29/2004
8
Method run
public void run () {
try {
InputStream in = socket.getInputStream ();
OutputStream out = socket.getOutputStream ();
out.write ("Welcome to the multithreaded echo server.\r\n"
.getBytes ("latin1"));
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/29/2004
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/29/2004
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/29/2004
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/29/2004
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/29/2004
13
Ethernet Frame
Preamble
8
1/29/2004
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/29/2004
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/29/2004
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/29/2004
17
Point-to-Point Protocol (PPP)

Three components




High-level data link control protocol (HDLC)
Link control protocol (LCP)
Network control protocol (NCP)
Fix deficiencies in SLIP
1/29/2004
18
Loopback Interface


Used for communication between client and server
on the same host, and for functionality testing
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 address to loopback interface
1/29/2004
19
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/29/2004
20
Next Class




ARP
RARP
IP
Read TI Ch. 3, 4, 5
1/29/2004
21