消除通过 tcp/ip 发送字符串的延迟

发布于 2024-11-09 07:59:16 字数 2204 浏览 0 评论 0原文

我的项目有一个灯开关,它有一个 IP 地址和端口,并接受字符串进行控制。我正在尝试创建一个应用程序来打开和关闭此功能。 我的代码有效,但是在第一次单击后,下一次单击和实际切换的开关之间存在显着的延迟。

我有一个开关按钮。 main.java:

package com.android.lswitch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class lightswitch extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Handle swon button
    Button swon = (Button) findViewById(R.id.swon);
    swon.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sw(true);
        }
    });

    // Handle swoff button
    Button swoff = (Button) findViewById(R.id.swoff);
    swoff.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sw(false);
        }
    });


}

private void sw(boolean swstate) {
    if (swstate == true) {
        Thread swonThread = new Thread(new swon());
        swonThread.start();    
    }

    if (swstate == false) {
    Thread swoffThread = new Thread(new swoff());
    swoffThread.start();    
    }

}
}

和 java 上的按钮: 包 com.android.lswitch;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class swon implements Runnable {
public static final String SERVERIP = "10.0.0.25";
public static final int SERVERPORT = 4000;


public void run(){
try {
    InetAddress serverAddr = InetAddress.getByName(SERVERIP);

    Socket socket = new Socket(serverAddr, SERVERPORT);

    String swon = "A55A6B0550000000FFFBDE0030C8";
    String but0 = "A55A6B0500000000FFFBDE002066";

    PrintWriter out = new PrintWriter( new BufferedWriter
            ( new OutputStreamWriter(socket.getOutputStream())),true);

    out.println(swon);
    out.println(but0);
    socket.close();
} catch(Exception e) {
}
    finally {

    }
}
}

按钮关闭实际上是相同的,但使用不同的字符串。 我是 android 编码(和 java 编码)的新手,所以看不出问题在哪里。我需要在某处冲洗琴弦吗?或者有更好的方法来处理这个项目吗?

干杯

My project has a light switch which has an IP address and port and accepts strings to control. I am trying to create an application to switch this on and off.
My code works, however after the first click, there is a significant delay between the next click and the switch actually switching.

I have a button for on and off. main.java:

package com.android.lswitch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class lightswitch extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Handle swon button
    Button swon = (Button) findViewById(R.id.swon);
    swon.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sw(true);
        }
    });

    // Handle swoff button
    Button swoff = (Button) findViewById(R.id.swoff);
    swoff.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sw(false);
        }
    });


}

private void sw(boolean swstate) {
    if (swstate == true) {
        Thread swonThread = new Thread(new swon());
        swonThread.start();    
    }

    if (swstate == false) {
    Thread swoffThread = new Thread(new swoff());
    swoffThread.start();    
    }

}
}

and button on java:
package com.android.lswitch;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class swon implements Runnable {
public static final String SERVERIP = "10.0.0.25";
public static final int SERVERPORT = 4000;


public void run(){
try {
    InetAddress serverAddr = InetAddress.getByName(SERVERIP);

    Socket socket = new Socket(serverAddr, SERVERPORT);

    String swon = "A55A6B0550000000FFFBDE0030C8";
    String but0 = "A55A6B0500000000FFFBDE002066";

    PrintWriter out = new PrintWriter( new BufferedWriter
            ( new OutputStreamWriter(socket.getOutputStream())),true);

    out.println(swon);
    out.println(but0);
    socket.close();
} catch(Exception e) {
}
    finally {

    }
}
}

button off is virtually the same but with different strings.
I'm new to android coding (and java coding) so can't see where the hold up is. Do I need to flush the strings somewhere? or is there a better way of tackling this project?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-11-16 07:59:16

PrintWriter 可能会被缓冲,因此在对底层套接字发出任何 close() 操作之前,您一定应该对其进行 flush() 操作。

最好在 finally 块中调用 PrintWriter.close() 而不是 Socket.close(),例如:

PrintWriter out = new PrintWriter( new BufferedWriter
        ( new OutputStreamWriter(socket.getOutputStream())),true);

try {
  out.println(swon);
  out.println(but0);
  out.flush();
} finally {
  out.close();
}

PrintWriter may be buffered, so you should definitely flush() it before issuing any close() operation on the underlying socket.

It is also preferable to call PrintWriter.close() instead of Socket.close(), e.g., in a finally block, such as:

PrintWriter out = new PrintWriter( new BufferedWriter
        ( new OutputStreamWriter(socket.getOutputStream())),true);

try {
  out.println(swon);
  out.println(but0);
  out.flush();
} finally {
  out.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文