Java通信网络服务器-套接字客户端

时间:2022-08-26 19:49:18

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"); to send back the message to my client, acting like an echo server.

所以,我的包被正确处理,它运行良好,但是,我做ctx.write(msg+“\r\n”);将消息发送回我的客户端,就像一个echo服务器。

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)而垃圾邮件为空,但是我没有得到我的消息,没有显示任何内容,我真的不知道为什么。

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”的服务器的数据包。

1 个解决方案

#1


0  

You did:

你做的:

ctx.write(msg+"\r\n");

msg is not a String but a ByteBuf. If you want to append \r\n to the received message, you should do the following:

味精不是字符串,而是ByteBuf。如果你想在收到的信息中追加\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。写(msg +“\r\n”),请确保您的管道有StringEncoder。

#1


0  

You did:

你做的:

ctx.write(msg+"\r\n");

msg is not a String but a ByteBuf. If you want to append \r\n to the received message, you should do the following:

味精不是字符串,而是ByteBuf。如果你想在收到的信息中追加\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。写(msg +“\r\n”),请确保您的管道有StringEncoder。