29 namespace Ntp.Analyzer.Monitor.Client
45 public string Send(
string command)
47 var clientSocket =
new Socket(
48 AddressFamily.InterNetwork,
54 IAsyncResult conRes = clientSocket.BeginConnect(Server, null, null);
55 bool success = conRes.AsyncWaitHandle.WaitOne(3000,
true);
59 throw new ApplicationException(
"Failed to connect to server.");
62 byte[] sendBuffer = Encoding.UTF8.GetBytes(command);
65 clientSocket.Send(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, out error);
67 const int size = 4096;
69 result =
new StringBuilder(4096);
70 var buffer =
new byte[size];
76 length = clientSocket.Receive(buffer, 0, size, SocketFlags.None, out error);
77 string response = Encoding.ASCII.GetString(buffer);
78 response = response.Substring(0, length);
79 result.Append(response);
82 return result.ToString();
86 Debug.WriteLine(ex.Message);
87 return "Failed to connect to server.";
91 if (clientSocket.IsBound)
92 clientSocket.Shutdown(SocketShutdown.Both);
TextRequest(IPAddress ip, int port)
TextRequest(IPEndPoint server)
string Send(string command)