package io.torey.niotest;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.util.Scanner;/** * NIO客户端 */public class NioClient { /** * 启动 */ public void start() throws IOException { //连接服务器端 //向服务器端发送数据 //接收服务器端相应 //连接服务器端 SocketChannel socketChannel = SocketChannel.open( new InetSocketAddress("127.0.0.1", 8000)); //向服务器端发送数据 Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String request = scanner.nextLine(); if (null!= request&&request.length()>0) { socketChannel.write(Charset.forName("UTF-8").encode(request)); } } //接收服务器端相应 } public static void main(String[] args){ try { NioClient nioClient = new NioClient(); nioClient.start(); } catch (IOException e) { e.printStackTrace(); } }}
这时候就可以开启服务器端和客户端,发送一下消息了

