动态航班信息处理的java实现(源码)

本文档介绍如何使用Java实现动态航班信息处理,包括EchoClient、EchoServer和测试部分。参考测试文档fds_data(lab4).txt,可在指定链接下载。涉及到核心技术包括Java网络编程和文件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

EchoClient:

 

import java.net.*;
import java.nio.channels.*;
import java.nio.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
public class EchoClient86
{
  private DatagramChannel datagramChannel = null;
  private ByteBuffer sendBuffer=ByteBuffer.allocate(1024);
  private ByteBuffer receiveBuffer=ByteBuffer.allocate(1024);
  private Charset charset=Charset.forName("GBK");
  private Selector selector;
  
  public EchoClient86()throws IOException{
    this(7000);
  }
  public EchoClient86(int port)throws IOException{
    datagramChannel = DatagramChannel.open();
    InetAddress ia = InetAddress.getLocalHost();
    InetSocketAddress isa = new InetSocketAddress(ia,port);
    datagramChannel.configureBlocking(false); //设置为非阻塞模式
    datagramChannel.socket().bind(isa); //与本地地址绑定
    isa = new InetSocketAddress(ia,9999);
    datagramChannel.connect(isa); //与远程地址连接
    selector=Selector.open();
  }
  public static void main(String args[])throws IOException
  {
    int port=7000;
    if(args.length>0)port=Integer.parseInt(args[0]);    
    final EchoClient86 client=new EchoClient86(port);
    client.talk();
  }
  
  public void talk()throws IOException {
     datagramChannel.register(selector,
                          SelectionKey.OP_READ 
                        );
     while (selector.select() > 0 ){
       Set readyKeys = selector.selectedKeys();
       Iterator it = readyKeys.iterator();
       while (it.hasNext()){
         SelectionKey key=null;
         try{
             key = (SelectionKey) it.next();
             it.remove();
             if (key.isReadable()) {
                 receive(key);
             }
       
         }catch(IOException e){
            e.printStackTrace();
            try{
                if(key!=null){
                    key.cancel();
                    key.channel().close();
                }
            }catch(Exception ex){e.printStackTrace();}
         }
      }//#while
    }//#while
  }
  public void receive(SelectionKey key)throws IOException{
    DatagramChannel datagramChannel=(DatagramChannel)key.channel();
    datagramChannel.read(receiveBuffer);
    receiveBuffer.flip();
    String receiveData=decode(receiveBuffer);
    if(receiveData.indexOf("\n")==-1)
     return;
    String outputData=receiveData.substring(0,receiveData.indexOf("\n")+1);
    System.out.print(outputData);
    if(outputData.equals("no data!\r\n")){
        key.cancel();
        datagramChannel.close();
        System.out.println("关闭与服务器的连接");
        selector.close();
        System.exit(0);
    }
    ByteBuffer temp=encode(outputData);
    receiveBuffer.position(temp.limit());
    receiveBuffer.compact();
  }
  public String decode(ByteBuffer buffer){  //解码
    CharBuffer charBuffer= charset.decode(buffer);
    return charBuffer.toString();
  }
  public ByteBuffer encode(String str){  //编码
    return charset.encode(str);
  }
}

EchoServer:

import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Handler;

public class EchoServer9 {
    private static int port = 9999;           //端口号
    private ExecutorService executorService;  //线程池
    private final int POOL_SIZE = 4;          //单个CPU时线程池中工作线程的数量
    private BufferedReader br = null;         //用于存储数据
    private static int num = 0;               //记录数据
    private DatagramSocket datagramSocket;    //新建一个datagramSocket

    public EchoServer9() throws Exception {
        datagramSocket = new DatagramSocket(port);//建立datagramSocket并绑定端口
        executorService = 
        		Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE);
        //创建线程池;Runtime的availableProcessors()方法返回当前系统的CPU的数目
        br = new BufferedReader(new FileReader("fds_data(lab4).txt"));  //BufferedReader存储发送文件的内容
        sum();
        System.out.println("服务器启动");
    }
    publi
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花月诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值