Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Diagnostics;
  9. namespace Client
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Stopwatch s = new Stopwatch();
  16. s.Reset();
  17. TcpClient client = null;
  18. (new Thread(new ThreadStart(() =>
  19. {
  20. Console.WriteLine("Connecting to server 127.0.0.1");
  21. while (true)
  22. {
  23. bool connected = false;
  24. while (!connected)
  25. {
  26. Console.WriteLine("Connecting to server 127.0.0.1");
  27. try
  28. {
  29. client = new TcpClient("127.0.0.1", 1234);
  30. connected = true;
  31. Console.WriteLine("Connected!");
  32. }
  33. catch { connected = false; }
  34. }
  35. while (true)
  36. {
  37. int bytesRead = 0;
  38. byte[] buff = new byte[1024 * 1024];
  39. try
  40. {
  41. bytesRead = 0;
  42. bytesRead = client.GetStream().Read(buff, 0, buff.Length);
  43. }
  44. catch { }
  45. if (bytesRead > 0)
  46. {
  47. string txt = System.Text.ASCIIEncoding.UTF8.GetString(buff, 0, bytesRead);
  48. Console.WriteLine("Received:" + txt);
  49. if (txt == "pong")
  50. {
  51. s.Stop();
  52. Console.WriteLine("Time elapsed: " + (((double)s.Elapsed.Milliseconds)/((double)1000)));
  53. s.Reset();
  54. }
  55. }
  56. }
  57. }
  58. }))).Start();
  59. string input="";
  60. while ((input = Console.ReadLine()) != "Exit")
  61. {
  62. if (input == "ping")
  63. s.Start();
  64. byte[] b=System.Text.ASCIIEncoding.UTF8.GetBytes(input);
  65. client.GetStream().Write(b, 0, b.Length);
  66. client.GetStream().Flush();
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement