有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在打开一个简单的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) 个答案

  1. # 1 楼答案

        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();
        }
    }