有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

客户端Java服务器非阻塞查询

我使用以下代码从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) 个答案

  1. # 1 楼答案

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

    被要求使服务器“非阻塞”可能意味着你被要求将其更改为使用异步IO(可能不太可能),也可能意味着你被要求一次处理多个客户机——因为目前你只能为一个客户机服务,然后你的程序退出

    这个问题很难回答,因为您当前的代码离您需要的地方很远,一般来说,阅读网络、套接字和Java编程似乎是一个很好的开始

    我推荐Netty用Java做任何与网络相关的事情,它们的示例和文档都很好,而且易于理解。祝你好运