Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Created April 7, 2018 20:47
Show Gist options
  • Select an option

  • Save Bahaaib/e9bd6b36a92ea04b925ac7bd024dbc7b to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/e9bd6b36a92ea04b925ac7bd024dbc7b to your computer and use it in GitHub Desktop.
TCP Connection via Java Sockets /Client-Side implementaion
/**
* Created by Bahaa on 4/7/2018.
*/
package com.example.bahaa.clientapp;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientActivity extends AppCompatActivity {
private ImageView clientImage;
private TextView localIP, portNumber, bytesDecoded, responseTime;
private Socket clientSocket;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private String IP_ADDRESS = "192.168.1.10";
private int PORT_NUMBER = 13267;
private String base64Code;
private byte[] decodedString;
private Bitmap bitmap;
private long INITIAL_TIME = 0;
private long ELAPSED_TIME = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
clientImage = (ImageView) findViewById(R.id.client_img);
localIP = (TextView)findViewById(R.id.ip_tv);
portNumber = (TextView)findViewById(R.id.port_tv);
bytesDecoded = (TextView)findViewById(R.id.bytes_tv);
responseTime = (TextView)findViewById(R.id.time_tv);
INITIAL_TIME = System.currentTimeMillis();
new ReceiverTask().execute();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Receiving Data in Background..
class ReceiverTask extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params) {
try {
clientSocket = new Socket(IP_ADDRESS, PORT_NUMBER);
dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
dataInputStream = new DataInputStream(clientSocket.getInputStream());
base64Code = dataInputStream.readUTF();
Log.i("Client", base64Code);
//NO_WRAP:: Removes All line breakers from received String to avoid Decoding Corruption
decodedString = Base64.decode(base64Code, Base64.NO_WRAP);
//Converting the Decoded String back into image. Bitmap First!
bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
} catch (IOException e) {
e.printStackTrace();
}
return "Task Completed";
}
@Override
protected void onPostExecute(String result) {
//Rescale Bitmap to fit the imageView
bitmap = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
clientImage.setImageBitmap(bitmap);
localIP.setText(IP_ADDRESS);
portNumber.setText(String.valueOf(PORT_NUMBER));
bytesDecoded.setText(String.valueOf(base64Code.length()) + " Bytes");
ELAPSED_TIME = System.currentTimeMillis();
responseTime.setText(String.valueOf(ELAPSED_TIME - INITIAL_TIME) + "ms");
Log.i("Client", "Leaving PostExecute..");
}
@Override
protected void onProgressUpdate(Integer... values) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment