标签:
服务器代码单元测试:
package com.qilu.iot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText send_data_edittext;
private TextView data_show;
private ServerSocket serversocket;
private Handler mhander;
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mhander=new Handler(){
public void handleMessage(Message msg) {
Bundle bundle=msg.getData();
String message=bundle.getString("message");
data_show.setText(message);
};
};
send_data_edittext = (EditText) findViewById(R.id.send_data_edittext);
data_show = (TextView) findViewById(R.id.data_show);
init();
}
@Test
private void init() {
// TODO Auto-generated method stub
System.out.println("客户端正在连接。。。。");
try {
serversocket = new ServerSocket(1234);
new Thread(new Runnable() {
public void run() {
while (true) {
// TODO Auto-generated method stub
try {
socket = serversocket.accept();
if(socket!=null){
System.out.println("客户端连接成功。。。");
InputStream s=socket.getInputStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(
s,"GBK"));
System.out.println("客户端数据接收成功。。。");
String strinfo=null;
while(!((strinfo=buffer.readLine())==null)){
System.out.println("客户端数据接收成功,接收的数据为:" + strinfo);
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("message",strinfo);
message.setData(bundle);
mhander.sendMessage(message);
}
buffer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
客户端单元测试:
package com.client.androidclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private EditText data_send;
private Socket socket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data_send=(EditText) findViewById(R.id.data_send);
Button button = (Button) this.findViewById(R.id.send_button);
System.out.println("正在连接。。。。");
new Thread(new Runnable() {
@Override
@Test
public void run() {
// TODO Auto-generated method stub
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName("192.168.43.209");
socket = new Socket(serverAddr, 1234);
System.out.println("连接成功。。。。。。。。");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//IP地址和端口号(对应服务端),我这的IP是本地路由器的IP地址
new Thread(new Runnable() {
@Override
@Test
public void run() {
// TODO Auto-generated method stub
try {
//发送给服务端的消息
String message = "本消息来自于安卓客户端";
System.out.println("安卓客户端发送消息: ‘" + data_send.getText().toString() + "‘");
//第二个参数为True则为自动flush
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream(),"GBK")));
out.println(data_send.getText().toString());
out.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}
客户端socket Junit4测试时间:60%
服务器socket Junit4 测试时间:40%
Android--socket连接无线模块获取传感器数据------JUnit4单元测试
标签:
原文地址:http://www.cnblogs.com/zxhong/p/4786918.html