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

📄 echoserver.java

📁 Bluetooth echo between pc server and client
💻 JAVA
字号:

// EchoServer.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, August 2005

/* EchoService is the top-level echo server: a RFCOMM service
   with the specified UUID and name. As a client
   connects, a ThreadedEchoHandler thread is spawned to
   deal with the client.

   The waiting for client connections is done in a separate
   thread so that the creation of the EchoServer doesn't 
   cause the top-level MIDlet to block.

   The server, and handlers, are terminated by calling closeDown().
*/

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;


public class EchoServer extends Thread 
{
  // UUID and name of the echo service
  private static final String UUID_STRING = "11111111111111111111111111111111";
       // 32 hex digits which will become a 128 bit ID
  private static final String SERVICE_NAME = "echoserver";   // use lowercase

  private EchoServerMIDlet ecm;
  private StreamConnectionNotifier server;

  private Vector handlers;  // stores the ThreadedEchoHandler objects
  private boolean isRunning = false;


  public EchoServer(EchoServerMIDlet ecm)
  // Create a server connection
  {
    this.ecm = ecm;
    handlers = new Vector();
    try {  // make the server's device discoverable
      LocalDevice local = LocalDevice.getLocalDevice();
      local.setDiscoverable(DiscoveryAgent.GIAC);
    }
    catch (BluetoothStateException e) {
      System.out.println(e);
      return;
    }

    /* Create a RFCOMM connection notifier for the server, with 
       the given UUID and name. This also creates a service record. */
    try {  
      server = (StreamConnectionNotifier) Connector.open(
          "btspp://localhost:" + UUID_STRING + ";name=" + SERVICE_NAME);
    }
    catch (IOException e) {
      System.out.println(e);
      return;
    }
  }  // end of EchoServer()


  public void run()
  // Wait for client connections, creating a handler for each one
  {
    isRunning = true;
    try {
      while (isRunning) {
        StreamConnection conn = server.acceptAndOpen(); 
           // wait for a client connection
           /* acceptAndOpen() also adds the service record to the 
              device's SDDB, making the service visible to clients */

        ThreadedEchoHandler hand = new ThreadedEchoHandler(conn, ecm);  
              // create client handler
        handlers.addElement(hand);
        hand.start();
      }
    }
    catch (IOException e) 
    {  System.out.println(e);  }
  }  // run()


  public void closeDown()
  /* Stop accepting any further client connections, and close down
     all the handlers. */
  {
    System.out.println("Close down server");
    if (isRunning) {
      isRunning = false;
      try {
        server.close();  
           // close connection, and remove service record from SDDB
      }
      catch (IOException e) 
      {  System.out.println(e);  }

      // close down the handlers
      ThreadedEchoHandler hand;
      for (int i=0; i < handlers.size(); i++) {
         hand = (ThreadedEchoHandler) handlers.elementAt(i);
         hand.closeDown();
      }
      handlers.removeAllElements();
    }
  }  // end of closeDown();


} // end of EchoServer class

⌨️ 快捷键说明

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