forked from pjohansson77/Remote-Lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoClient.java
51 lines (43 loc) · 1.24 KB
/
ArduinoClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package test;
import java.net.*;
import java.io.*;
/**
* A client class that let's the computer talk to the Arduino server.
* @author Jesper Hansen, Peter Johansson, Andree Höög, Qasim Ahmad, Andreas Flink, Gustav Frigren
*
*/
public class ArduinoClient {
private String serverIP;
private int serverPort;
private int message;
/**
* The class constructor that set's the servers IP and port number from the Server class.
* @param serverIP is the IP number to the Arduino
* @param serverPort is the port number to the Arduino
*/
public ArduinoClient(String serverIP, int serverPort) {
this.serverIP = serverIP;
this.serverPort = serverPort;
}
/**
* A method that print the clients message to the arduino.
* @param message The message from the client to the arduino.
*/
public void TalkToArduino (int message) {
DataOutputStream output;
Socket socket = null;
try {
socket = new Socket( InetAddress.getByName( serverIP ), serverPort );
output = new DataOutputStream(socket.getOutputStream());
output.write( message ); // Message to the Arduino
output.flush();
} catch(Exception e1 ) {
System.out.println( e1 );
}
try {
socket.close();
} catch( IOException e ) {
System.out.println( e );
}
}
}