mina TCP、UDP通讯

  • Z4_281537
    了解作者
  • 1.1MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 07:08
    上传日期
收集几个mina的通讯例子,实现TCP、UDP通讯
MinaDemo.zip
内容介绍
package com.demo; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.future.IoFutureListener; import org.apache.mina.core.service.IoConnector; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.LineDelimiter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioDatagramConnector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Sends its memory usage to the MemoryMonitor server. * 这样的写法 将本来可以单独的client写到IoHandlerAdapter 更紧凑 * * @author <a href="http://mina.apache.org" mce_href="http://mina.apache.org" rel='nofollow' onclick='return false;'>Apache MINA Project</a> */ public class MemMonClient extends IoHandlerAdapter { private final static Logger LOGGER = LoggerFactory.getLogger(MemMonClient.class); private IoSession session; private IoConnector connector; /** * Default constructor. */ public MemMonClient() { connector = new NioDatagramConnector(); connector.setHandler(this); // 设置filter DefaultIoFilterChainBuilder chain = connector.getFilterChain(); chain.addLast("logger", new LoggingFilter()); // 设置编码过滤器 chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("utf-8"), LineDelimiter.NUL, LineDelimiter.NUL))); ConnectFuture connFuture = connector.connect(new InetSocketAddress( "localhost", 1234)); connFuture.awaitUninterruptibly(); // 给conn添加一个监听器 connFuture.addListener(new IoFutureListener<ConnectFuture>() { public void operationComplete(ConnectFuture future) { if (future.isConnected()) { session = future.getSession(); try { sendData(); } catch (InterruptedException e) { e.printStackTrace(); } } else { try { throw new Exception(" 连接错误。 "); } catch (Exception e) { e.printStackTrace(); } } } }); } private void sendData() throws InterruptedException { // long free = Runtime.getRuntime().freeMemory(); // 得到当前空闲内存大小 // IoBuffer buffer = IoBuffer.allocate(20); // buffer.putLong(free); // 只把剩余内存大小放入buffer, 扔给server // buffer.flip(); String buffer = "测试数据"; session.write(buffer.toString()); // 写入 } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { cause.printStackTrace(); } @Override public void messageReceived(IoSession session, Object message) throws Exception { //Charset c = Charset.forName("UTF-8"); //CharsetDecoder cd = c.newDecoder(); IoBuffer buffer = (IoBuffer)message; System.out.println("客户端收到来自服务器的消息String:" + message.toString()); } @Override public void messageSent(IoSession session, Object message) throws Exception { System.out.println("客户端向服务器发送信息:" + message.toString()); } @Override public void sessionClosed(IoSession session) throws Exception { System.out.println("客户端关闭了当前会话"); } @Override public void sessionCreated(IoSession session) throws Exception { System.out.println("客户端成功创建session"); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { } @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("客户端成功开启一个session id:"+session.getId()); } public static void main(String[] args) { new MemMonClient(); } }
评论
    相关推荐
    • UDP发送工具
      可以用自动驾考系统的数据模拟发送工具,支持udp传输,端口可自由配置,支持单条单次发送、单条自动发送和顺序发送,支持拖拽和发送周期自由配置
    • reliable-udp
      基于 UDP 的可靠数据传输 等停协议 确认机制 超时重传 序号 流水线协议 滑动窗口 选择重传 参考文献: [1]: 计算机网络-自顶向下方法
    • Udp编程
      udp程序设计实例,学习之用
    • udp 聊天
      c# udp 聊天 c# udp 聊天
    • UDPCLIENT
      使用UDP的新闻发布和 聊天室
    • UDP网络通讯
      UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!UDP网络通讯!
    • udp 广播实现
      udp 实现广播,如果存在多张网卡,禁用其他的网卡自保留一个,或者制定绑定本地ip 地址 _server_add.sin_addr.s_addr = htons(INADDR_ANY);改成 _server_add.sin_addr.s_addr = htons(Ip地址);
    • UDP Flooder
      包含3个UDP Flooder工具,都是网上搜罗来的。其中的Shawosti Udp flooder.exe效率看起来最高。
    • udp
      这是的入门模板。 ghgjhgjhg
    • Socket UDP
      Delphi7写的UDP通信项目源码,用winsock API,非控件