Guest User

Untitled

a guest
Dec 14th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System.Net.Sockets;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace ConsoleChatClient
  9. {
  10. class Program
  11. {
  12. static string UserName;
  13. private static string Host;
  14. private static int Port;
  15. static TcpClient Client;
  16. static NetworkStream Stream;
  17.  
  18. static void InitializeSocket()
  19. {
  20. try
  21. {
  22. Client.Connect(Host, Port);
  23. Stream = Client.GetStream();
  24. }
  25. catch(Exception ex)
  26. {
  27. Console.WriteLine(ex.Message);
  28. }
  29. }
  30.  
  31. static void Disconnect()
  32. {
  33. if(Stream != null)
  34. {
  35. Stream.Close();
  36. }
  37. if(Client != null)
  38. {
  39. Client.Close();
  40. }
  41. Environment.Exit(0);
  42. }
  43.  
  44. static void RecieveMessages()
  45. {
  46. while(true)
  47. {
  48. try
  49. {
  50. byte[] data = new byte[64];
  51. StringBuilder Builder = new StringBuilder();
  52. int bytes = 0;
  53.  
  54. do
  55. {
  56. bytes = Stream.Read(data, 0, data.Length);
  57. Builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
  58. }
  59. while (Stream.DataAvailable);
  60.  
  61. String message = Builder.ToString();
  62. Console.WriteLine(message);
  63. }
  64. catch
  65. {
  66. Console.WriteLine("Connection closed!");
  67. Console.ReadLine();
  68. Disconnect();
  69. }
  70. }
  71. }
  72.  
  73. static void InitializeConnection()
  74. {
  75. Thread InputThread = new Thread(RecieveMessages);
  76. InputThread.Start();
  77. byte[] UserNameBuffer = Encoding.Unicode.GetBytes(UserName);
  78. try
  79. {
  80. Stream.Write(UserNameBuffer, 0, UserNameBuffer.Length);
  81. }
  82. catch
  83. {
  84. Disconnect();
  85. }
  86. }
  87.  
  88. static void SendMessages()
  89. {
  90. String Message = "";
  91.  
  92. while (Message.ToLower() != "@exit")
  93. {
  94. Message = Console.ReadLine().Trim();
  95. if(String.IsNullOrEmpty(Message))
  96. {
  97. continue;
  98. }
  99.  
  100. byte[] WriteBuffer = Encoding.Unicode.GetBytes(Message);
  101. try
  102. {
  103. Stream.Write(WriteBuffer, 0, WriteBuffer.Length);
  104. }
  105. catch
  106. {
  107. Console.WriteLine("[ERROR] Some error occured. Maybe try again?");
  108. }
  109. }
  110. }
  111.  
  112. static void SendPingPong()
  113. {
  114. String Message = "PING";
  115. byte[] buffer = Encoding.Unicode.GetBytes(Message);
  116. try
  117. {
  118. Stream.Write(buffer, 0, buffer.Length);
  119. }
  120. catch (Exception ex)
  121. {
  122. Console.WriteLine(ex.Message);
  123. }
  124.  
  125. }
  126.  
  127. static void Init(bool IsRestarting)
  128. {
  129. if(IsRestarting)
  130. {
  131.  
  132. }
  133.  
  134. Console.Write("Enter server IP:");
  135. Host = Console.ReadLine();
  136. Console.Write("Enter server port:");
  137. Port = Convert.ToInt32(Console.ReadLine());
  138. Console.Write("Enter ur username:");
  139. UserName = Console.ReadLine();
  140. Client = new TcpClient();
  141.  
  142. InitializeSocket();
  143. InitializeConnection();
  144. SendMessages();
  145. Disconnect();
  146. }
  147.  
  148. static void Main(string[] args)
  149. {
  150. Init(false);
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment