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

📄 server.java

📁 J2ME手机蓝牙五子棋游戏
💻 JAVA
字号:
package bluetoochgame;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.io.IOException;

import java.util.Vector;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;


public class Server implements Runnable{
  Connection connection;
   StreamConnection conn1 = null;
  LocalDevice local = null;
  ClientProcessor processor;
  DataOutputStream dos;
  boolean isClosed = false;
  boolean isOut = false;
  StreamConnectionNotifier notifier;
  String messagex = null,messagey=null;
  String connectionURL =
      "btspp://localhost:F0E0D0C0B0A000908070605040302010;"
      + "authenticate=false;encrypt=false;name=RFCOMM Server"; //UUID is 393a84ee7cd111d89527000bdb544cb1
  Vector queue = new Vector();

  public Server(Connection connection) {
    this.connection = connection;
  }

  public void start()
  {
    Thread t = new Thread(this);
    t.start() ;
  }


  public void run()
  {

     try {
         local = LocalDevice.getLocalDevice();
         local.setDiscoverable(DiscoveryAgent.GIAC);
         } catch (Exception e) {}

      try {
          notifier = (StreamConnectionNotifier)Connector.open(connectionURL);
          System.out.println("server is opening") ;
          } catch (IOException e1) {}

              processor = new ClientProcessor();

             // ok, start accepting connections then
             while (!isClosed) {
                 StreamConnection conn = null;

                 try {
                     conn = notifier.acceptAndOpen();
                 } catch (IOException e) {
                     // wrong client or interrupted - continue anyway
                     continue;
                 }
                 processor.addConnection(conn);
             }



  }//end run


 public void sendMessage(String message1,String message2)
 {
    messagex = message1;
    messagey = message2;
    try {
           // DataOutputStream dos = conn.openDataOutputStream();
            dos.writeUTF(messagex);
            System.out.println("afsfas");
            dos.writeUTF(messagey) ;
            dos.flush();
            dos.close();
        } catch (IOException e) {
        }
}

     private class ClientProcessor implements Runnable {
           private Thread processorThread;

           private Vector queue = new Vector();

           private boolean isOk = true;

           ClientProcessor() {
               processorThread = new Thread(this);
               processorThread.start();
           }

           public void run() {
               while (!isClosed) {

                   synchronized (this) {
                       if (queue.size() == 0) {
                           try {
                               //阻塞,直到有新客户连接
                               wait();
                           } catch (InterruptedException e) {

                           }
                       }
                   }

                   //处理连接队列

                   StreamConnection conn;

                   synchronized (this) {

                       if (isClosed) {
                           return;
                       }
                       conn = (StreamConnection) queue.firstElement();
                       queue.removeElementAt(0);
                       processConnection(conn);
                   }
               }
           }

           /**
            * 往连接队列添加新连接,同时唤醒处理线程
            */

           void addConnection(StreamConnection conn) {
             synchronized (this) {
                   queue.addElement(conn);
                   notify();
               }
           }

} //end processor

private void readInputString(StreamConnection conn) {
       String message1,message2 = null;

       try {
           DataInputStream dis = conn.openDataInputStream();
           message1 = dis.readUTF();
           message2 = dis.readUTF() ;
           dis.close();
           connection.receiveMessage(message1,message2) ;
       } catch (Exception e) {
           e.printStackTrace();
           System.out.println("yi chang") ;
       }
   }


   private void processConnection(StreamConnection conn) {
    try{
     dos = conn.openDataOutputStream();
     }
    catch(Exception e){}// 读取输入
     readInputString(conn);

     //输出响应
   //   sendOutputData(messagex,messagey, conn);

  }
/*
  private void sendOutputData(String messagex,String messagey, StreamConnection conn) {

         try {
             DataOutputStream dos = conn.openDataOutputStream();
             dos.writeUTF(messagex);
             dos.writeUTF(messagey) ;
             dos.flush();
             dos.close();
         } catch (IOException e) {
         }




     }*/




}

⌨️ 快捷键说明

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