public Message Execute(String command) { IPHostEntry ipHost = null; try { ipHost = Dns.GetHostEntry(ServerIp); } catch (Exception ex) { MessageBox.Show("Cannot connect to IP"); return(null); } IPAddress ipAddr = ipHost.AddressList[0]; ipEndPoint = new IPEndPoint(ipAddr, 11000); sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sender.Connect(ipEndPoint); byte[] bytes = new byte[32768]; byte[] msg = Encoding.UTF8.GetBytes(command); // Отправляем данные через сокет int bytesSent = sender.Send(msg); // Получаем ответ от сервера int bytesRec = sender.Receive(bytes); Object reply = SerializeUtils.ByteArrayToObject(bytes); return((Message)reply); }
private static void SendData(Socket handler, Message response) { byte[] msg = SerializeUtils.ObjectToByteArray(response); handler.Send(msg); }