2012-02-28 92 views
0

我正在使用以下代碼從Android客戶端讀取一些數據。一切都很好。但是現在我被要求讓這個服務器代碼不被阻塞。有什麼建議嗎?我試圖使用線程,但不知道如何?我是初學者在Java中:)Java服務器非阻塞查詢

感謝

import java.awt.image.BufferedImage; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Calendar; 
import java.util.Date; 

import javax.imageio.ImageIO; 


public class Server { 
    //Server Constructor 
    public Server() 
    {} 
    //Variables Initialization 
    private static ServerSocket server; 
    byte[] imagetemp; 
    private static Socket socket1; 
    private static boolean newImage; 
    private static Sdfdata data; 
    private static boolean cond; 
    public static int port; 
    private static int number = 0; 
    //Image Availability return method 
    public boolean imageAvailable() 
    { 
     return newImage; 
    } 
    public boolean clientchk() 
    { 
     return socket1.isClosed(); 
    } 
    //Image Flag set by Vis group when image read. 
    public void setImageFlag(boolean set) 
    { 
     newImage = set; 
    } 
    // Send the data to the Vis Group 
    public Sdfdata getData() 
    { 
    return data; 
    } 
    //Starts the Server 
    public static boolean start(int port1) 
    { 
     try { 
      port=port1; 

       server = new ServerSocket(port1); 
      System.out.println("Waiting for Client to Connect"); 
      //New thread here 

      socket1=server.accept(); 


     } catch (IOException e) { 
      System.out.println("Cannot Connect"); 
      e.printStackTrace(); 
      return false; 
     } 
     return true; 
    } 
    //Stops the Server 
    public boolean stop() 
    { 

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

      e.printStackTrace(); 
      return false; 
     } 
     return true; 

    } 
    /** 
    * @param args 
    * @throws IOException 
    */ 



    public static void main(String[] args) throws IOException { 
     // Starts the server 
     start(4444); 
     // DataInput Stream for reading the data 
     DataInputStream in = null; 
     try { 
      in = new DataInputStream(socket1.getInputStream()); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     cond=true; 

     do { 

      try 
      { 
       //Read Image Data 
       int length = in.readInt(); 
       //Create an ByteArray of length read from Client for Image transfer 
       Sdfdata data = new Sdfdata(length); 

       //for (int i=0; i<length; i++) 
       //{ data.image[i] = in.readbyte(); } 

       if (length > 0) { 
        in.readFully(data.image); 
       } 

       //Read Orientation 
       data.orientation[0] = in.readFloat();  //Orientation x 
       data.orientation[1] = in.readFloat();  //Orientation y 
       data.orientation[2] = in.readFloat();  //Orientation z 

       //Read GPS 
       data.longitude = in.readDouble(); 
       data.latitude = in.readDouble(); 
       data.altitude = in.readDouble(); 

       //Display orientation and GPS data 
       System.out.println(data.orientation[0] + " " + data.orientation[1] + " " + data.orientation[2]); 
       System.out.println(data.longitude + " " + data.latitude + " " + data.altitude); 

       String fileName = "IMG_" + Integer.toString(++number) + ".JPG"; 
       System.out.println("FileName: " + fileName); 
       FileOutputStream fos = new FileOutputStream(fileName); 
       fos.write(data.image); 
       fos.close(); 





       /*InputStream ins = new ByteArrayInputStream(data.image); 
       BufferedImage image = ImageIO.read(ins); 
       ImageIO.write(image, "JPG", new File (fileName)); 
       */ 
       //set image flag 
       newImage = true; 


      } catch (Exception e) { 
       //System.out.println("EOF Or ? " + e); 

       cond =false; 
       socket1.close(); 
       server.close(); 
       start(port); 

      } 
    }while (cond); 
     } 

}

回答

2

你的代碼啓動服務器,等待連接,讀取從第一連接的客戶端的一些數據,然後寫這後退出數據到文件。要求讓您的服務器「非阻塞」可能意味着您被要求將其更改爲使用異步IO(可能不太可能),或者這可能意味着您被要求處理多個客戶端一次 - 因爲目前你只能服務一個客戶,然後你的程序退出。

這個問題很難回答,因爲你現在的代碼離你所需要的地方很遠,看起來好像有人在網絡,套接字和一般的Java編程上讀到這些是一個很好的開始。

我推薦使用Netty來處理Java中與網絡相關的任何事情,它們的樣本和文檔都很好且易於遵循。祝你好運!

+0

感謝高山。不,我不會向我們詢問異步IO。我只是被要求啓動一個應該監聽套接字而不是這個套接字監聽的線程。它將只有一個線程,並有助於最大限度地減少CPU使用率等。如果我以正確的方式。 – 2012-02-29 00:20:45