Download presentation
Presentation is loading. Please wait.
1
Thread per client and Java NIO
SPL – PS10 Thread per client and Java NIO
2
Overview Thread per client server Threading in C++
Java’s new IO classes
3
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.
4
Thread per client server (cont)
5
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.
6
Message encoder decoder
7
Messaging protocol Similar to the messaging protocol we saw in the last PS, this time the protocol is generic.
8
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.
9
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.
10
Threading in C++ example
11
Threading in C++ example(cont)
12
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.
13
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.
14
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:
15
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:
16
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.
17
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.
18
From channel to buffer and back
Reading from channel to a buffer: Writing from a buffer to a channel:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.