消除通过 tcp/ip 发送字符串的延迟
我的项目有一个灯开关,它有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PrintWriter
可能会被缓冲,因此在对底层套接字发出任何close()
操作之前,您一定应该对其进行flush()
操作。最好在
finally
块中调用PrintWriter.close()
而不是Socket.close()
,例如:PrintWriter
may be buffered, so you should definitelyflush()
it before issuing anyclose()
operation on the underlying socket.It is also preferable to call
PrintWriter.close()
instead ofSocket.close()
, e.g., in afinally
block, such as: