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

📄 btserver.java

📁 服务器端源文件:btServer-20050819.zip
💻 JAVA
字号:
package btServer;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

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

import btServer.fileManager;

/**
 * @author renyi
 * 
 * @param
 * @return
 *
 */

/*	            
        Runnable sthread = new startServer();
        Thread thread = new Thread(sthread);
        thread.start(); 
        
            //public class startServer implements Runnable {
        //public void run() {
    }
    */

public class btServer {
    private boolean freeThread = true;    
    
    public static void main(String[] args) {
        printMsg("");
        printMsg("Starting btServer v1.0.....");
        writeLog("Starting btServer v1.0.....");
        //while (true) {
        btServer bs = new btServer();
        //}        
        System.exit(0);
    }
    
    public btServer(){
        Runnable sthread = new startService();
        Thread thread = new Thread(sthread);
        
        //while (!accepting) { synchronized (this) { wait (1000); } }
        try {
            for (int i=0; i<5; i++ ) {
                while (!freeThread) { synchronized (this) { wait (1000); } }
                thread.start();                 
            }
        } catch (Exception e) { printMsg (e.toString()); }
    }
        
    public class startService implements Runnable {    
        public void run() {
            freeThread=false;
            printMsg("Running");
            /*
            fileManager fm = new fileManager();        
            
            // Setup local device
    	    try {
    	        LocalDevice l = LocalDevice.getLocalDevice();	        
    	    } catch(BluetoothStateException e) {
    	        printMsg(e.toString());	  
    	        writeLog(e.toString());
    	        return;
    	    }
    	    
            // Setting up host URL
            String serviceType = new String ("btspp://localhost:");
            UUID uuid = new UUID("32df102b20d64f0b8b1f45d824eff75f", false);
            String name = new String(";name=Bluetooth Fileserver");
            String parameters = new String(";authorize=false;authenticate=false;encrypt=false");
            String url = new String( serviceType + uuid.toString() + name + parameters);
            
            try {
                StreamConnectionNotifier server = (StreamConnectionNotifier)Connector.open(url);
                printMsg("Waiting for client connection....");
                writeLog("Waiting for client connection....");
                
                StreamConnection conn = server.acceptAndOpen();                
                printMsg("Client connected.");
                writeLog("Client connected.");
                
                DataInputStream in = conn.openDataInputStream();
    			String req = in.readUTF();											
    			printMsg("Operation: " + req);
    			writeLog("Operation: " + req);
    			    			    			
    			if (req.equals("sendList")) {
    			    in.close();    			        			    
    			    printMsg("Sending list...");
    			    writeLog("Sending list...");
    			    sendList(fm,conn);
    			    writeLog("Done.");
    			    conn.close();    			    
    				server.close();
    			}
    			else if (req.equals("sendFile")) {		    
    			    String fileToSend = in.readUTF();
    			    String clientInfo = in.readUTF();
    			    
    			    in.close();
    			    conn.close();
    				server.close();
    				
    				printMsg("Sending file...");
    				writeLog("Sending file...");
    				sendFile sf = new sendFile(fileToSend, clientInfo);	 
    				writeLog("Done.");
    			}
    			printMsg("Closing connection...");
            } catch (Exception e) { 
                printMsg(e.toString());
                writeLog(e.toString());
                }
                */        
        }
    }
         
    public void sendList(fileManager f, StreamConnection c){         
        String[] tmpFile = f.getFileList();
        String tmpF = tmpFile[0];
        
        // Make string array into one string
        for (int i=1; i<tmpFile.length; i++)
            tmpF = tmpF.concat(",").concat(tmpFile[i]);        
        
        try {                        
            DataOutputStream out = new DataOutputStream(c.openOutputStream());
            out.writeUTF(tmpF);
            out.flush();                                    
            out.close();            
            printMsg("Done.");
        	} catch (Exception e) {
        	    printMsg(e.toString());			
   				}	
    }
    
    public static void printMsg(String m) {
        System.out.println(m);
    }
    
    public static void writeLog(String m) {
        try {
            File logFile = new File(System.getProperty("user.dir") + File.separator + "log.txt");
        	FileOutputStream fout = new FileOutputStream(logFile,true);        	
        	DataOutputStream wout = new DataOutputStream(fout);
        	wout.writeUTF(m);
        	wout.flush();
        	wout.close();
        	fout.close();        	                	
        } catch (Exception e) { System.out.println(e.toString()); }
    }
    
} // END btServer

⌨️ 快捷键说明

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