2012-02-28 95 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