Giter VIP home page Giter VIP logo

java12-nio-blocking-client-server-workshop's Introduction

Build Status License: GPL v3

java12-nio-blocking-client-server-workshop

Reference: https://medium.com/coderscorner/tale-of-client-server-and-socket-a6ef54a74763
Reference: https://www.amazon.com/Java-Nio-Ron-Hitchens/dp/0596002882

project description

  • please refer firstly https://github.com/mtumilowicz/java12-io-socket-client-server-workshop
    • the task is to rewrite SingleThreadedServerAnswer and ClientConnectionAnswer using NIO
  • the main goal of this project is to show how to implement blocking server using java NIO
  • in the workshop we will try to fix failing tests from test/workshop package by following steps and hints in java/workshop package
  • answers: java/answers package

theory in a nutshell

  • Java NIO - Java New IO (not Java Non-blocking IO)
  • channels are a new, first-class Java I/O paradigm
  • channel is a conduit that transports data efficiently between byte buffers and the entity on the other end of the channel
    • you can think of the buffer as a temporary storage place
    • channel can be viewed as an endpoint for communication
      • for example if we take SocketChannel class, it reads from and writes to TCP sockets
      • data must be encoded in ByteBuffer objects for reading and writing
  • channel implementations vary radically between operating systems
  • I/O falls into two categories: file I/O and stream I/O
    • so there are two types of channels: file and socket
  • socket channels are blocking by default
  • socket channels can operate in nonblocking mode and are selectable (will be discussed in details in the other workshops)
  • a good metaphor for a channel is a pneumatic tube, the type used at drive-up bank-teller windows
    • your paycheck would be the information you're sending
    • the carrier would be like a buffer
    • you fill the buffer (place your paycheck in the carrier), "write" the buffer to the channel (drop the carrier into the tube)
    • the payload is carried to the I/O service (bank teller) on the other end of the channel
    • the response would be the teller filling the buffer (placing your receipt in the carrier) and starting a channel transfer in the opposite direction (dropping the carrier back into the tube)
    • the carrier arrives on your end of the channel (a filled buffer is ready for you to examine)
    • you then flip the buffer (open the lid) and drain it (remove your receipt)
    • you drive away and the next object (bank customer) is ready to repeat the process using the same carrier (Buffer) and tube (Channel) objects
  • vs io.sockets
    • in NIO based systems, instead of writing data onto output streams and reading data from input streams, we read and write data from buffers
    • instead of asking for socket’s input and output streams, we are going to write data to the channel itself
  • SocketChannel.socket() - retrieves a socket associated with this channel

conclusions in a nutshell

  • SocketChannel - client side of the connection (~ Socket if blocking)
  • ServerSocketChannel - server side of the connection (~ ServerSocket if blocking)

java12-nio-blocking-client-server-workshop's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.