netty游戏服务器搭建之客户端
来源:互联网 发布:seo整站优化效果 编辑:程序博客网 时间:2024/05/23 02:08
由于本人没有游戏开发经验,所以用java模拟了一下游戏客户端
首先写client的主函数
public class NettyClient {
/*
* 服务器端口号
*/
private int port;
/*
* 服务器IP
*/
private String host;
public NettyClient(int port, String host)throws InterruptedException {
this.port = port;
this.host = host;
start();
}
private void start() throws InterruptedException {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.group(eventLoopGroup);
bootstrap.remoteAddress(host, port);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel)
throws Exception {
//socketChannel.pipeline().addLast(new NettyClientHandler());//客户端业务处理handler,先注释掉
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
if (future.isSuccess()) {
SocketChannel socketChannel = (SocketChannel) future.channel();
System.out.println("----------------connect server success----------------");
}
future.channel().closeFuture().sync();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
NettyClient client = new NettyClient(8012,"localhost");
}
启动函数:控制台输出
----------------connect server success----------------
服务器控制台输出:
clinet编号:f73761a3加入服务器
关闭客户端,服务器控制台输出
clinet:f73761a3 离开服务器
至此,客户端已经连上了服务器,下面写客户端的业务handler
public class NettyClientHandler extends ChannelInboundHandlerAdapter{
private ByteBuf firstMessage;
public NettyClientHandler(){
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {//向服务器发送消息
byte[] data = "服务器,给我一个APPLE".getBytes();
firstMessage=Unpooled.buffer();
firstMessage.writeBytes(data);
ctx.writeAndFlush(firstMessage);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
String rev = getMessage(buf);
System.out.println("客户端收到服务器数据:" + rev);
}
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
将之前注释的解开
socketChannel.pipeline().addLast(new NettyClientHandler());//客户端业务处理handler
再次运行client,服务器控制台输出
clinet编号:54f2b2d5加入服务器
服务端收到客户端数据:服务器,给我一个APPLE
客户端控制台输出
客户端收到服务器数据:我收到了,apple给你,succcess
- netty游戏服务器搭建之客户端
- netty游戏服务器搭建之服务端
- 基于netty的rts游戏服务器搭建
- 《从零开始搭建游戏服务器》Netty导入创建一个Socket服务器
- 《从零开始搭建游戏服务器》Netty导入创建一个Socket服务器
- netty服务器搭建-http
- spring+netty服务器搭建
- Netty搭建服务器
- java游戏服务器之网络层Netty 之EventLoop
- java游戏服务器之网络层Netty 之ChannelPipeline
- netty服务器,客户端的开发
- netty服务器,客户端的开发
- netty 实现 服务器 客户端通信
- netty源码分析之客户端
- Netty服务端和客户端搭建(入门)
- Netty 服务器客户端,发送自定义消息
- netty服务器向客户端同步请求
- 1.2.2Netty 客户端/服务器 总览
- Linux动态调频系统CPUFreq之一:概述
- Elasticsearch 中文分词器 IK 配置和使用
- 错误收集
- tbox新增stackless协程支持
- Linux 下设置环境变量
- netty游戏服务器搭建之客户端
- CodeForces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses
- exp8-2
- 在eclipse4.5版本下手动安装hibernate tools
- PHP 字符串格式化为json数据
- 老版本IE中的BUG
- Qt之格栅布局(QGridLayout)
- angular点击button切换相对页面同时按钮变颜色
- 字节、字、bit、byte的关系