Netty服务器部署在Android设备上,接收来自PC客户端的Java Socket客户端发送的JSON数据

}

}).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();

}

}

private class Message {

public String data = “ping”;

public String time = “”;

我的面试宝典:一线互联网大厂Java核心面试题库

以下是我个人的一些做法,希望可以给各位提供一些帮助:

整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!

image

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

image

还有源码相关的阅读学习

image

数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

[外链图片转存中…(img-6ZYpdfYR-1721150416858)]

还有源码相关的阅读学习

[外链图片转存中…(img-NmGSUKfS-1721150416858)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值