diff --git a/src/main/java/com/msb/model/ClientFrame.java b/src/main/java/com/msb/model/ClientFrame.java index 4015d55..2d8e08b 100644 --- a/src/main/java/com/msb/model/ClientFrame.java +++ b/src/main/java/com/msb/model/ClientFrame.java @@ -5,6 +5,8 @@ package com.msb.model;/** * @Version: 1.0 */ +import com.msb.socket.Client; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -21,6 +23,10 @@ import java.awt.event.WindowEvent; public class ClientFrame extends Frame { public ClientFrame() { + + Client client = new Client("localhost", 8888); + client.clientStart(); + TextArea ta = new TextArea(); TextField tf = new TextField(); this.setSize(600, 400); @@ -41,6 +47,7 @@ public class ClientFrame extends Frame { public void actionPerformed(ActionEvent e) { ta.setText(ta.getText() + tf.getText() + "\r\n"); tf.setText(""); + client.getChannel().writeAndFlush(ta.getText().getBytes()); } }); } diff --git a/src/main/java/com/msb/socket/Client.java b/src/main/java/com/msb/socket/Client.java new file mode 100644 index 0000000..e6ac106 --- /dev/null +++ b/src/main/java/com/msb/socket/Client.java @@ -0,0 +1,81 @@ +package com.msb.socket;/** + * @Author bingor + * @Date 2022/11/16 13:39 + * @Description: com.msb.socket + * @Version: 1.0 + */ + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +/** + *@ClassName Client + *@Description TODO + *@Author bingor + *@Date 2022/11/16 13:39 + *@Version 3.0 + */ +public class Client { + + public void connect() { + //线程池 + EventLoopGroup group = new NioEventLoopGroup(1); + Bootstrap bootstrap = new Bootstrap(); + + try { + ChannelFuture future = bootstrap.group(group) + .channel(NioSocketChannel.class) + .handler(new ClientChannelInitializer()) + .connect("localhost", 8888); + + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + if(channelFuture.isSuccess()) { + System.out.println("connected!"); + } else { + System.out.println("not connect!"); + } + } + }); + + future.sync(); + + future.channel().closeFuture().sync(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + group.shutdownGracefully(); + } + } + + public static void main(String[] args) { + Client client = new Client(); + client.connect(); + } + +} + +class ClientChannelInitializer extends ChannelInitializer { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new ClientHandler()); + } +} + +class ClientHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { +// super.channelRead(ctx, msg); + ByteBuf buf = (ByteBuf) msg; + byte[] bytes = new byte[buf.readableBytes()]; + buf.getBytes(buf.readerIndex(), bytes); + System.out.println(new String(bytes)); + } +} diff --git a/src/main/java/com/msb/socket/Server.java b/src/main/java/com/msb/socket/Server.java index be7ef0e..65538bd 100644 --- a/src/main/java/com/msb/socket/Server.java +++ b/src/main/java/com/msb/socket/Server.java @@ -69,7 +69,9 @@ class Handler extends ChannelInboundHandlerAdapter { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // super.channelRead(ctx, msg); channelGroup.forEach(channel -> { - + if( ! channel.equals(ctx.channel())) { + channel.writeAndFlush(msg); + } }); }