我用 C 语言编写了一段服务器代码,我希望每次迭代都将数据连续发送到我的客户端应用程序。
while (1)
{
startInt = mymillis();//capture starting time
Sensor_raw_data_float = Sensor_Read_Values_();
float data = Sensor_raw_data_float.x * 0.007;
send(sockfd, &data , sizeof(float),0);//Send to client
//Wait until 5ms
while (mymillis() - startInt < (5))
{
usleep(100);
}
}
我尝试在发送之前使用“snprintf”将 float 据转换为字符串,但我意识到整个系统开始变慢并且滞后很多。
因此,我尝试对客户端代码进行编辑。附件是我的客户端代码的主要部分,与我想要做的事情相关
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editTextAddress, editTextPort, editTextMsg;
Button buttonConnect, buttonDisconnect, buttonSend;
TextView textViewState, textViewRx;
ClientHandler clientHandler;
ClientThread clientThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textViewRx = (TextView) findViewById(R.id.received);
clientHandler = new ClientHandler(this);
}
private void updateState(String state){
textViewState.setText(state);
}
private void updateRxMsg(String rxmsg){
textViewRx.append(rxmsg + "\n");
}
public static class ClientHandler extends Handler {
public static final int UPDATE_STATE = 0;
public static final int UPDATE_MSG = 1;
public static final int UPDATE_END = 2;
private MainActivity parent;
public ClientHandler(MainActivity parent) {
super();
this.parent = parent;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case UPDATE_STATE:
parent.updateState((String)msg.obj);
break;
case UPDATE_MSG:
parent.updateRxMsg((String)msg.obj);
break;
case UPDATE_END:
parent.clientEnd();
break;
default:
super.handleMessage(msg);
}
}
}
}
我尝试将 updateRxMsg(String rxmsg) 的代码编辑为
private void updateRxMsg(String rxmsg){
private void updateRxMsg(String rxmsg){
//textViewRx.append(rxmsg + "\n");
float f = Float.parseFloat(rxmsg);
String mytext=Float.toString(f);
textViewRx.append(mytext + "\n");
}
}
但我的 TextView 上仍然打印出垃圾。我无法弄清楚问题是什么,但我怀疑它与编码有关。我想获得一些有关如何将 float 数据从服务器传输到客户端的帮助。
更新:附件是我的缓冲区读取器等的代码......,最初我认为问题不在这里,但我也会将其发布在这里。
public class ClientThread extends Thread{
String dstAddress;
int dstPort;
private boolean running;
MainActivity.ClientHandler handler;
Socket socket;
PrintWriter printWriter=null;
BufferedReader bufferedReader;
public ClientThread(String addr, int port, MainActivity.ClientHandler handler) {
super();
dstAddress = addr;
dstPort = port;
this.handler = handler;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void setRunning(boolean running){
this.running = running;
}
private void sendState(String state){
handler.sendMessage(
Message.obtain(handler,
MainActivity.ClientHandler.UPDATE_STATE, state));
}
public void txMsg(String msgToSend){
if(printWriter != null){
printWriter.println(msgToSend);
}
}
@Override
public void run() {
sendState("connecting...");
running = true;
try {
socket = new Socket(dstAddress, dstPort);
sendState("connected");
OutputStream outputStream = socket.getOutputStream();
printWriter = new PrintWriter(outputStream, true);
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader =
new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
while(running){
//bufferedReader block the code
String line = bufferedReader.readLine();
if(line != null){
handler.sendMessage(
Message.obtain(handler,
MainActivity.ClientHandler.UPDATE_MSG, line));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(printWriter != null){
printWriter.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
handler.sendEmptyMessage(MainActivity.ClientHandler.UPDATE_END);
}
}
我将代码更新为:
import android.os.Message;
import android.os.StrictMode;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.ByteBuffer;
公共(public)类 ClientThread 扩展 Thread{
String dstAddress;
int dstPort;
private boolean running;
MainActivity.ClientHandler handler;
Socket socket;
PrintWriter printWriter=null;
BufferedReader bufferedReader;
public ClientThread(String addr, int port, MainActivity.ClientHandler handler) {
super();
dstAddress = addr;
dstPort = port;
this.handler = handler;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void setRunning(boolean running){
this.running = running;
}
private void sendState(String state){
handler.sendMessage(
Message.obtain(handler,
MainActivity.ClientHandler.UPDATE_STATE, state));
}
public void txMsg(String msgToSend){
if(printWriter != null){
printWriter.println(msgToSend);
}
}
@Override
public void run() {
sendState("connecting...");
running = true;
try {
socket = new Socket(dstAddress, dstPort);
sendState("connected");
OutputStream outputStream = socket.getOutputStream();
printWriter = new PrintWriter(outputStream, true);
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader =
new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
int bufferSize = 8192;
ByteBuffer bf = ByteBuffer.allocate(bufferSize);
BufferedInputStream inFromServer = new BufferedInputStream(socket.getInputStream());
while (true) {
int b = inFromServer.read();
if (b == -1) {
break;
}
bf.put( (byte) b);
handler.sendMessage(
Message.obtain(handler,
MainActivity.ClientHandler.UPDATE_MSG, (byte) b));
}
// while(running){
//bufferedReader block the code
// String line = bufferedReader.readLine();
// if(line != null){
// handler.sendMessage(
// Message.obtain(handler,
// MainActivity.ClientHandler.UPDATE_MSG, line));
// }
// }
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(printWriter != null){
printWriter.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
handler.sendEmptyMessage(MainActivity.ClientHandler.UPDATE_END);
}
}
我很困惑应该更改什么才能使字节缓冲区正常工作,并且不确定我是否正确更改了它。 谢谢!
最佳答案
下定决心。
如果您直接发送 float
,请根据您发布的代码,使用 DataInputStream.readFloat()
读取它。如果幸运的话并且字节序是相同的,那么就完成了。
或者将其转换为字符串,将其作为一行发送,将其作为一行读取,然后将其解析回float
。
不是两者的混合。
关于java - 将 float 据发送到我的客户端应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48992469/