Клиент
public void Conect(View view) {
Thread t = new Thread(new Runnable() {
public void run() {
String data = "привет клиент";
Socket client = null;
int r = 0;
try {
client = new Socket(java.net.InetAddress.getByName("192.168.43.216"),53211);
} catch (UnknownHostException e) {
h.sendEmptyMessage(1);
} catch (IOException e) {
h.sendEmptyMessage(2);
}
catch (NetworkOnMainThreadException e) {
h.sendEmptyMessage(3);
}
if (client !=null)
h.sendEmptyMessage(4);
else h.sendEmptyMessage(5);
try {
client.getOutputStream().write(toBase64().getBytes());
} catch (IOException e) {
h.sendEmptyMessage(6);
}
byte[] buf = new byte[64 * 1024];
try {
r = client.getInputStream().read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
h.sendEmptyMessage(7);
}
s_data = new String(buf, 0, r);
// h.sendEmptyMessage(8);
try {
client.close();
}catch (IOException e)
{
}
}
});
textView.setText(s_data);
t.start();
}
public String toBase64()
{
Bitmap bm=BitmapFactory.decodeResource(getResources(),
R.drawable.sts);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}