您好,登錄后才能下訂單哦!
//聲明量
private String IPaddress="192.168.1.120";
private int port =50003;
private Handler mhandler =null;
private String cc ="";
private TextView returnvalue;
// 動態接收數據
mhandler = new Handler(){
public voidhandleMessage(Message msg)
{
if(msg.what== 1)
{
if(cc!=null||cc!="")
{
returnvalue.setText(cc);
cc=null;
}
}
super.handleMessage(msg);
}
};
//啟動相關線程
serverThread thread = new serverThread();
thread.start();
// 創建并發送數據
byte[] buf = {(byte)0x55,(byte)0xAA};
try {
socket = newSocket(this.IPaddress,this.port);
OutputStream out =socket.getOutputStream();
OutputStreamWriter writer =new OutputStreamWriter(out);
out.write(buf);
writer.flush();
Toast.makeText(PauseActivity.this,"成功",
Toast.LENGTH_SHORT).show();
} catch (UnknownHostExceptione) {
Toast.makeText(PauseActivity.this, "異常",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 接受數據
public class serverThread extendsThread {
publicvoid run() {
while(!Thread.currentThread().isInterrupted()){
if(socket==null)
{
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}else
{
try {
Thread.sleep(1000);
InputStreamips=socket.getInputStream();
InputStreamReader in = new InputStreamReader(ips);
in.ready();
cc ="";
for(inti= 0 ;i<2 ;i++)
{
cc+=ips.read()+"-";
}
} catch(InterruptedException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
Message msg = newMessage();
msg.what= 1;
mhandler.sendMessage(msg);
}
}
}
}
// 新人求關注
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。