1 / 17

Multiplexing/Demux

Multiplexing/Demux. Multiplexing at send host:. Demultiplexing at rcv host:. Multiplexing/demultiplexing. delivering received segments to correct socket. gathering data from multiple sockets, enveloping data with header (later used for demultiplexing). = socket. = process.

Download Presentation

Multiplexing/Demux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multiplexing/Demux

  2. Multiplexing at send host: Demultiplexing at rcv host: Multiplexing/demultiplexing delivering received segments to correct socket gathering data from multiple sockets, enveloping data with header (later used for demultiplexing) = socket = process application P4 application application P1 P2 P3 P1 transport transport transport network network network link link link physical physical physical host 3 host 2 host 1 CPSC 441 - Transport Layer

  3. host receives IP datagrams each datagram has source IP address, destination IP address each datagram carries 1 transport-layer segment each segment has source, destination port number host uses IP addresses & port numbers to direct segment to appropriate socket How demultiplexing works 32 bits source port # dest port # other header fields application data (message) TCP/UDP segment format CPSC 441 - Transport Layer

  4. Create sockets with port numbers: DatagramSocket mySocket1 = new DatagramSocket(12534); DatagramSocket mySocket2 = new DatagramSocket(12535); UDP socket identified by two-tuple: (dest IP address, dest port number) When host receives UDP segment: checks destination port number in segment directs UDP segment to socket with that port number IP datagrams with different source IP addresses and/or source port numbers directed to same socket Connectionless demultiplexing CPSC 441 - Transport Layer

  5. P3 P2 P1 P1 SP: 9157 client IP: A DP: 6428 Client IP:B server IP: C SP: 5775 SP: 6428 SP: 6428 DP: 6428 DP: 9157 DP: 5775 Connectionless demux (cont) DatagramSocket serverSocket = new DatagramSocket(6428); SP provides “return address” CPSC 441 - Transport Layer

  6. TCP socket identified by 4-tuple: source IP address source port number dest IP address dest port number recv host uses all four values to direct segment to appropriate socket Server host may support many simultaneous TCP sockets: each socket identified by its own 4-tuple Web servers have different sockets for each connecting client non-persistent HTTP will have different socket for each request Connection-oriented demux CPSC 441 - Transport Layer

  7. SP: 9157 SP: 5775 P1 P1 P2 P4 P3 P6 P5 client IP: A DP: 80 DP: 80 Connection-oriented demux (cont) S-IP: B D-IP:C SP: 9157 DP: 80 Client IP:B server IP: C S-IP: A S-IP: B D-IP:C D-IP:C CPSC 441 - Transport Layer

  8. SP: 9157 SP: 5775 P1 P1 P2 P3 client IP: A DP: 80 DP: 80 Connection-oriented demux: Threaded Web Server P4 S-IP: B D-IP:C SP: 9157 DP: 80 Client IP:B server IP: C S-IP: A S-IP: B D-IP:C D-IP:C CPSC 441 - Transport Layer

  9. Java Threads

  10. Introduction • TCP echo server shown in earlier tutorial handles one client at a time. • This server is known as iterative server. • Iterative servers handle clients sequentially, finishing with one client before servicing the next. • Java threads: Helps servers handle many client simultaneously.

  11. Threading Mechanisms • Create a class that extends the Thread class • Create a class that implements the Runnable interface Thread Runnable Thread MyThread MyClass (objects are threads) (objects with run() body) [a] [b]

  12. 1st method: Extending Thread class • Create a class by extending Thread class and override run() method: class MyThread extends Thread { public void run() { // thread body of execution } } • Create a thread: MyThread thr1 = new MyThread(); • Start Execution of threads: thr1.start(); • Create and Execute: new MyThread().start();

  13. Template class MyThread extends Thread { public void run() { System.out.println(" this thread is running ... "); } } class ThreadEx1 { public static void main(String [] args ) { MyThread t = new MyThread(); t.start(); } }

  14. 2nd method: Threads by implementing Runnable interface • Create a class that implements the interface Runnable and override run() method: class MyThread implements Runnable { ..... public void run() { // thread body of execution } } • Creating Object: MyThread myObject = new MyThread(); • Creating Thread Object: Thread thr1 = new Thread( myObject ); • Start Execution: thr1.start();

  15. Template class MyThread implements Runnable { public void run() { System.out.println(" this thread is running ... "); } } class ThreadEx2 { public static void main(String [] args ) { Thread t = new Thread(new MyThread()); t.start(); } }

  16. Example of Java Threads ThreadExample implements Runnable interface; it can be passed to the constructor of Thread public class ThreadExample implements Runnable { private String greeting; public ThreadExample(String greeting) { this.greeting = greeting; } public void run( ) { while(true) { System.out.println(Thread.currentThread( ).getName( ) + ": "+greeting); try { TimeUnit.MILLISECONDS.sleep(((long) Math.random( ) * 100)); } catch(InterruptedException e) { } } } public static void main(String [ ] args) { new Thread(new ThreadExample(“Greeting 1")).start( ); new Thread(new ThreadExample(“Greeting 2")).start( ); new Thread(new ThreadExample(“Greeting 3")).start( ); } } Each instance of ThreadExample contains own greeting string Returns reference to current thread Returns name of thread as string Suspend the thread; Thread sleeps for random amount of time. Create new instance of ThreadExample with different greeting Passes new instance to the constructor of Thread. Calls new Thread instance's start() Each thread independently executes run() of ThreadExample, while the main thread terminates. Upon execution an interleaving of three greeting messages is printed

  17. Multithreaded Java Server public void run() { try { PrintStream pstream = new PrintStream(csocket.getOutputStream()); for (int i = 100; i >= 0; i--) { pstream.println(i + " counts"); } pstream.close(); csocket.close(); } catch (IOException e) { System.out.println(e); } } } import java.io.*; import java.net.*; public class MultiThreadServer implements Runnable { Socket csocket; MultiThreadServer(Socket csocket) { this.csocket = csocket; } public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); while (true) { Socket sock = ssock.accept(); System.out.println("Connected"); new Thread(new MultiThreadServer(sock)).start(); } }

More Related