Netty客户端到Tcp服务器的连接

时间:2019-01-11 12:00:39

标签: java tcp server client netty

我正在打开一个简单的tcp服务器,但是netty客户端无法连接。

如有必要

(netty client)      <----->  (netty server)  it works

(simple tcp client) <----->  (simple tcp server) it works

但是

(netty client) <----> (simple tcp server) doesn't work

你能帮我吗

1 个答案:

答案 0 :(得分:0)

    public class TCPServer extends Thread {
   private ServerSocket serverSocket;

   public TCPServer(int port) throws IOException {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run() {
      while(true) {
         try {
            System.out.println("Waiting for client on port " + 
               serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();

            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());

            System.out.println(in.readUTF());
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
               + "\nGoodbye!");
//            server.close();

         } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
//            break;
         } catch (IOException e) {
            e.printStackTrace();
//            break;
         }
      }
   }

   public static void main(String [] args) {
      int port = 14115;
      try {
         Thread t = new TCPServer(port);
         t.start();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

---客户----

import io.netty.bootstrap.Bootstrap;
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.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public final class Client {

    static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "14115"));

    public static void main(String[] args) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(group);

            b.channel(NioSocketChannel.class);

            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();

                    p.addLast(new StringDecoder());
                    p.addLast(new StringDecoder());
                    p.addLast(new ClientHandler());
                }
            });

            b.option(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.connect(HOST, PORT).sync();

        } finally {
            group.shutdownGracefully();
        }
    }
}

---客户端处理程序---

public class ClientHandler extends ChannelInboundHandlerAdapter {

    public ClientHandler() {
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("Channel Active");
        try {
            ctx.writeAndFlush("Hello Server");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("client received: " + msg);
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        cause.printStackTrace();
        ctx.close();
    }
}
相关问题