Thread per client and Java NIO

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Java Network Programming Vishnuvardhan.M. Dept. of Computer Science - SSBN Java Overview Object-oriented Developed with the network in mind Built-in exception.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Andrew Borg. The University of York. 1 Java NIO Andrew Borg.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
System Programming Practical session 12 Reactor.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
VSP Video Station Protocol Presented by : Mittelman Dana Ben-Hamo Revital Ariel Tal Instructor : Sela Guy Presented by : Mittelman Dana Ben-Hamo Revital.
Fundamentals of Python: From First Programs Through Data Structures
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Practical Session 11 Multi Client-Server Java NIO.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Apache Mina Dima Ionut Daniel. Contents What is Apache Mina? Why Apache Mina? Mina Architecture Mina Core Mina Advanced JMX Support Spring Integration.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Practical Session 11 Multi Client-Server Java NIO.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
Java Server Programming Web Interface for Java Programs.
Remote Method Invocation by James Hunt, Joel Dominic, and Adam Mcculloch.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
1 Network Communications A Brief Introduction. 2 Network Communications.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
Object-Orientated Analysis, Design and Programming
The Object-Oriented Thought Process Chapter 14
Threaded Programming in Python
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Cross Platform Development using Software Matrix
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Netty.
Application Protocols
Java Byte IPC: Part 6-Summary
Implementation CAN Communication Engine
Lecture 11 Socket Programming.
Reactor Design Pattern
Clients and Servers 19-Nov-18.
The command invocation protocol
Clients and Servers 1-Dec-18.
Threaded Programming in Python
.Net Sockets.
NETWORK PROGRAMMING CNET 441
An Introduction to Internetworking
NETWORK PROGRAMMING CNET 441
Chapter 4: Threads.
The command invocation protocol
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Exceptions and networking
More concurrency issues
The reactor design pattern
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Threads and concurrency / Safety
Presentation transcript:

Thread per client and Java NIO SPL – PS10 Thread per client and Java NIO

Overview Thread per client server Threading in C++ Java’s new IO classes

Thread per client server In the previous practical session we saw a simple server implementation in Java. Recall that reading and writing into sockets, as well as awaiting new connections on the server side, block the thread execution. A simple solution to this problem is to create a thread for each client.

Thread per client server (cont)

Message encoder decoder The message encoder decoder interface is responsible for encoding and decoding objects to be sent and received by our server. The messages are sent and received as bytes.

Message encoder decoder

Messaging protocol Similar to the messaging protocol we saw in the last PS, this time the protocol is generic.

Blocking Connection Handler The Blocking connection handler is a runnable which is tasked with handling a connection to a single client in the server. It has his own Message encoder decoder, his own instance of the protocol, and his own socket. In the TPC, each non-blocking connection handler run on it’s own thread.

Threading in C++ There is no runnable interface in C++ Assigning tasks to threads is done by giving them a pointer to the function they should run. Locking can be done using std::mutex and std::lock_guard.

Threading in C++ example

Threading in C++ example(cont)

Java NIO In the client and server implementation we saw we used blocking IO. For example, in this line in the client: The client is blocked until the server sent a response. We want a non-blocking IO, so that the network buffer will keep working, even when we get to the above code. Java’s java.nio package is a new, efficient IO package. It also supports Non-blocking IO.

Channels A channel is something you can read to, or write from. Channels can be either blocking (by default) or non-blocking. We will discuss SocketChannel, and ServerSocketChannel, which are like Socket and ServerSocket, but their read() , write() , and accept() can be non-blocking. The ServerSocketChannel’s accept() returns a SocketChannel.

Channels example Setting up a non-blocking ServerSocketChannel listening on a specific port is done as follows: Setting up a non-blocking SocketChannel and connecting to a server is done as follows:

Buffers All the IO in NIO goes through buffers. Channels know how to read and write into buffers, and buffers can read and write into other buffers. We’ll discuss ByteBuffer, this buffer holds bytes. We can create a new ByteBuffer in the following manner: We can also create a ByteBuffer from an array of bytes:

Flipping Each buffer has a current position, limit, and capacity. The capacity is the maximum amount of data stored in the buffer. The limit is the amount of data actually stored in the buffer. The position is where you currently read/write from the buffer. The following inequality always holds: 0<=position<=limit<=capacity A read operation reads an amount of bytes from the buffer and updates the current position. Likewise, a write operation writes an amount of bytes to the buffer and updates the current position.

Flipping (cont) If someone wrote into the buffer, and you want to read those bytes, you need to set the position marker to the start of the buffer, and the limit to the current position. This is what the buffer flip() action does.

From channel to buffer and back Reading from channel to a buffer: Writing from a buffer to a channel: