project0( - university of washington · 2015-04-10 · udp((user(datagram(protocol)(•...
TRANSCRIPT
![Page 1: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/1.jpg)
Project 0
Danyang Zhuo CSE 461 15sp Sec;on #2
![Page 2: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/2.jpg)
Proj0
• You have to implement: – Mul;-‐Thread UDP Server – Mul;-‐Thread UDP Client – Async IO UDP Server – Async IO UDP Client
• Your server must support mul;ple clients
![Page 3: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/3.jpg)
Submit Format
• Use any programming language you like – Tips: TAs are familiar with C/C++, Java, Python
• Must write scripts to make tes;ng generic: – Server:
• ./server <portnum>
– Client: • ./client <hostname> <portnum>
![Page 4: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/4.jpg)
UDP (User Datagram Protocol)
• Communica;on Protocol on the top of Internet Protocol – Use IP address and port as a iden;fier
• Server: – Bind to a port
• Client: – Connect through Server’s IP address and port
• Send/Receive
![Page 5: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/5.jpg)
Server
//Bind to a port (5533): DatagramSocket socket = new DatagramSocket(5533); // Receive Message byte[] buf = new byte[256]; DatagramPacket p = new DatagramPacket(buf, buf.length); socket.receive(p);
![Page 6: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/6.jpg)
Client
// Connect to server DatagramSocket socket = new DatagramSocket(); socket.connect(InetAddress.getByName("127.0.0.1"), 5533); // Send Message (byte[] buffer) DatagramPacket p = new DatagramPacket(buffer, buffer.length); socket.send(p);
![Page 7: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/7.jpg)
Demo
• UDP Server • UDP Client
![Page 8: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/8.jpg)
Interac;ve Client/Server
• Use one thread to listen to user command • Use another thread to send/receive message
![Page 9: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/9.jpg)
Demo
• Interac;ve UDP Server
![Page 10: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/10.jpg)
Mul;-‐Thread Server Session 1 Thread
Session 2 Thread
Session 3 Thread
Session 4 Thread
UDP Listening Thread
Packet Session2
Packet Session4
Packet Session2
Packet Session4
![Page 11: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/11.jpg)
Mul;-‐thread Server
• Easy to reason – Each thread handles a session – All the logic for a given session is in one thread
• High overhead – Each thread to handle the current incoming packet can be blocked by other threads
– High memory u;liza;on
![Page 12: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/12.jpg)
Mul;-‐thread Server Session 1 Thread
Session 2 Thread
Session 3 Thread
Session 4 Thread
UDP Listening Thread
Packet Session2
Packet Session2
Ac;ve Thread
![Page 13: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/13.jpg)
Mul;-‐thread Server Session 1 Thread
Session 2 Thread
Session 3 Thread
Session 4 Thread
UDP Listening Thread
Packet Session2
Packet Session2
Ac;ve Thread
![Page 14: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/14.jpg)
Mul;-‐thread Server Session 1 Thread
Session 2 Thread
Session 3 Thread
Session 4 Thread
UDP Listening Thread
Packet Session2
Packet Session2
Ac;ve Thread May be blocked by other threads for a long ;me
![Page 15: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/15.jpg)
ConcurrentLinkedQueue
• UDP listening thread is wri;ng to a data structure that session thread is reading from
• Race condi;on may happen • Java has a race free implementa;on of a queue
![Page 16: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/16.jpg)
Non-‐blocking IO
Session 1 Session 2 Session 3 Session 4
UDP Listening Thread
Packet Session2
Packet Session4
Update data structure
![Page 17: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/17.jpg)
Why is this design block-‐free?
• Server is always processing incoming packet rather than wai;ng for the correct thread to wake up
• High performance • Slightly harder to reason
![Page 18: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/18.jpg)
Demo
• Non-‐blocking Server
• You non-‐blocking server and client s;ll should have 2 threads because 1 thread is handling user input
![Page 19: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/19.jpg)
Tips
• Use a shared Object (Java class) implementa;on for p0p packet format for both server and client
• ByteBuffer is easier to use than byte[] • TimerTask for ;meout implementa;on
• Start Early!!! • Ask Ques;ons!!
![Page 20: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/20.jpg)
Reference
• Java: – hop://courses.cs.washington.edu/courses/cse461/15wi/lectures/networkProgrammingStyle/javaNIO/
• Node.js – hop://courses.cs.washington.edu/courses/cse461/15wi/lectures/networkProgrammingStyle/
• Python – hop://courses.cs.washington.edu/courses/cse461/15wi/scripts/
![Page 21: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/21.jpg)
Sample Code – UDP Server DatagramSocket socket = new DatagramSocket(5533); while (true) { byte[] buf = new byte[256]; DatagramPacket p = new DatagramPacket(buf, buf.length); socket.receive(p); String string = new String(buf); System.out.println(string); if (string.contentEquals("BYE")) { break; } } socket.close();
![Page 22: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/22.jpg)
Sample Code – UDP Client
String msg = args[0]; DatagramSocket socket = new DatagramSocket(); socket.connect(InetAddress.getByName("127.0.0.1"), 5533); ByteBuffer buffer = ByteBuffer.allocate(256); buffer.asCharBuffer().put(msg); DatagramPacket p = new DatagramPacket(buffer.array(), buffer.array().length); socket.send(p); socket.close();
![Page 23: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/23.jpg)
User Command Thread sta;c class InputWatcher extends Thread { @Override public void run() { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String msg; try { while((msg = reader.readLine()) != null && msg.length() != 0) { System.out.println("You just typed: " + msg); } } catch (IOExcep;on e) { // A Java programmer would know what to do here... } System.exit(0); } }
![Page 24: Project0( - University of Washington · 2015-04-10 · UDP((User(Datagram(Protocol)(• Communicaon(Protocol(on(the(top(of(InternetProtocol(– Use(IP(address(and(portas(aiden;fier(•](https://reader034.vdocument.in/reader034/viewer/2022050311/5f73d86e8c8afa5fac1a6cd4/html5/thumbnails/24.jpg)
Async IO Selector selector = Selector.open(); DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(false); channel.socket().bind(new InetSocketAddress(5533)); channel.register(selector, Selec;onKey.OP_READ); while (true) { selector.select(); Iterator<Selec;onKey> keyIter = selector.selectedKeys().iterator(); while (keyIter.hasNext()) { Selec;onKey key = keyIter.next(); if (key.isReadable()) { handle(channel); } keyIter.remove(); } }