在项目中,通过netty框架搭建的服务器。但是在Android端我想直接采用Socket与服务器通信。但是在这个过程中遇到两个问题。
1.服务器接收不到数据
解决方法:我发送的形式是直接将字符串转成字节流,按照tcp协议进行发送的。一开始服务器能看到客户端接入了,但是就是收不到消息。后来在字符串的末尾加上了'\n'换行符就行了。
2.服务器发送数据时,客户端接收的数据是一串奇怪的字符串。例如:[@Bd31d456
解决方法:在服务器发送数据时,一开始是直接ctx.writeAndFlush(s.getbyte()),后来改了很多的编码设置之类的都没起作用。后来在一个知乎的回答里面看到按这样的方式ctx.writeAndFlush(Unpooled.copiedBuffer(s.getBytes()))发送,就可以正常接收了。
对于netty的使用不是很熟悉,只是按照视频里的步骤粗略的写了一下,所以遇到问题就一直不知道怎么解决。