CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina
1/29/20042 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/20043 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()
1/29/20044 Class Thread Instance Methods void run() void setPriority(int newPriority) int getPriority() void start() void stop()
1/29/20045 Interface Runnable Method void run()
1/29/20046 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/20047 MTEchoServer.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) 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/20048 Constructor MTEchoServer protected Socket socket; MTEchoServer (Socket socket) { this.socket = socket; }
1/29/20049 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/ 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 (); }
1/29/ 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/ 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/ 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/ Ethernet Frame Dest. address Source address PreambleDataCRCType
1/29/ Ethernet Frame Demultiplexing Ethernet driver ARP IP RARP incoming frame
1/29/ 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/ 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/ 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/ Loopback Interface Used for communication between client and server on the same host, and for functionality testing Assigned the address 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/ 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/ Next Class ARP RARP IP Read TI Ch. 3, 4, 5