parent
bf17b71b2f
commit
897f6c094b
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,52 @@
|
||||
package com.msb.model;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/11/10 15:55
|
||||
* @Description: com.msb.model
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
/**
|
||||
*@ClassName ClientFrame
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/11/10 15:55
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ClientFrame extends Frame {
|
||||
|
||||
public ClientFrame() {
|
||||
TextArea ta = new TextArea();
|
||||
TextField tf = new TextField();
|
||||
this.setSize(600, 400);
|
||||
this.add(ta, BorderLayout.CENTER);
|
||||
this.add(tf, BorderLayout.SOUTH);
|
||||
this.setVisible(true);
|
||||
this.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
ta.setEditable(false);
|
||||
|
||||
tf.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ta.setText(ta.getText() + tf.getText() + "\r\n");
|
||||
tf.setText("");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ClientFrame();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package com.msb.socket;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/11/10 16:12
|
||||
* @Description: com.msb.socket
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.group.ChannelGroup;
|
||||
import io.netty.channel.group.DefaultChannelGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.util.concurrent.GlobalEventExecutor;
|
||||
|
||||
/**
|
||||
*@ClassName Server
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/11/10 16:12
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Server {
|
||||
|
||||
private int port;
|
||||
|
||||
public Server(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void serverStart() {
|
||||
EventLoopGroup bootGroup = new NioEventLoopGroup(1);
|
||||
EventLoopGroup workGroup = new NioEventLoopGroup(3);
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap.group(bootGroup, workGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||
socketChannel.pipeline().addLast(new Handler());
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
bootstrap.bind(this.port).sync();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
bootGroup.shutdownGracefully();
|
||||
workGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Server(8888);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Handler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
// super.channelRead(ctx, msg);
|
||||
channelGroup.forEach(channel -> {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
// super.exceptionCaught(ctx, cause);
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue