[英]Java Communication netty server - Socket client
For a chat server project I use netty as server, with the following code in my handler : 对于聊天服务器项目,我将netty用作服务器,并在处理程序中添加以下代码:
public class PacketHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
AbstractClientPacket packet = ClientPacketHandler.handle(in);
if(packet != null && packet.read()){
packet.run();
ctx.write(msg+"\r\n");
}
} finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
So, my packet is correctly handled and it works fine, but then, i do ctx.write(msg+"\\r\\n");
因此,我的数据包已正确处理,并且可以正常工作,但是随后,我执行
ctx.write(msg+"\\r\\n");
to send back the message to my client, acting like an echo server. 将邮件发回给我的客户端,就像回显服务器一样。
Here is the Client's code : 这是客户的代码:
public class ChatClient {
static Socket socket;
static DataOutputStream out;
static BufferedReader in;
public static void main(String[] args){
try {
initSocket();
String test = "Salut 1";
TestPacket packet = new TestPacket(0x18,test.getBytes());
sendPacket(packet);
while(true){
try {
String message = in.readLine();
System.out.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
//TEST
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void initSocket(){
try {
socket = new Socket("localhost",58008);
out = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader((socket.getInputStream())));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendPacket(TestPacket p) throws IOException{
out.write(p.getRawData());
out.flush();
}
}
The packet is correctly sent, but i get nothing as reply, and when i stop my server, the client is spamming null because of my while(true)
, but i don't get my message back, nothing is displayed and i really don't know why. 数据包已正确发送,但是我没有得到任何答复,并且当我停止服务器时,由于我的
while(true)
,客户端发送的垃圾邮件为null,但是我没有收到消息,没有任何显示,我真的不知道不知道为什么。
I can't use netty for the client because this one is just for test purpose, the final client will be written in C# (Unity Engine), so i can't use netty in this one, I have to do it with native socket handling. 我不能为客户端使用netty,因为这只是出于测试目的,最终的客户端将用C#(Unity Engine)编写,因此我不能在此客户端中使用netty,我必须使用本机套接字进行操作处理。
EDIT: 编辑:
According to wireshark, The packet from client is sent but the server answer is not, i don't see the packet From server containing "Salut 1". 据wireshark说,来自客户端的数据包已发送,但服务器的答案却没有,我看不到来自服务器的包含“ Salut 1”的数据包。
You did: 您做了:
ctx.write(msg+"\r\n");
msg
is not a String
but a ByteBuf
. msg
不是String
而是ByteBuf
。 If you want to append \\r\\n
to the received message, you should do the following: 如果要将
\\r\\n
附加到收到的消息中,则应执行以下操作:
in.writeByte('\r');
in.writeByte('\n');
ctx.write(in);
Also, because you reused the received message ( in
) as a response, you should not release it: 另外,由于您将接收到的消息(
in
)作为响应重用,因此您不应释放它:
// Do NOT call this.
ReferenceCountUtil.release(in);
If you really intended to call ctx.write(msg + "\\r\\n")
, please make sure that your pipeline has StringEncoder
. 如果您确实打算调用
ctx.write(msg + "\\r\\n")
,请确保您的管道具有StringEncoder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.