Guest User

Untitled

a guest
Jan 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.ComponentModel;
  10.  
  11. namespace restarter
  12. {
  13. class Program
  14. {
  15.  
  16. static void Main()
  17. {
  18.  
  19. loadConfig();
  20. // First interval = 1000ms; subsequent intervals = 15mins
  21. Timer timer = new Timer(Tick, "Time to check", 1000, 900000);
  22. Console.ReadLine();
  23. timer.Dispose();
  24. }
  25.  
  26. static void Tick(object data)
  27. {
  28. Console.WriteLine(data);
  29. checkifdown();
  30. }
  31.  
  32.  
  33. static String startupFile;
  34. static IPAddress address;
  35. static int port;
  36.  
  37. static void loadConfig()
  38. {
  39. try
  40. {
  41. // Create an instance of StreamReader to read from a file.
  42. // The using statement also closes the StreamReader.
  43. using (StreamReader sr = new StreamReader("config.txt"))
  44. {
  45. String line;
  46. // Read and display lines from the file until the end of
  47. // the file is reached.
  48. while ((line = sr.ReadLine()) != null)
  49. {
  50. if (line.StartsWith("serverIP="))
  51. address = IPAddress.Parse(line.Substring(9));
  52. if (line.StartsWith("port="))
  53. port = Int32.Parse(line.Substring(5));
  54. if(line.StartsWith("run="))
  55. startupFile = line.Substring(4);
  56.  
  57. }
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. // Let the user know what went wrong.
  63. //Console.WriteLine("The file could not be read:");
  64. Console.WriteLine(e.Message);
  65. }
  66. }
  67.  
  68.  
  69. static void checkifdown()
  70. {
  71.  
  72. TcpClient client;
  73. try
  74. {
  75.  
  76. client = new TcpClient();
  77.  
  78. client.Connect(new IPEndPoint(address, port));
  79.  
  80.  
  81. byte[] garbagehandshake = { 0, 9, 0, 80, 0, 108, 0, 97, 0, 121, 0, 101, 0, 114, 0, 57, 0, 51, 0, 54 };
  82. NetworkStream stream = client.GetStream();
  83. stream.Write(garbagehandshake, 0, garbagehandshake.Length);
  84.  
  85. Console.WriteLine("Server seems to be up! ;D");
  86. //were not expecting a responce as it will disconnect us with a protocol error
  87.  
  88. client.Close();
  89. }
  90. catch (Exception e)
  91. {
  92. Console.WriteLine(e.Message);
  93. Console.WriteLine("Server down Q_Q.. restating it");
  94.  
  95. foreach (Process p in System.Diagnostics.Process.GetProcessesByName("java"))
  96. {
  97. try
  98. {
  99. p.Kill();
  100. p.WaitForExit(); // possibly with a timeout
  101. }
  102. catch (Exception e2)
  103. {
  104. }
  105. }
  106.  
  107. foreach (Process p in System.Diagnostics.Process.GetProcessesByName("javaw"))
  108. {
  109. try
  110. {
  111. p.Kill();
  112. p.WaitForExit(); // possibly with a timeout
  113. }
  114. catch (Exception e2)
  115. {
  116. }
  117. }
  118.  
  119. Console.WriteLine("Launching server now");
  120. Console.WriteLine(startupFile);
  121. System.Diagnostics.Process.Start(startupFile);
  122.  
  123. }
  124.  
  125.  
  126. }
  127. }
  128. }
Add Comment
Please, Sign In to add comment