netty通信框架_王国风云2控制台代码

netty通信框架_王国风云2控制台代码写在前面所属章节链接第二章2.1BIO通信aaaTimeServer代码importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;publicclassTimeServer{ publicstaticvoidmain(String[]args)throwsIOException{ intport=8081; if(args

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

写在前面

用我的双手成就你的梦想。记录Netty权威指南Demo代码并分享出来,帮助读者快速完成Demo。

所属章节 链接
第二章2.1BIO通信 代码链接

TimeServer代码

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TimeServer {
	public static void main(String[] args) throws IOException {
		int port = 8081;
		if (args != null && args.length > 0) {
			try {
				port = Integer.valueOf(args[0]);
			} catch (NumberFormatException e) {

			}
		}
		ServerSocket server = null;
		try {
			server = new ServerSocket(port);
			System.out.println("The time server is start in port : " + port);
			Socket socket = null;
			while (true) {
				socket = server.accept();
				new Thread(new TimeServerHandler(socket)).start();
			}
		} finally {
			if (server != null) {
				System.out.println("The time server close");
				server.close();
				server = null;
			}
		}
	}
}

TimeServerHandler代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TimeServerHandler implements Runnable {
	
	private  Socket socket;
	
	public TimeServerHandler(Socket socket) {
		this.socket = socket;
	}
	
	public void run() {
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
			String currentTime = null;
			String body = null;
			while (true) {
				body = in.readLine();
				if (body == null) {
					break;
				}
				System.out.println("The time server receive order : " + body);
				currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(System.currentTimeMillis()).toString() : "BAD ORDER";
				out.println(currentTime);
			}
		} catch (Exception e) {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if (out != null) {
				out.close();
				out = null;
			}
			if (this.socket != null) {
				try {
					this.socket.close();
					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				this.socket = null;
			}
		}
	}
}

TimeClient代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TimeClient {

	public static void main(String[] args) {
		int port = 8081;
		if (args != null && args.length > 0) {
			try {
				port = Integer.valueOf(args[0]);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		Socket socket = null;
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			socket = new Socket("127.0.0.1", port);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
			out.println("QUERY TIME ORDER");
			System.out.println("Send order 2 server succeed.");
			String resp = in.readLine();
			System.out.println("Now is : " + resp);
		} catch (Exception e) {
			
		} finally {
			if (out != null) {
				out.close();
				out = null;
			}
			
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				in = null;
			}
			
			if (socket != null) {
				try {
					socket.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				socket = null;
			}
		}
	}

}

说明

代码中的package要自行加入,运行结果与书本一致。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186848.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 安捷伦示波器使用说明书_安捷伦labview采集

    安捷伦示波器使用说明书_安捷伦labview采集Step1:配置VISA协议。VISA协议是AgilentIOLibraries的一部分,主要包含一个虚拟仪器软件架构VISA(一个比较通用的工业仪器软件架构)和标准控制库SICL.我理解前者相当一个底层架构,后者相当于一个指令集.先配置好VISA,然后通过SICL指令集发命令。直接上例子,简单明了。agilent示波器可以直接用VISA,所以只要在C++项目里进行配置。首先在C++里配…

    2022年10月12日
    0
  • idea正版在线激活码破解方法「建议收藏」

    idea正版在线激活码破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    144
  • 查询数据库空间使用情况的函数_查看当前数据库

    查询数据库空间使用情况的函数_查看当前数据库sp_spaceused[[@objname=]'objname'][,[@updateusage=]'updateusage'][@objname

    2022年8月3日
    9
  • postman安装使用教程(标贝科技)「建议收藏」

    postman安装使用教程(标贝科技)「建议收藏」postman安装使用教程文章目录postman安装使用教程前言一、postman安装二、postman使用前言postman是Chrome浏览器的插件,是一款功能强大的网页调试工具(接口调试神器)一、postman安装1.下载:https://www.postman.com/downloads/2.安装双击postman应用程序进入到postman主界面,如下证明安装成功3.界面主要功能介绍二、postman使用1.状态码解释各位小伙伴可以学习一下关于接口

    2022年9月18日
    3
  • Java基础-遍历数组

    Java基础-遍历数组Java基础-遍历数组1、语法简介2、一维数组3、二维数组4、三维数组1、语法简介在Java中,对for语句的功能给予了扩充、加强,以便更好的遍历数组。语法格式如下:for(声明循环变量:数组的名字){ ………}其中,声明的循环变量的类型必须与数组类型相同。2、一维数组代码:packageThroughArray;//遍历一维数组publicclassOneDimensionalArray{publicstaticvoidmain(String

    2022年9月19日
    3
  • 列文伯格算法_最短路径matlab程序

    列文伯格算法_最短路径matlab程序  本系列文章主要介绍基于A*算法的路径规划的实现,并使用MATLAB进行仿真演示。  一、A*算法简介    A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法。算法中的距离估算值与实际值越接近,最终搜索速度越快。    公式表示为:f(n)=g(n)+h(n),其中,f(n)是从初始状态经由状态n到目标状态的代价估计,g(n)是在状态空间中从初始状态到状态n的实际代价,h(n)是从状态n到目标状态的最佳路径的估计代价。

    2022年10月7日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号