Java Server非阻塞查询

时间:2012-02-28 23:32:11

标签: java client blocking

我使用以下代码从Android客户端读取一些数据。一切都很好。但现在我被要求使这个服务器代码无阻塞。对此有什么建议吗?我试图使用线程,但不知道如何?我是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);
        }

}

1 个答案:

答案 0 :(得分:2)

您的代码启动服务器,等待连接,从第一个连接的客户端读取一些数据,然后在将此数据写入文件后退出。

被要求让您的服务器“无阻塞”可能意味着您被要求更改它以使用异步IO(可能不太可能),或者它可能意味着您被要求处理多个客户端一段时间 - 因为目前你只能为一个客户服务,然后退出你的程序。

这个问题很难回答,因为你当前的代码离你需要的地方很远,而且似乎有些阅读网络,套接字和Java编程通常都是一个好的开始。 / p>

我建议使用Netty来处理与Java相关的任何事情,他们的样本和文档很好且易于理解。祝你好运!

相关问题