Skip to content
Snippets Groups Projects
Commit a6d6fffa authored by Aylen Ricca's avatar Aylen Ricca
Browse files

modified to have a client on the device and configure server ip to send data

parent 6bafaf1e
No related branches found
No related tags found
No related merge requests found
package uy.edu.chesstrack.communication;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;
public class Client extends Activity {
public class Client {
private Socket socket;
private static final String TAG = "CLIENT";
private Socket serverSocket;
//private BufferedReader input;
private DataOutputStream output;
private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";
public static final int SERVERPORT = 5555;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
public Client(String serverIp) {
Log.i(TAG, "init client-server communication");
try {
InetAddress serverAddr = InetAddress.getByName(serverIp);
serverSocket = new Socket(serverAddr, SERVERPORT);
Log.i(TAG, "Server on " + serverIp + ":" + SERVERPORT);
new Thread(new ClientThread()).start();
// get stream to send data
this.output = new DataOutputStream(this.serverSocket.getOutputStream());
this.SendData(new String("Aylen Ricca Cambon\n"));
// get stream to receive data
//this.input = new BufferedReader(new InputStreamReader(this.serverSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(View view) {
public void SendData(String msg) {
try {
// EditText et = (EditText) findViewById(R.id.EditText01);
// String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
//out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
Log.i(TAG, "sending="+ msg);
this.output.writeBytes(msg);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
/*
public String ReceiveData() {
try {
String read = input.readLine();
Log.i(TAG, "received="+ read);
return read;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
*/
public void Stop() {
try {
serverSocket.close();
output.close();
//input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
}
package uy.edu.chesstrack.communication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class Server {
private static final String TAG = "SERVER";
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedReader input;
private DataOutputStream output;
public static final int SERVERPORT = 5555;
public Server(String serverIp) {
clientSocket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
clientSocket = serverSocket.accept();
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
String read = input.readLine();
Log.i(TAG, "line="+ read);
this.output = new DataOutputStream(this.clientSocket.getOutputStream());
this.output.writeBytes(new String("AYLEN RICCA"));//(read.toCharArray());
} catch (IOException e) {
e.printStackTrace();
}
}
public void SendData() {
try {
serverSocket.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Stop() {
try {
serverSocket.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import uy.edu.chesstrack.communication.Server;
import uy.edu.chesstrack.communication.Client;
import uy.edu.fing.chesstrack.modulovision.Adquisicion;
import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.app.Activity;
......@@ -18,17 +18,13 @@ import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ChessTrackActivity extends Activity implements CvCameraViewListener {
......@@ -39,8 +35,7 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
private Adquisicion adq;
private Calibracion calibrar;
private Button btnCalibrar;
private Server ServidorCommunication;
private EditText text;
private Client ClientCommunication;
//TODO es chancho pero ver luego
private Mat frame;
......@@ -89,11 +84,10 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
// TODO Auto-generated catch block
e.printStackTrace();
}
getIp();
getServerIp();
}
public void getIp(){
public void getServerIp(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Communication Setup");
alert.setMessage("Set server ip");
......@@ -106,15 +100,17 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
Log.i(TAG,"INPUT=" + value.toString());
ServidorCommunication = new Server(value.toString());
ClientCommunication = new Client(value.toString());
}
});
/*
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// no server communication
// no server communication -- FIXME do not send info to client
}
});
*/
alert.show();
}
......@@ -166,7 +162,7 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
this.ServidorCommunication.Stop();
this.ClientCommunication.Stop();
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment