Android App für SRCP gesteuerte Modelleisenbahnen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

76 lignes
2.4KB

  1. package de.erich.railcontrol;
  2. import java.io.*;
  3. import java.net.*;
  4. class CommunicationClient
  5. {
  6. String sentence;
  7. String modifiedSentence;
  8. Socket clientSocket;
  9. InputStreamReader isr;
  10. BufferedReader inFromStdIn;
  11. DataOutputStream outToServer;
  12. boolean connected;
  13. public CommunicationClient() {
  14. Thread thread = new Thread(new Runnable() {
  15. @Override
  16. public void run() {
  17. String ip;
  18. int port;
  19. isr = new InputStreamReader(System.in);
  20. inFromStdIn = new BufferedReader(isr);
  21. if(MyActivity.PlaceholderFragment.ip_selector.isChecked()) {
  22. System.out.println("Online");
  23. if(ParseLoksConfiguration.ip == null || ParseLoksConfiguration.port == 0) {
  24. ip = "192.168.4.29";
  25. port = 4303;
  26. } else {
  27. ip = ParseLoksConfiguration.ip;
  28. port = ParseLoksConfiguration.port;
  29. }
  30. } else {
  31. System.out.println("Default");
  32. ip = "192.168.4.24";
  33. port = 6789;
  34. }
  35. System.out.println(ip + " " + port);
  36. try {
  37. clientSocket = new Socket(ip, port);
  38. System.out.println("Verbunden");
  39. connected = true;
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. connected = false;
  43. }
  44. }
  45. });
  46. thread.setPriority(1);
  47. thread.start();
  48. }
  49. public void sendMessage(String message) throws IOException {
  50. System.out.printf("Enter: ");
  51. outToServer = new DataOutputStream(clientSocket.getOutputStream());
  52. //BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  53. sentence = message;
  54. outToServer.writeBytes(sentence + '\n');
  55. //modifiedSentence = inFromServer.readLine();
  56. System.out.println("FROM SERVER: " + modifiedSentence);
  57. }
  58. public void killClient() throws IOException {
  59. System.out.println("Kill Client");
  60. sendMessage("." + "\n");
  61. clientSocket.close();
  62. System.out.println("CLient killed");
  63. }
  64. }