This time, I will briefly summarize how to use SocketChannel. Please refer to the flow as follows.
--Client
- Connect the server via SocketChannel
- Prepare a data buffer and write / read to the server
- Close Socket Channel
--Server
- Bind iP address and port
- Get an instance of SocketChannel with the ServerSocketChannelImpl.accept () method
- Prepare a data buffer and write / read to the client
- Close SocketChannel and ServerSocketChannel
Then, let's make a communication demo of the client and the server from here.
The source code is as follows.
package socketchannel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class WebClient {
public static void main(String[] args) throws IOException {
//1. Let's create a SocketChannel instance with the SocketChannel open () method SocketChannel socketChannel = SocketChannel.open();
// 2. Connect to the server socketChannel.connect(new InetSocketAddress("127.0.0.1", 3333));
// 3. Prepare the data to be sent to the server ByteBuffer writeBuffer = ByteBuffer.allocate(128); writeBuffer.put("hello WebServer this is from WebClient".getBytes()); writeBuffer.flip(); socketChannel.write(writeBuffer); ByteBuffer readBuffer = ByteBuffer.allocate(128); socketChannel.read(readBuffer); StringBuilder stringBuffer=new StringBuilder();
// 4. Receive data from the server readBuffer.flip(); while (readBuffer.hasRemaining()) { stringBuffer.append((char) readBuffer.get()); } System.out.println ("Message from server:" + stringBuffer); socketChannel.close(); } }
The source code on the server side is as follows.
package socketchannel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class WebServer {
public static void main(String args[]) throws IOException {
try {
// 1. Create an instance of ServerSocketChannel with the open () method of ServerSocketChannel ServerSocketChannel ssc = ServerSocketChannel.open();
//2. Bind ip and port ssc.socket().bind(new InetSocketAddress("127.0.0.1", 3333));
//ServerSocketChannelImplのaccept()メソッドでSocketChannelインスタンスをもらってクライアントへの書き読み SocketChannel socketChannel = ssc.accept();
// 3. Prepare the data to export ByteBuffer writeBuffer = ByteBuffer.allocate(128); writeBuffer.put("hello WebClient this is from WebServer".getBytes()); writeBuffer.flip(); socketChannel.write(writeBuffer); ByteBuffer readBuffer = ByteBuffer.allocate(128);
////4. Prepare the data to read socketChannel.read(readBuffer); StringBuilder stringBuffer=new StringBuilder();
readBuffer.flip();
while (readBuffer.hasRemaining()) {
stringBuffer.append((char) readBuffer.get());
}
System.out.println ("Receive data from client:" + stringBuffer); socketChannel.close(); ssc.close(); } catch (IOException e) { e.printStackTrace(); } } }
Well, if you run the above demo and it works, the result will be:
Thank you for reading to the end.
Recommended Posts