⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videomeetingserver.java

📁 一个初级的远程视频系统的代码
💻 JAVA
字号:
package videoserver.server;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;


public class VideoMeetingServer extends JFrame{
  private ServerSocketChannel serverSocketChannel;
  private Selector selector;
  private Vector sockets = new Vector();
  private int counter = 0;
  private JTextArea displayArea;
  private Charset charSet;
  private ByteBuffer writeBuffer;
  private ByteBuffer readBuffer = ByteBuffer.allocate(512);
  private String ipPort = null;
  private boolean isReady = false;
  private boolean isTransport = false;
  private String storeSendPort = "*>*>*>";
  public VideoMeetingServer() {
    super("服务器");
    displayArea = new JTextArea();
    getContentPane().add(new JScrollPane(displayArea));
    setSize(200,300);
    setVisible(true);
    //设置窗口监听
    addWindowListener(
      new WindowAdapter(){
        public void windowClosing(WindowEvent windowEvent)
        {
          try{
            serverSocketChannel.close();
            selector.close();
          }
          catch(IOException ioException){
            ioException.printStackTrace();
          }
          finally{
            System.exit(0);
          }
        }
      }
      );
  }
  public void runServer()
  {
    try{
      charSet = Charset.forName("UTF-8");
      //开启服务器套接字通道
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.socket().bind(new InetSocketAddress(12345));
      serverSocketChannel.configureBlocking(false);//设置阻塞模式为非阻塞模式
      //等待一个连接
      getConnection();
    }
    catch(Exception ioException){
      ioException.printStackTrace();
    }

  }
  //等待连接的到来,然后输出连接信息
  private void getConnection()throws Exception
  {
    //寻找正在进来的请求
    selector = SelectorProvider.provider().openSelector();
    serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT,null);
    //进行进来的请求
    while(selector.select()>0){
      Set readyKeys = selector.selectedKeys();
      Iterator iterator = readyKeys.iterator();

      //为每个准备好的channel进行请求
      while(iterator.hasNext()){
        SelectionKey key = (SelectionKey)iterator.next();
        iterator.remove();

        if(key.isAcceptable()){
          ServerSocketChannel nextReady = (ServerSocketChannel)key.channel();
          SocketChannel socketChannel=nextReady.accept();
          if(socketChannel!=null){
            socketChannel.configureBlocking(false);
            sockets.add(socketChannel.socket());
            counter++;

            SwingUtilities.invokeLater(
               new Runnable(){
                 public void run()
                 {
                   displayArea.append("\nConnection with Client"+counter);
                 }
               }
            );

            SelectionKey readKey = socketChannel.register(selector,SelectionKey.OP_READ,null);

          }
        }
        else if(key.isReadable()){
          SocketChannel socketChannel = (SocketChannel)key.channel();
          readMessage(socketChannel);
        }
        
      }
    }
  }
  private void writeMessage(String message)throws IOException
  {
    Socket socket;
    SocketChannel socketChannel;

    for(int i = 0;i<sockets.size();i++){
      socket = (Socket) sockets.elementAt(i);
      socketChannel = socket.getChannel();

      try {
        writeBuffer = charSet.encode(message);

        socketChannel.write(writeBuffer);
      }
      catch (IOException ioException) {
        ioException.printStackTrace();
        socketChannel.close();
        sockets.remove(socket);
      }
    }
  }
  private void readMessage(SocketChannel socketChannel)throws IOException
  {
    try{
      if(socketChannel.isOpen()){
        readBuffer.clear();
        socketChannel.read(readBuffer);
        readBuffer.flip();
        CharBuffer charMessage = charSet.decode(readBuffer);
        String message = charMessage.toString().trim();
        StringTokenizer tokenizer = new StringTokenizer(message,">");
       
        int count = tokenizer.countTokens();
        if(message.indexOf("Disconnect")>=0){
          writeMessage(message);
          sockets.remove(socketChannel.socket());
          socketChannel.close();
        }
        else if(count == 3)
        {
        	if(ipPort == null)
        	{
        		ipPort = message;
        	}
        	else
        		ipPort = ipPort + "$" + message;
        	writeMessage(ipPort);
        }
        else if(count == 4)
        {
        	for(int i=0;i<count-1;i++)
        		tokenizer.nextToken();
        	if(storeSendPort.equals("*>*>*>"))
        		storeSendPort = storeSendPort + tokenizer.nextToken();
        	else
        		storeSendPort = storeSendPort+"%"+tokenizer.nextToken();
        	writeMessage(storeSendPort);
        }
        else{
             writeMessage(message);
        }
      }
        
      System.out.println(ipPort);
    }
    catch(IOException ioException){
      ioException.printStackTrace();
      sockets.remove(socketChannel.socket());
      socketChannel.close();
    }
  }
  public static void main(String args[])
  {
    VideoMeetingServer application = new VideoMeetingServer();
    application.runServer();
    
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -