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

📄 server.java

📁 这是一个蓝牙五子棋的手机游戏,大家来看看呀
💻 JAVA
字号:





import javax.microedition.lcdui.*;
import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;

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;




public class Server implements Runnable
{
    private Connection connection;

    private LocalDevice local = null;
    private ClientProcessor processor;
    private DataOutputStream dos;
    private boolean isClosed = false;
    private boolean isOut = false;
    private StreamConnectionNotifier notifier;
    private String messagex = null, messagey = null;
    private String connectionURL =
        "btspp://localhost:F0E0D0C0B0A000908070605040302010;" +
        "authenticate=false;encrypt=false;name=FIR Server";
    
    private Vector queue = new Vector();
    private Thread accepterThread;
   

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

    public void start()
    {
        System.out.println("Server start");
        accepterThread = new Thread(this);
        accepterThread.start();
    }


    public void run()
    {
        boolean isBTReady = false;
        try
        {
            local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);
        }
        catch (Exception e)
        {
            System.out.println("Can't set discoverable mode...");
        }

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

            isBTReady = true;

        }
        catch (Exception e)
        {
            System.err.println("Can't initialize bluetooth: " + e);
        }
     

        connection.completeInitialization(isBTReady);

        if (!isBTReady)
        {
            return ;
        }

        processor = new ClientProcessor();

   
        while (!isClosed)
        {
            StreamConnection conn = null;

            try
            {
                conn = notifier.acceptAndOpen();
            }
            catch (IOException e)
            {
              
                continue;
            }
            processor.addConnection(conn);
        }



    } 


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

    private void processConnection(StreamConnection conn)
    {
        try
        {
            dos = conn.openDataOutputStream();
        }
        catch (Exception e){}

        readInputString(conn);


    }

    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 class ClientProcessor implements Runnable
    {
        private Thread processorThread;

        private Vector queue = new Vector();

        private boolean isOk = true;

        ClientProcessor()
        {
            processorThread = new Thread(this);
            processorThread.start();
            System.out.println("client");
        }

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

                synchronized(this)
                {
                    if (queue.size() == 0)
                    {
                        try
                        {
                            wait();

                        }
                        catch (InterruptedException e)
                        {
                            System.err.println("Unexpected exception: " + e);
                            destroy(false);
                            return ;
                        }
                    }
                }

 

                StreamConnection conn;

                synchronized(this)
                {
                    // may be awaked by "destroy" method.
                    if (isClosed)
                    {
                        return ;
                    }
                    conn = (StreamConnection)queue.firstElement();
                    queue.removeElementAt(0);
                    processConnection(conn);
                    try
                    {
                        dos = conn.openDataOutputStream();
                    }
                    catch (Exception e){}
              
                }
            }
        }

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


        void destroy(boolean needJoin)
        {
            StreamConnection conn;

            synchronized(this)
            {
                notify();

                while (queue.size() != 0)
                {
                    conn = (StreamConnection)queue.firstElement();
                    queue.removeElementAt(0);

                    try
                    {
                        conn.close();
                    }
                    catch (IOException e){}
                  
                }
            }

   
            try
            {
                processorThread.join();
            }
            catch (InterruptedException e){}
          
        }

    } 

}

⌨️ 快捷键说明

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