一,简介
创建Socket,连接服务器地址和端口号,进行发送信息。
二,实现
启动线程发送信息
data = et.getText().toString();
if(data == null){
Toast.makeText(MainActivity.this,"please input Sending Data", Toast.LENGTH_SHORT).show();
}else {
//在后面加上 '\0' ,是为了在服务端方便我们去解析;
data = data + '\0';
}
Thread thread = new Thread(){
@Override
public void run() {
super.run();
try {
sockets = new Socket(ip,8000);
outputStream = sockets.getOutputStream();
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();