<< Previous | Table of Categories | Next>> |
Java 7 introduces a new interface - NetworkChannel that provides common methods to all network channel classes and a new SocketOption
Blocking TCP Server
import java.io.IOException; import java.net.InetSocketAddress; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; public class BlockingTcpServer { public static void main(String[] args) { final int SERVER_PORT = 9001; final String SERVER_IP = "127.0.0.1"; ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(2); ByteBuffer outgoingBuffer = ByteBuffer.wrap("World".getBytes()); Charset charset = Charset.defaultCharset(); CharsetDecoder decoder = charset.newDecoder(); String requestMsg = ""; try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) { if (serverSocketChannel.isOpen()) { serverSocketChannel.configureBlocking(true); //set options serverSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024); serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); //bind the server socket channel to local address serverSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT)); while (true) { try (SocketChannel socketChannel = serverSocketChannel.accept()) { while (socketChannel.read(incomingBuffer) != -1) { incomingBuffer.flip(); String msgReceived = decoder.decode(incomingBuffer).toString(); System.out.println("Msg received in this loop : " + msgReceived); requestMsg = requestMsg + msgReceived; if (incomingBuffer.hasRemaining()) { incomingBuffer.compact(); } else { incomingBuffer.clear(); } } System.out.println("Request from " + socketChannel.getRemoteAddress() + " : " + requestMsg); socketChannel.write(outgoingBuffer); outgoingBuffer.flip(); } catch (IOException ex) { ex.printStackTrace(); } } } else { System.out.println("The server socket channel cannot be opened!"); } } catch (IOException ex) { System.err.println(ex); } } }Blocking TCP Client
import java.io.IOException; import java.net.InetSocketAddress; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; public class BlockingTcpClient { public static void main(String[] args) throws IOException { final int SERVER_PORT = 9001; final String SERVER_IP = "127.0.0.1"; ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(2); ByteBuffer sendingBuffer = ByteBuffer.wrap("Hello".getBytes()); Charset charset = Charset.defaultCharset(); CharsetDecoder decoder = charset.newDecoder(); String responseMsg = ""; //create a new socket channel try (SocketChannel socketChannel = SocketChannel.open()) { if (socketChannel.isOpen()) { //set the blocking mode socketChannel.configureBlocking(true); socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 1024); socketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024); socketChannel.setOption(StandardSocketOptions.SO_LINGER, 10); //establish channel connection socketChannel.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT)); if (socketChannel.isConnected()) { //sending data socketChannel.write(sendingBuffer); socketChannel.shutdownOutput(); //receving data while (socketChannel.read(receivingBuffer) != -1) { receivingBuffer.flip(); String msgReceived = decoder.decode(receivingBuffer).toString(); System.out.println("Msg received in this loop : " + msgReceived); responseMsg = responseMsg + msgReceived; if (receivingBuffer.hasRemaining()) { receivingBuffer.compact(); } else { receivingBuffer.clear(); } } System.out.println("Response from server : "+ responseMsg); } else { System.out.println("The connection cannot be established!"); } } else { System.out.println("The socket channel cannot be opened!"); } } catch (IOException ex) { System.err.println(ex); } } }
No comments:
Post a Comment