Socket Client 怎么连接 Netty Server

AlenCheng 2015-03-06 05:47:08
如题,想在需要用Netty做一个Server端。用来接收Socket Client发送的Request.
感觉已经连接上了,但是就是没有把请求文本打出来。不知道是怎么回事了。

Netty Server

package server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class MyNettyServer {

ChannelFuture f = null;

EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

private void run(int port) {
try {
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
// 有连接到达时会创建一个Channel
@Override
protected void initChannel(SocketChannel channel) throws Exception {
System.out.println("has a request....");
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
// 在Channel队列中,添加自己的Handler处理业务
pipeline.addLast(new HttpServerInboundHandler());
}
});

// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
f = b.bind(port).sync();
System.out.println("Server statrted ...");
// 应用程序会一直等待,直到channel关闭
f.channel().closeFuture().sync();
} catch (Exception e) {

}
}

private void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
MyNettyServer server = new MyNettyServer();
server.run(8000);
}
}



Netty handler

package server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HttpServerInboundHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("server receive message : "+ msg);
ctx.channel().writeAndFlush("server response message : " + msg + "...");
ctx.close();
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("read complete...");
ctx.close();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println("throw exption...");
ctx.close();
}
}


Socket Client

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MySocketClient {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket client=new Socket("127.0.0.1", 8000);
PrintWriter pw=new PrintWriter(client.getOutputStream());
pw.write(“hello!”);
pw.flush();
pw.close();
br.close();
}

}


运行完Client以后的结果
Server statrted ...
has a request....

说明已经连接到了Netty,但是请求的文本没有过来。
麻烦各位帮忙看下了,谢谢!
...全文
1305 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));

这四行全删除,试一下
mvp_yezi 2018-09-04
  • 打赏
  • 举报
回复
刚好遇到同样问题,handler里面读取的部分,直接上代码。客户端传的是byte,用的是8楼的思路,只是8楼没说清楚
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;

String abc = convertByteBufToString(in);
withawind 2018-08-03
  • 打赏
  • 举报
回复
引用 8 楼 qq_35023312 的回复:
应该是这几个解码方法不能有效的解释传递过来的参数:
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));

如果client传递的参数是使用类似以下方法,那应该使用ByteArrayDecoder、ByteArrayEncoder就可以了:
byte[] bytes = content.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
socketChannel.write(buffer);
socketChannel.socket().shutdownOutput();



你这客户端用的是netty 我们要求的是使用 基本的socket
huage 2017-11-24
  • 打赏
  • 举报
回复
Socket client=new Socket("127.0.0.1", 8000); PrintWriter pw=new PrintWriter(client.getOutputStream()); pw.write(“hello!”);//这里输入了流马上就关闭了,所以接收不到了 输入了等待响应了在关闭就可以了 pw.flush(); pw.close(); br.close();
xiacal 2017-06-07
  • 打赏
  • 举报
回复
楼主,请问下是怎么解决的,我传了长度,任然报同样的错
zhanghe086 2017-02-04
  • 打赏
  • 举报
回复
怎么解决的呢?难道不用netty了?
zrtlin 2017-01-18
  • 打赏
  • 举报
回复
请问怎么解决的
qq_35023312 2016-05-17
  • 打赏
  • 举报
回复
应该是这几个解码方法不能有效的解释传递过来的参数: pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); 如果client传递的参数是使用类似以下方法,那应该使用ByteArrayDecoder、ByteArrayEncoder就可以了: byte[] bytes = content.getBytes(); ByteBuffer buffer = ByteBuffer.wrap(bytes); socketChannel.write(buffer); socketChannel.socket().shutdownOutput();
b2125267 2016-03-04
  • 打赏
  • 举报
回复
楼上的好好想想,蛮简单滴。。。。。。字节流读写
lidali163com 2016-02-16
  • 打赏
  • 举报
回复
楼主怎么解决的,能否分享下
光脚快乐 2016-02-15
  • 打赏
  • 举报
回复
pw.write(“hello!”); 在这个前面需要将hello!的长度发送过去!
poiuy886 2015-12-21
  • 打赏
  • 举报
回复
楼主,最后怎么解决的,同求
wm850730498 2015-08-09
  • 打赏
  • 举报
回复
请问楼主最后是怎么解决这个问题的啊?
AlenCheng 2015-06-02
  • 打赏
  • 举报
回复
引用 1 楼 kxn308 的回复:
客户端怎么不用netty
客户那边是Socket没办法
For_Ning 2015-05-05
  • 打赏
  • 举报
回复
客户端怎么不用netty
# 基于Nettysocket server ------ ## 介绍 使用Netty分别实现了三个Socket server和一个socket client: > * server1:9099 主要用来跟硬件传感器通信 > * server2:8888/websocket 作为websocket服务端跟网页通信 > * server2:8889/websocket 跟storm服务做数据通信,同时也作为websocket服务端跟网页通信 > * client 作为模拟客户端,跟server1建立连接后,不断给服务端发送假数据 整个项目启动后,主要做了下面几件事: - [ ] 创建socket serversocket client,并建立连接 - [ ] 执行定时任务,每5秒socket server往所有连接socket client发送请求数据命令 - [ ] socket client接受到请求数据的命令后,从mysql中读取假数据,伪造成真实设备传输的数据格式,并发送给socket server - [ ] socket server接收到返回的数据后,分别写入到hbase数据库和kafka队列中 - [ ] 最后调用websocket server,往所有跟它建立的客户端发送接收到的数据 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧