|
Re: FRC Java TCP client
You can definitely use streams on the Java cRIO. We have been using TCP for years on our robot.
Here is some code to help you get started
Code:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.StreamConnection;
import org.team2168.utils.Util;
private int port;
private String messageOut;
private volatile boolean clientConnected;
// A TCP Socket Connection
private ServerSocketConnection conn = null;
// TCP Socket Stream
private StreamConnection sc = null;
// Address Variable
private String addressIn = "socket://:" + port;
// Opens a socket to listen for incoming connections
try{
try {
conn = (ServerSocketConnection) Connector
.open(addressIn);
} catch (IOException e) {
e.printStackTrace();
}
// wait for a client to connect, this blocks until a connect
// is made
System.out.println("Listening on: "
+ conn.getLocalAddress() + " on port: "
+ conn.getLocalPort());
sc = conn.acceptAndOpen();
System.out.println("Client Connected");
} catch (IOException e) {
}
//To get the input stream
try {
InputStream is = null;
is = sc.openInputStream();
}catch (IOException x) {
x.printStackTrace();
}
//Example to get the output stream
OutputStream os = null;
try {
os = sc.openOutputStream();
while (true) {
//keep sending some message
messageOut = "Some Message";
buf = messageOut.getBytes("US_ASCII");
try {
os.write(buf);
} catch (IOException e) {
// e.printStackTrace();
System.out.println("Appears Client Closed "
+ "the Connection");
// close streams
os.close();
sc.close();
conn.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
For 2015: Instead of using ServerSocketConnection and SocketConnection, just use ServerSocket and Socket classes to create TCP streams. It is much simpler to create a server in JavaSE
Here is an example for 2015 roboRio:
Code:
// A TCP Socket Connection for RoboRio
private ServerSocket listener = null;
// TCP Socket Stream connection
private Socket client = null;
private int port;
try {
listener = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
// wait for a client to connect, this blocks the current thread until a connect
// is made
System.out.println("Listening on port: " + listener.getLocalPort());
client = listener.accept();
System.out.println("Client Connected");
//For the input stream, try the following:
try {
DataInputStream is = new DataInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
//And for the outputstream try the following
try {
DataOutputStream os = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
|