How are java's standard networking API different from netty?

Viewed 48

So im trying to figure out how java's standard networking work different from netty's.

public TCPClientDemo() throws Exception {

    Socket clientSocket = new Socket("127.0.0.1", 9234);
    String serverHostName = "localhost";

    PrintStream printStream = new PrintStream(clientSocket.getOutputStream());
    InputStreamReader inputStream = new InputStreamReader(clientSocket.getInputStream());
    BufferedReader reader = new BufferedReader(inputStream);

    String message = "Message \n";

    printStream.print(message);

    System.out.println("Message sent");

    message = reader.readLine();
    System.out.println("Message received from server: " + message);

}

Here above i have a simple TCP client written from java's default networking framework. Since this client works in a synchronous (IO) fashion will the printStream.print(message); method block our current thread, meaning that println("Message sent") will not print until our message has actually been sent?

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
    System.out.println("Message sent");
}

The above example shows how we send in asynchronous fashion in netty. Now here i would assume that ctx.writeAndFlush() might actually have not sent the message even if we reach println("Message sent") or even returned the channelActive() method because of asynchrony? So this essentially would be the opposite of the first example where our code will not continue until the message has actually been sent?

0 Answers
Related