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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • bat全屏运行_cmd指令打开全屏

    bat全屏运行_cmd指令打开全屏新建1.vbsSetobjShell=CreateObject(“Wscript.Shell”)objShell.SendKeys”{F11}”Setxxx用于指示一个未初始化的对象值,或者把对象变量从对象分离用于释放系统资源。新建test.bat@echooffstarthttps://blog.csdn.net/qq_44275213choice/t1/dy/n>nulstart%cd%\1.vbs…

    2022年9月2日
    3
  • 个人总结 – JS逆向解析[通俗易懂]

    个人总结 – JS逆向解析[通俗易懂]目前加密的方式总结有下面几点: 对称加密(加密解密密钥相同):DES、DES3、AES 非对称加密(分公钥私钥):RSA 信息摘要算法/签名算法:MD5、HMAC、SHA 前端实际使用中MD5、AES、RSA,自定义加密函数使用频率是最高的 几种加密方式配合次序:采用非对称加密算法管理对称算法的密钥,然后用对称加密算法加密数据,用签名算法生成非对称加密…

    2022年6月19日
    106
  • JS中的几种循环和跳出方式

    JS中的几种循环和跳出方式JS中的循环是大家很常用的,这里总结一下几种常用循环的跳出方式。1.for循环vararr=[‘q’,’w’,’e’,’r’,’t’];for(vari=0,len=arr.length;i<len;i++){console.log(arr[i]);}//q,w,e,r,t跳出本次循环continue:f…

    2022年6月3日
    35
  • synchronousqueue场景_SynchronousQueue原理解析

    synchronousqueue场景_SynchronousQueue原理解析经典的生产者-消费者模式,操作流程是这样的:有多个生产者,可以并发生产产品,把产品置入队列中,如果队列满了,生产者就会阻塞;有多个消费者,并发从队列中获取产品,如果队列空了,消费者就会阻塞;image.pngSynchronousQueue也是一个队列来的,但它的特别之处在于它内部没有容器,一个生产线程,当它生产产品(即put的时候),如果当前没有人想要消费产品(即当前没有线程执行take),此…

    2022年6月22日
    22
  • anaconda和python版本对照表

    anaconda和python版本对照表python2 python3 anaconda2/3 2.7.14 3.6.5 5.2.0 2.7.14 3.6.4 5.1.0 2.7.14 3.6.3 5.0.1 2.7.13 3.6.2 5.0.0 2.7.13 3.6.1 4.4.0 2.7.13 3.6.0 4.3.1 2….

    2022年5月28日
    484
  • xxxxxxxxxxxxxxxxxxxxxxxxxxxx

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxGetAuthorizationcode:Request:https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https%3A%2

    2022年7月2日
    48

发表回复

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

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