Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spendingtracker.readdeo.spendingtracker;
- import android.content.Intent;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.UUID;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onResume() {
- super.onResume();
- new Thread(new ClientThread()).start();
- updateConversationHandler = new Handler();
- this.serverThread = new Thread(new ServerThread());
- this.serverThread.start();
- }
- @Override
- protected void onPause() {
- super.onPause();
- /*
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- socketC.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- }
- private ServerSocket serverSocket;
- Handler updateConversationHandler;
- Thread serverThread = null;
- private static final int CLIENTPORT = 5000;
- Button button3, button4;
- TextView text;
- private Socket socketC;
- private static final int SERVERPORT = 8000;
- private static final String SERVER_IP = "195.228.24.3";
- TextView textview7, textview2, textview3, textview4, textview5, textview6;
- Button button1, button2;
- EditText et1, et2, et3, et4, et5, et6;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textview7 = (TextView) findViewById(R.id.textView7);
- Button button1 = (Button) findViewById(R.id.button);
- Button button2 = (Button) findViewById(R.id.button2);
- // new Thread(new ClientThread()).start();
- updateConversationHandler = new Handler();
- this.serverThread = new Thread(new ServerThread());
- this.serverThread.start();
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(MainActivity.this, Main2Activity.class));
- /*
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- socketC.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- }
- });
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- // new Thread(new ClientThread()).start();
- String uniqueID = UUID.randomUUID().toString();
- long unixTime = System.currentTimeMillis() / 1000L;
- EditText et1 = (EditText) findViewById(R.id.editText);
- EditText et2 = (EditText) findViewById(R.id.editText2);
- EditText et3 = (EditText) findViewById(R.id.editText3);
- EditText et4 = (EditText) findViewById(R.id.editText4);
- EditText et5 = (EditText) findViewById(R.id.editText5);
- EditText et6 = (EditText) findViewById(R.id.editText6);
- ///
- String str1 = et1.getText().toString();
- String str2 = et2.getText().toString();
- String str3 = et3.getText().toString();
- String str4 = et4.getText().toString();
- String str5 = et5.getText().toString();
- String str6 = et6.getText().toString();
- if (str1 != null && !str1.isEmpty());{str1 = "0";};
- if (str2 != null && !str2.isEmpty());{str2 = "0";};
- if (str3 != null && !str3.isEmpty());{str3 = "0";};
- if (str4 != null && !str4.isEmpty());{str4 = "0";};
- if (str5 != null && !str5.isEmpty());{str5 = "0";};
- if (str6 != null && !str6.isEmpty());{str6 = "0";};
- String Msg = "WriteNewSpend,Readdeo,"+unixTime+","+str1+","+str2+","
- +str3+","+str4+","+str5+","+str6+","+uniqueID;
- PrintWriter out = null;
- try {
- out = new PrintWriter(new BufferedWriter(
- new OutputStreamWriter(socketC.getOutputStream())),
- true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- out.println(Msg);
- }
- finally {
- /*
- try {
- socketC.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- // new Thread(new ClientThread()).start();
- }
- }
- });
- }
- class ClientThread implements Runnable {
- @Override
- public void run() {
- try {
- // InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
- socketC = new Socket("192.168.1.110", 5000);
- } catch (UnknownHostException e1) {
- e1.printStackTrace();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- /*
- public void sendMessageToServer(String message) {
- if (message != null) {
- try {
- //Assume I already have an instance of client Socket:
- //Socket outgoingConn = new Socket(host, port)
- //1. I get the OutputStream of my outgoing connection
- OutputStream outStream = socket.getOutputStream();
- //2. Create an object output stream
- ObjectOutputStream objectWriter = new ObjectOutputStream(outStream);
- //3. Write the object
- objectWriter.writeObject(message);
- //Close the io if required (would recommend try-with-resources if using jdk7)
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- */
- class ServerThread implements Runnable {
- public void run() {
- Socket socket = null;
- try {
- InetAddress srvAddr = InetAddress.getByName("0.0.0.0");
- serverSocket = new ServerSocket(SERVERPORT);
- } catch (IOException e) {
- e.printStackTrace();
- }
- while (!Thread.currentThread().isInterrupted()) {
- try {
- socket = serverSocket.accept();
- CommunicationThread commThread = new CommunicationThread(socket);
- new Thread(commThread).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class CommunicationThread implements Runnable {
- private Socket clientSocket;
- private BufferedReader input;
- public CommunicationThread(Socket clientSocket) {
- this.clientSocket = clientSocket;
- try {
- this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- while (!Thread.currentThread().isInterrupted()) {
- try {
- String read = input.readLine();
- if(read != null)
- {
- updateConversationHandler.post(new updateUIThread(read));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class updateUIThread implements Runnable {
- private String msg;
- public updateUIThread(String str) {
- this.msg = str;
- }
- @Override
- public void run() {
- textview7.setText("\r\n"+"Client Says: "+ msg + textview7.getText().toString()+ "\r\n"
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment