Пример #1
0
        private bool SendFileChunk(UDPDataChunk chunk)
        {
            var serialized = chunk.Serialize();
            var encoded    = Encoding.ASCII.GetBytes(serialized);

            socket.SendTo(encoded, sendingEp);
            var  result       = listener.Receive(ref receivingEp);
            var  hashReceived = Encoding.ASCII.GetString(result, 0, result.Length);
            long longResult;

            long.TryParse(hashReceived, out longResult);
            Console.WriteLine($"{chunk.HashSumChunk} == {longResult}");
            return(chunk.HashSumChunk == longResult);
        }
Пример #2
0
        private void HandleMessage(UDPDataChunk chunk)
        {
            var message = Encoding.ASCII.GetString(chunk.Data, 0, chunk.Data.Length);

            Console.WriteLine($"Received message: {message}");

            if (message.Contains("DOWNLOAD") || message.Contains("UPLOAD"))
            {
                var split = message.Split(':');
                previousMessage  = split[0];
                previousFileName = split[1];
                socket.SendTo(Encoding.ASCII.GetBytes(message), sendingEp);
                return;
            }

            string dataBack = "";

            switch (message)
            {
            case "CLOCK":
            {
                dataBack = DateTime.Now.ToLongDateString(); break;
            }

            case "ECHO":
            {
                dataBack = previousMessage;
                break;
            }

            case "CLOSE":
            {
                this.socket.Close();
                break;
            }

            case "SAVE":
            {
                this.SaveFile();
                break;
            }

            default: dataBack = message; break;
            }

            socket.SendTo(Encoding.ASCII.GetBytes(dataBack), sendingEp);
            previousMessage = message;
        }
Пример #3
0
        private void HandleFile(UDPDataChunk chunk)
        {
            if (filechunks.Any(x => x.NumberOfChunk == chunk.NumberOfChunk))
            {
                var index = filechunks.FindIndex(x => x.NumberOfChunk == chunk.NumberOfChunk);
                filechunks[index] = chunk;
            }
            else
            {
                filechunks.Add(chunk);
            }

            var backMessage = Encoding.ASCII.GetBytes(chunk.HashSumChunk.ToString());

            var res = DateTime.Now.Second;

            if (chunk.HashSumChunk % res == 0)
            {
                backMessage = Encoding.ASCII.GetBytes("35243642");
            }

            socket.SendTo(backMessage, sendingEp);
        }