Download presentation
Presentation is loading. Please wait.
Published byEmil Dalton Modified over 9 years ago
1
CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina
2
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
3
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()
4
1/29/20044 Class Thread Instance Methods void run() void setPriority(int newPriority) int getPriority() void start() void stop()
5
1/29/20045 Interface Runnable Method void run()
6
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
7
1/29/20047 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/29/20048 Constructor MTEchoServer protected Socket socket; MTEchoServer (Socket socket) { this.socket = socket; }
9
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) { }
10
1/29/200410 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/29/200411 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/29/200412 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/29/200413 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/29/200414 Ethernet Frame Dest. address Source address PreambleDataCRCType 662446-15008
15
1/29/200415 Ethernet Frame Demultiplexing Ethernet driver ARP IP RARP incoming frame
16
1/29/200416 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/29/200417 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/29/200418 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
19
1/29/200419 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
20
1/29/200420 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
21
1/29/200421 Next Class ARP RARP IP Read TI Ch. 3, 4, 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.