组合键 ctrl+] ,进入命令行模式,发送行数据
测试
服务端
package test.nio;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class OioServer {
public static void main(String[] args) throws IOException {
// 创建 Socket 服务, 监听10101端口
ServerSocket server = new ServerSocket(10101);
System.out.println("服务器启动!");
while (true) {
// 获取一个套接字(阻塞1)
final Socket socket = server.accept();
System.out.println("来了一个新客户端!");
// 业务处理
handler(socket);
}
}
/** * 读取数据 */
private static void handler(Socket socket) {
try {
byte[] bytes = new byte[1024];
InputStream in = socket.getInputStream() ;
while (true) {
// 读取数据(阻塞2)
int read = in.read(bytes);
if (read != -1) {
System.out.println(new String(bytes, 0, read));
}else {
break ;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
System.out.println("socket关闭");
socket.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端