/** * TcpAsyncServer.java */ import java.nio.ByteBuffer; import java.nio.channels.*; import java.net.*; import java.util.Iterator; public class TcpAsyncServer { /*监听端口*/ int port = 8888; /*缓冲区大小*/ ByteBuffer buffer = ByteBuffer.allocate(48); /*其它相关定义*/ Selector selector; ServerSocketChannel channel; ServerSocket socket; //数据的发送到网站 SendMassage sendMassage=new SendMassage(); /*启动*/ public void Start() throws Exception { /*初始化一个Selector*/ selector = Selector.open(); /*打开通道*/ channel = ServerSocketChannel.open(); /*非阻塞模式*/ channel.configureBlocking(false); /*本机IP*/ //InetAddress ip = InetAddress.getByName("127.0.0.1"); InetAddress ip = InetAddress.getLocalHost(); System.out.println(ip.toString()); /*绑定IP和端口*/ InetSocketAddress address = new InetSocketAddress(ip,port); socket = channel.socket(); socket.bind(address); /*启动监听*/ System.out.println("TCP服务器开始监听..."); Listen(); } /*停止*/ public void Stop() throws Exception { channel.close(); selector.close(); } /*监听*/ public void Listen() throws Exception { /*注册接收事件*/ channel.register(selector,SelectionKey.OP_ACCEPT); /*无限循环*/ while (true) { selector.select(); /*轮询事件*/ Iterator iter = selector.selectedKeys().iterator(); while (iter.hasNext()) { SelectionKey key = (SelectionKey)iter.next(); iter.remove(); /*事件分类处理*/ if (key.isAcceptable()) { ServerSocketChannel ssc = (ServerSocketChannel)key.channel(); SocketChannel sc = ssc.accept(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); System.out.println("新终端已连接:"+ sc.getRemoteAddress()); } else if (key.isReadable()) { SocketChannel sc = (SocketChannel)key.channel(); int recvCount = sc.read(buffer); if (recvCount > 0) { byte[] arr = buffer.array(); System.out.println(sc.getRemoteAddress() + "发来数据: "+ new String(arr)); // sendMassage.setMassage(new String(arr)); // sendMassage.send(); buffer.flip(); } else { sc.close(); } buffer.clear(); } else { } } } } }
主方法
//主方法, class serverMain { public static void main(String[] args) throws Exception { TcpAsyncServer tcpServer = new TcpAsyncServer(); tcpServer.Start(); } }
发送端
import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; /** * 发送消息 * 发送数据 端口10001 * 接受数据端口10002 */ public class SendMassage { // 发送消息内容,当消息为a时不处理 private String massage = ""; public String getMassage() { return massage; } public void setMassage(String massage) { this.massage = massage; } // 建立连接,发送制定消息 public void send(){ try { if(!massage.equals("") || !massage.equals(null)){ String i= massage; ServerSocket ss = new ServerSocket(10001); System.out.println("等待链接"); Socket socket = ss.accept(); System.out.println("链接成功"+i); OutputStream out = socket.getOutputStream(); String w = String.valueOf(i); out.write(w.getBytes()); socket.close(); ss.close(); } } catch (IOException e) { e.printStackTrace(); } } public static String suiji() { int x; String y; Random r = new Random(); x=r.nextInt(2); switch (x) { case 0: y="25,44,204,312,0.5,北"; break; case 1: y="29,55,215,333,0.45,南"; break; default: y ="27,66,256,450,0.4,东"; break; } return y; } }
时间: 2024-10-08 14:32:41