Exemplo n.º 1
0
        public bool SendCommandToServer(string serverIp, BaseCommand command, ClientCommandType clientCommandType)
        {
            var commandHeader = new ClientCommandHeader
            {
                Type = clientCommandType
            };

            var client = new TcpClient();

            try
            {
                client.Connect(serverIp, Settings.ServerPort);

                // + составить байты из ClientHeader и сериализации команды (+prepend with length)
                byte[] messageBytes           = CommandUtils.ConcatByteArrays(commandHeader.ToBytes(), command.ToBytes());
                byte[] messageBytesWithLength = CommandUtils.PrependLengthBytes(messageBytes);

                // + отправить байты в NetworkStream
                NetworkStream networkStream = client.GetStream();
                networkStream.Write(messageBytesWithLength, 0, messageBytesWithLength.Length);

                // + Обработать ответ
                HandleServerResponse(client);
            }
            catch (SocketException)
            {
                Error("Cannot sent command to server");
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public static void SendQueryCommand(QueryCommand queryCommand)
        {
            var commandHeader = new CommandHeader
            {
                Count = 1,
                Type  = (int)CommandTypeEnum.QueryCommand
            };


            byte[] commandBytes = CommandUtils.ConcatByteArrays(commandHeader.ToBytes(), queryCommand.ToBytes());
            SendAnswer(IP, Port, commandBytes);
        }