Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."— Presentation transcript:

1 CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

2 1/27/20052 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

3 1/27/20053 Class Thread Constructors Thread(String name) Thread(Runnable target) Static Methods static int activeCount() static Thread currentThread() static void sleep(long millis) throws InterruptedException static void yield()

4 1/27/20054 Class Thread Instance Methods void run() void setPriority(int newPriority) int getPriority() void start() void stop()

5 1/27/20055 Interface Runnable Method void run()

6 1/27/20056 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

7 1/27/20057 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 … }

8 1/27/20058 Constructor MTEchoServer protected Socket socket; MTEchoServer (Socket socket) { this.socket = socket; }

9 1/27/20059 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) { }

10 1/27/200510 Method main public static void main (String[] args) throws IOException { if (args.length != 1) throw new IllegalArgumentException ("Syntax: MTEchoServer "); 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 (); }

11 1/27/200511 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

12 1/27/200512 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

13 1/27/200513 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

14 1/27/200514 Ethernet Frame Dest. address Source address PreambleDataCRCType 662446-15008

15 1/27/200515 Ethernet Frame Demultiplexing Ethernet driver ARP IP RARP incoming frame

16 1/27/200516 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

17 1/27/200517 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

18 1/27/200518 Point-to-Point Protocol (PPP) Three components Framing mechanism Link control protocol (LCP) Network control protocol (NCP) Fix deficiencies in SLIP

19 1/27/200519 Requirements of PPP Packet framing Transparency Multiple network-layer protocols Multiple types of links Error detection Connection liveness Network-layer address negotiation Simplicity

20 1/27/200520 PPP Data Framing 01111110Data Variable length 1 Flag 11111111 1 Address 00000011 1 Control Protocol 1 or 2 01111110 1 Flag Check 2 or 4

21 1/27/200521 PPP Byte Stuffing When flag pattern appears in data, PPP stuffs a control escape byte before it PPP b1 01111110 b2 b4 b5 PPP b5 01111110 b4 b2 b1 b20111110101111110b4b5

22 1/27/200522 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

23 1/27/200523 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

24 1/27/200524 Next Class ARP RARP IP Read TI Ch. 3, 4, 5


Download ppt "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."

Similar presentations


Ads by Google