Advertisement
Guest User

TelNetClient

a guest
Nov 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. // This file is part of TrinityCore Manager.
  2.  
  3. // TrinityCore Manager is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7.  
  8. // TrinityCore Manager is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12.  
  13. // You should have received a copy of the GNU General Public License
  14. // along with TrinityCore Manager. If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Net;
  22. using System.Net.Sockets;
  23.  
  24. namespace TrinityCore_Manager
  25. {
  26. class Client
  27. {
  28. private Socket client;
  29. private byte[] data = new byte[2048];
  30.  
  31. private string HOST = "Host HERE";
  32. private int PORT = 3443;
  33. private string USERNAME = "USER HERE";
  34. private string PASSWORD = "PASS HERE";
  35.  
  36.  
  37. public delegate void ClientConnectedEventHandler(object sender, EventArgs e);
  38. public delegate void ConnectionFailedEventHandler(object sender, EventArgs e);
  39.  
  40. public delegate void ReceiveFailedEventHandler(object sender, EventArgs e);
  41.  
  42. public delegate void MessageReceivedEventHandler(object sender, MessageReceivedEventArgs e);
  43.  
  44. public delegate void SentMessageFailedEventHandler(object sender, EventArgs e);
  45.  
  46. public event ClientConnectedEventHandler clientConnected;
  47. public event ConnectionFailedEventHandler connFailed;
  48.  
  49. public event ReceiveFailedEventHandler receiveFailed;
  50.  
  51. public event MessageReceivedEventHandler msgReceived;
  52.  
  53. public event SentMessageFailedEventHandler sentMSGFailed;
  54.  
  55. public class MessageReceivedEventArgs : EventArgs
  56. {
  57. public string message { get; set; }
  58.  
  59. public MessageReceivedEventArgs(string msg)
  60. {
  61. message = msg;
  62. }
  63. }
  64.  
  65. public Client(string host, int port, string username, string password)
  66. {
  67. HOST = host;
  68. PORT = port;
  69. USERNAME = username;
  70. PASSWORD = password;
  71. }
  72.  
  73. public void StartConnection()
  74. {
  75. try
  76. {
  77. if (client != null && client.Connected)
  78. client.Disconnect(true);
  79.  
  80.  
  81. IPAddress ipa = IPAddress.None;
  82.  
  83. IPAddress.TryParse(HOST, out ipa);
  84.  
  85. if (HOST == "localhost" || HOST == String.Empty)
  86. {
  87. ipa = IPAddress.Parse("127.0.0.1");
  88. }
  89. else
  90. {
  91. ipa = Dns.Resolve(HOST).AddressList[0];
  92. }
  93.  
  94. IPEndPoint ipe = new IPEndPoint(ipa, PORT);
  95.  
  96. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Create New Socket
  97. client.NoDelay = true;
  98.  
  99. client.BeginConnect(ipe, BeginConnect, client); //Begin Connection
  100. }
  101. catch (SocketException ex)
  102. {
  103. Console.WriteLine(ex.Message);
  104. }
  105. }
  106.  
  107. private void BeginConnect(IAsyncResult iar)
  108. {
  109. try
  110. {
  111. client = (Socket)iar.AsyncState;
  112.  
  113. client.EndConnect(iar);
  114.  
  115. data = new byte[2048];
  116.  
  117. if (clientConnected != null)
  118. clientConnected(this, new EventArgs());
  119.  
  120.  
  121. client.BeginReceive(data, 0, 2048, SocketFlags.None, BeginReceive, client); //Start Receiving
  122. }
  123. catch (SocketException ex)
  124. {
  125. if (connFailed != null)
  126. connFailed(this, new EventArgs());
  127.  
  128. Console.WriteLine(ex.Message);
  129. }
  130.  
  131. }
  132.  
  133. private void BeginReceive(IAsyncResult iar)
  134. {
  135. try
  136. {
  137. if (isConnected())
  138. {
  139. client = (Socket)iar.AsyncState;
  140.  
  141. int bytesRead = client.EndReceive(iar);
  142.  
  143. if (bytesRead != 0)
  144. {
  145. string message = Encoding.ASCII.GetString(data, 0, bytesRead);
  146.  
  147. msgReceived(this, new MessageReceivedEventArgs(message));
  148. }
  149.  
  150. client.BeginReceive(data, 0, 2048, SocketFlags.None, new AsyncCallback(BeginReceive), client);
  151. }
  152. }
  153. catch (SocketException ex)
  154. {
  155. Console.WriteLine(String.Format("{0} {1}", ex.Message, ex.ErrorCode));
  156.  
  157. if (receiveFailed != null)
  158. receiveFailed(this, new EventArgs());
  159. }
  160. }
  161.  
  162. private void SendData(IAsyncResult iar)
  163. {
  164. try
  165. {
  166. Socket socket = (Socket)iar.AsyncState;
  167.  
  168. int sent = socket.EndSend(iar);
  169. }
  170. catch (SocketException ex)
  171. {
  172. Console.WriteLine(ex.Message);
  173.  
  174. if (sentMSGFailed != null)
  175. sentMSGFailed(this, new EventArgs());
  176. }
  177. }
  178.  
  179. public void SendMessage(string message)
  180. {
  181. try
  182. {
  183.  
  184. if (!isConnected())
  185. return;
  186.  
  187. byte[] msg = Encoding.ASCII.GetBytes(message + "\n");
  188.  
  189. client.BeginSend(msg, 0, msg.Length, SocketFlags.None, SendData, client);
  190. }
  191. catch (SocketException ex)
  192. {
  193. Console.WriteLine(ex.Message);
  194. }
  195. }
  196.  
  197. public bool isConnected()
  198. {
  199. if (client != null && client.Connected)
  200. return true;
  201.  
  202. return false;
  203. }
  204.  
  205. public void Disconnect()
  206. {
  207. if (isConnected())
  208. {
  209. try
  210. {
  211. client.Shutdown(SocketShutdown.Both);
  212.  
  213. client.Disconnect(false);
  214.  
  215. //client.Shutdown(SocketShutdown.Both);
  216. //client.BeginDisconnect(true, new AsyncCallback(BeginDisconnection), client);
  217. }
  218. catch (Exception ex)
  219. {
  220. Console.WriteLine(ex.Message);
  221. }
  222. }
  223. }
  224.  
  225. private void BeginDisconnection(IAsyncResult iar)
  226. {
  227. try
  228. {
  229. client = (Socket)iar.AsyncState;
  230.  
  231. client.EndDisconnect(iar);
  232. }
  233. catch (Exception ex)
  234. {
  235. Console.WriteLine(ex.Message);
  236. }
  237. }
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement