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
|
class ThreadClient implements Runnable {
private Socket socket;
private String messageClient;
private BufferedReader input;
private PrintWriter output;
public CommunicationThread(String messageClient) {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
this.socket = new Socket(serverAddr, SERVERPORT);
this.messageClient = messageClient;
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
//envoie le message
try {
output.print(messageClient);
output.flush();
//attend la réception
if (input.readLine() != null) {
//message, variable de ton activité, readLine() est bloquant
message = mBufferIn.readLine();
tu test si ton message te correspond
//lance ton activité
launchActivity();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager