import io.netty.handler.codec.json.JsonObjectDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread(new Runnable() {
@Override
public void run() {
try {
new Server();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private class Server {
private final int SERVER_PORT = 9000;
public Server() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).option(ChannelOption.SO_KEEPALIVE, true).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128).handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new JsonObjectDecoder());//JSon解码器。
sc.pipeline().addLast(new StringEncoder());// 发送字符串的编码器。
sc.pipeline().addLast(new StringDecoder());// 接收到字符串的解码器。
sc.pipeline().addLast(new MyChannelHandlerAdapter());
}
});
// 绑定端口,开始接收进来的连接。
ChannelFuture cf = b.bind(SERVER_PORT).sync();
// 等待服务器关闭Socket。
cf.channel().closeFuture().sync();
}
private class MyChannelHandlerAdapter extends ChannelHandlerAdapter {
private final Logger logger = Logger.getLogger(MyChannelHandlerAdapter.class.getName());
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(“连接激活”);
ctx.writeAndFlush(“hello,world!”); // 若没有StringEncoder,则发送不出去字符串。
System.out.println(“写入完成”);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//ByteBuf buf = (ByteBuf) msg;
logger.info(msg.toString());
//System.out.println(buf.toString(CharsetUtil.UTF_8));
//logger.info(msg.toString());
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
logger.info(“channelReadComplete”);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.log(Level.WARNING, “发生错误,关闭链接。”, cause);
ctx.close();
}
}
}
}
运行在PC电脑上的客户端,不停的发送字符串数据(转换成byte字节)。
import java.io.DataOutputStream;
import java.net.Socket;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.google.gson.Gson;
public class Client {
public Client() throws Exception {
Socket socket = new Socket(“localhost”, 6000);
System.out.println(“连接建立:” + socket.toString());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
Date date = new Date(System.currentTimeMillis());
DateFormat dateFormat = new SimpleDateFormat(“MM-dd,HH:ss:SSS”);
Message message = new Message();
Gson gson = new Gson();
System.out.println(“开始循环发一万次ping…”);
for (int i = 0; i < 10000; i++) {
date.setTime(System.currentTimeMillis());
message.time = dateFormat.format(date);
String jsonString = gson.toJson(message);
尾声
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
进阶学习视频
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
** (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-2n4YiZyS-1715678399603)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!