title: Java Communication netty server - Socket client
body: <p>For a chat server project I use netty as server, with the following code in my handler : </p>
  
  <pre><code>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 &amp;&amp; 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();
          }
  }
  </code></pre>
  
  <p>So, my packet is correctly handled and it works fine, but then, i do <code>ctx.write(msg+"\r\n");</code> to send back the message to my client, acting like an echo server.</p>
  
  <p>Here is the Client's code : </p>
  
  <pre><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();
      }
  }
  </code></pre>
  
  <p>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 <code>while(true)</code>, but i don't get my message back, nothing is displayed and i really don't know why.</p>
  
  <p>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.</p>
  
  <p>EDIT:</p>
  
  <p>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".</p>