Skip to content

Instantly share code, notes, and snippets.

@williamtoader
Created July 31, 2019 12:40
Show Gist options
  • Save williamtoader/7a96d40c3eeb00d44665d070290a889e to your computer and use it in GitHub Desktop.
Save williamtoader/7a96d40c3eeb00d44665d070290a889e to your computer and use it in GitHub Desktop.
TCP Client handling
package org.firstinspires.ftc.teamcode.apis.core_lib;
import org.firstinspires.ftc.robotcore.internal.camera.libuvc.nativeobject.VuforiaExternalProviderCameraFrame;
import java.net.ConnectException;
import java.io.*;
import java.net.*;
public class HelixLinkService {
public boolean connected = false;
Socket clientSocket = null;
PrintWriter outToServer = null;
BufferedReader inFromServer = null;
public void connect(String host, int port) throws IOException {
clientSocket = new Socket(host, port);
outToServer = new PrintWriter(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
connected = clientSocket.isConnected();
}
public void issueCommand(String command) {
outToServer.println(command);
outToServer.flush();
}
public String query(String command) {
try {
issueCommand(command);
return inFromServer.readLine();
}
catch (IOException e) {
return "FAIL";
}
}
public void close() {
try {
inFromServer.close();
outToServer.close();
clientSocket.close();
}
catch (IOException e) {
System.out.println("Falied to close TCP socket to HELIX LINK");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment