}
}).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);
System.out.println(jsonString);
dos.write(jsonString.getBytes(“UTF-8”));
dos.flush();
Thread.sleep(1000);
}
System.out.println(“循环一万次发ping结束”);
socket.close();
}
public static void main(String[] args) {
try {
new Client();
} catch (Exception e) {
e.printStackTrace();
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
所以只是把题目列出来了
[外链图片转存中…(img-obyZI79C-1713397975638)]
[外链图片转存中…(img-WyxAT6bz-1713397975638)]
[外链图片转存中…(img-HPAwJPDy-1713397975640)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!