Last active
February 11, 2017 06:58
-
-
Save victoryforphil/346f50ffb88bbe98b52871feba07e7af to your computer and use it in GitHub Desktop.
TCP Code Tests - Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net') | |
var port = 7777; | |
var server = net.createServer(); | |
server.listen(port, function(err,err2){ | |
console.log(err2); | |
}); | |
server.on('connection', function(socket) { //This is a standard net.Socket | |
console.log("Conneted!"); | |
socket.on('data', function(message) { | |
console.log(message.toString()); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.firstinspires.ftc.teamcode; | |
import android.util.Log; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.net.InetAddress; | |
import java.net.Socket; | |
public class TCPClient { | |
public static final String SERVER_IP = "10.0.0.7"; //server IP address | |
public static final int SERVER_PORT = 7777; | |
// message to send to the server | |
private String mServerMessage; | |
// sends message received notifications | |
private OnMessageReceived mMessageListener = null; | |
// while this is true, the server will continue running | |
private boolean mRun = false; | |
// used to send messages | |
private PrintWriter mBufferOut; | |
// used to read messages from the server | |
private BufferedReader mBufferIn; | |
/** | |
* Constructor of the class. OnMessagedReceived listens for the messages received from server | |
*/ | |
public TCPClient(OnMessageReceived listener) { | |
mMessageListener = listener; | |
} | |
/** | |
* Sends the message entered by client to the server | |
* | |
* @param message text entered by client | |
*/ | |
public void sendMessage(String message) { | |
if (mBufferOut != null && !mBufferOut.checkError()) { | |
mBufferOut.println(message); | |
mBufferOut.flush(); | |
} | |
} | |
/** | |
* Close the connection and release the members | |
*/ | |
public void stopClient() { | |
mRun = false; | |
if (mBufferOut != null) { | |
mBufferOut.flush(); | |
mBufferOut.close(); | |
} | |
mMessageListener = null; | |
mBufferIn = null; | |
mBufferOut = null; | |
mServerMessage = null; | |
} | |
public void run() { | |
mRun = true; | |
try { | |
//here you must put your computer's IP address. | |
InetAddress serverAddr = InetAddress.getByName(SERVER_IP); | |
Log.e("TCP Client", "C: Connecting..."); | |
//create a socket to make the connection with the server | |
Socket socket = new Socket(serverAddr, SERVER_PORT); | |
try { | |
//sends the message to the server | |
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); | |
//receives the message which the server sends back | |
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
//in this while the client listens for the messages sent by the server | |
while (mRun) { | |
mServerMessage = mBufferIn.readLine(); | |
if (mServerMessage != null && mMessageListener != null) { | |
//call the method messageReceived from MyActivity class | |
mMessageListener.messageReceived(mServerMessage); | |
} | |
} | |
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'"); | |
} catch (Exception e) { | |
Log.e("TCP", "S: Error", e); | |
} finally { | |
//the socket must be closed. It is not possible to reconnect to this socket | |
// after it is closed, which means a new socket instance has to be created. | |
socket.close(); | |
} | |
} catch (Exception e) { | |
Log.e("TCP", "C: Error", e); | |
} | |
} | |
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity | |
//class at on asynckTask doInBackground | |
public interface OnMessageReceived { | |
public void messageReceived(String message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.firstinspires.ftc.teamcode; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.qualcomm.robotcore.eventloop.opmode.Autonomous; | |
import com.qualcomm.robotcore.eventloop.opmode.Disabled; | |
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | |
import com.qualcomm.robotcore.eventloop.opmode.TeleOp; | |
import com.qualcomm.robotcore.hardware.DcMotor; | |
import com.qualcomm.robotcore.util.ElapsedTime; | |
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | |
import com.qualcomm.robotcore.eventloop.opmode.TeleOp; | |
import com.qualcomm.robotcore.eventloop.opmode.Disabled; | |
import com.qualcomm.robotcore.hardware.DcMotor; | |
import com.qualcomm.robotcore.hardware.DcMotorSimple; | |
import com.qualcomm.robotcore.util.ElapsedTime; | |
import org.firstinspires.ftc.robotcore.external.Telemetry; | |
@Autonomous(name="Titan Planner", group="Linear Opmode") // @Autonomous(...) is the other common choice | |
public class TitanPlanner extends LinearOpMode { | |
private ElapsedTime runtime = new ElapsedTime(); | |
TCPClient mTcpClient; | |
@Override | |
public void runOpMode() { | |
telemetry.addData("Status", "Initialized"); | |
telemetry.update(); | |
waitForStart(); | |
runtime.reset(); | |
new ConnectTask().execute(""); | |
while (opModeIsActive()) { | |
telemetry.addData("Status", "Run Time: " + runtime.toString()); | |
telemetry.update(); | |
if (mTcpClient != null) { | |
mTcpClient.sendMessage( runtime.toString()); | |
}else{ | |
telemetry.addData("Connection", "Null"); | |
} | |
} | |
} | |
public class ConnectTask extends AsyncTask<String, String, TCPClient> { | |
@Override | |
protected TCPClient doInBackground(String... message) { | |
//we create a TCPClient object | |
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() { | |
@Override | |
//here the messageReceived method is implemented | |
public void messageReceived(String message) { | |
//this method calls the onProgressUpdate | |
publishProgress(message); | |
} | |
}); | |
mTcpClient.run(); | |
return null; | |
} | |
@Override | |
protected void onProgressUpdate(String... values) { | |
super.onProgressUpdate(values); | |
//response received from server | |
telemetry.addData("test", "response " + values[0]); | |
//process server response here.... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment