Reputation: 695
I am running an application via a videogame environment (Unity, which is basically a C# library), on my Android device. I would like this application to respond to commands from my PC (preferably running Windows 8 but can be Linux:Ubuntu if easier). The device and the PC are on the same LAN, but are not attached by a physical cable. I need the device to respond to commands from the PC within 0.5 seconds of the command being issued.
Question: what is the easiest (conceptually and practically) way to establish such a connection between my PC and my device?
Sample scenario: I am running a game on my Android phone where spiders are crawling on my screen. When I hit space on my computer, I want all of the spiders to be swatted away.
Current solution: create a ruby-on-rails website & database. When a command is entered on the PC, the database is updated with the command. Every 0.5 seconds, the device checks the timestamp on the database and pulls any new commands. This solution is sub-optimal because I do not know ruby (I am willing to learn it, but I would like an easier solution).
Should I use C# sockets for this?
I would love some simple code that, for example, would open a direct connection between my PC and my device that will allow me to send byte streams (for instance, my PC could send the string "spacebar was pressed").
I am very uneducated regarding networks and would appreciated simple explanations.
Upvotes: 1
Views: 2612
Reputation: 1026
The best way to communicate with pc and mobile is a simple socket. Create a server socket at one side on a specific port number and connect it from other side. It's very fast (even less than 0.1 second)
Example:
The server (in mobile side)
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}
and client side (PC - C#)
class ClientThread implements Runnable
{
public void run()
{
try
{
Socket socket = new Socket(serverIpAddress, serverPort);
socket.setSoTimeout(5000);
while (true)
{
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.d("Nicklas", "Out it goes");
out.println(Command);
if (Command == "CMD:GetOptions<EOF>")
{
Log.d("Nicklas", "Getting options");
try
{
Log.d("Nicklas", "Line 1");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("Nicklas", "Line 2");
String answer = in.readLine();
Log.d("Nicklas", "answer = " + answer );
}
catch (Exception ee)
{
Log.d("Nicklasasasas", ee.toString());
}
}
break;
}
catch (Exception e)
{
Log.d("Nicklas", "CAE = " + e.toString());
break;
}
}
socket.close();
}
catch (ConnectException ee)
{
Log.d("Nicklas", "Kunne ikke forbinde");
}
catch (Exception e)
{
Log.d("Nicklasssssss", e.toString());
}
}
}
Sample code above copied from this SO and this SO threads.
Upvotes: 2