Windows环境Telnet - 访问指定端口并发送数据
1.安装 telnet客户端 控制面板--》程序--》启用或者关闭Windows功能 勾选Telnet Client
2. docs窗口输入 telnet 127.0.0 6666 回车
3. 输入 CTRL + ] 命令
4. 输入 send +需要传输的数据。
5.服务端接口数据的程序,以线程池的方式接收处理
public static void main(String[] args) throws IOException {
//1.创建一个线程池
//2.如果有客户端连接,就创建一个线程,与之通讯
ExecutorService cachedThreadPool=Executors.newCachedThreadPool();
ServerSocket serverSocket=new ServerSocket(6666);
System.out.println("服务器启动了");
while(true) {
System.out.println("等待连接");
final Socket socket=serverSocket.accept();
System.out.println("连接到一个客户端");
cachedThreadPool.execute(new Runnable() {
public void run() {
// TODO Auto-generated method stub
handler(socket);
}
});
}
}
public static void handler(Socket socket) {
byte[] bytes=new byte[1024];
try {
InputStream inputStream=socket.getInputStream();
//循环读取客户端发送的数据
while(true) {
System.out.println("read.....");
int read=inputStream.read(bytes);
if(read !=-1) {
System.out.println(Thread.currentThread().getName()+"\t"+new String(bytes,0,read));
}else {
break;
}
}
}catch(Exception e) {
e.printStackTrace();
}finally {
System.out.println("关闭和client的连接");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出
Telnet支持的命令