gpt4 book ai didi

Java 服务器非阻塞查询

转载 作者:行者123 更新时间:2023-12-02 07:57:00 24 4
gpt4 key购买 nike

我正在使用以下代码从 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);
}

}

最佳答案

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

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

这个问题很难回答,因为您当前的代码离您需要的位置很远,而且似乎阅读一些有关网络、套接字和 Java 编程的一般知识将是一个很好的开始方式。

我推荐Netty他们的示例和文档非常好且易于理解。祝你好运!

关于Java 服务器非阻塞查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com