Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //create a new server
            var server = new UdpListener();


            //start listening for messages and copy the messages back to the client
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    var received = await server.Receive();
                    server.Reply("ack " + received.Message, received.Sender);
                    Console.WriteLine(received.Message);
                    if (received.Message == "quit")
                        break;
                }
            });

            //create a new client
            var client = UdpUser.ConnectTo(Credentials.IPAddress, 32123);

            //wait for reply messages from server and send them to console 
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    try
                    {
                        var received = await client.Receive();
                        Console.WriteLine(received.Message);
                        if (received.Message.Contains("quit"))
                            break;
                    }
                    catch (Exception ex)
                    {
                        Debug.Write(ex);
                    }
                }
            });

            //type ahead :-)
            string read;
            do
            {
                read = Console.ReadLine();
                client.Send(read);
            } while (read != "quit");
        }
Exemplo n.º 2
0
    public bool receive(UdpListener receiver, string fileName, Int32 fileSize)
    {
      //Byte[][] bytes = new Byte[fileSize/1024][];
      SortedDictionary<Int32, Byte[]> bytes = new SortedDictionary<int, byte[]>();
      bool endReception = false;

      Task.Factory.StartNew(async () =>
      {
        var FIN = false;
        bool endFile = false;
        int counter = 0;

        while (!FIN)
        {
          var received = await receiver.Receive();

          if (!bytes.Keys.Contains(received.packet.SequenceNumber))
          {
            bytes[received.packet.SequenceNumber] = received.packet.DATA;
            counter++;
          }
          
          receiver.Reply(new Packet(0, received.packet.SequenceNumber, false, false, 0, new byte[0]).BuildPacket(), received.Sender);

          if(received.packet.FIN == true)
          {
            endFile = true;
          }

          if (bytes.Keys.Max() == counter && endFile)
          {
            FIN = true;

            byte[] file = Extensions.Combine(bytes.Values.ToArray());

            System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            fileStream.Write(file, 0, file.Length);
            fileStream.Close();
            endReception = true;
          }
        }
      });
      while (!endReception) { }
      return endReception;
    }
Exemplo n.º 3
0
    public void start()
    {
      /* Créer un socket et attendre la demande du client */
      var server = new UdpListener(new IPEndPoint(IPAddress.Any, 32123));

      //start listening for messages and copy the messages back to the client
      Task.Factory.StartNew(async () =>
      {
        while (true)
        {
          Received received = await server.Receive();

          if (received.packet.SOR == false)
          {
            UdpListener receiver = new UdpListener(new IPEndPoint(IPAddress.Any, Extensions.FreeUdpPort()));
            Byte[] portNumber = new Byte[4];
            portNumber = BitConverter.GetBytes(((IPEndPoint)receiver.Client.Client.LocalEndPoint).Port);
            server.Reply(new Packet(0, received.packet.SequenceNumber, false, false, portNumber.Length, portNumber).BuildPacket(), received.Sender);

            Reception reception = new Reception();
            reception.receive(receiver, System.Text.Encoding.UTF8.GetString(received.packet.DATA), received.packet.DataLength);
          }
          else
          {
            UdpUser sender = UdpUser.ConnectTo(received.Sender.Address.ToString(), received.Sender.Port);
            string fileName = System.Text.Encoding.UTF8.GetString(received.packet.DATA);

            await Task.Factory.StartNew(async () =>
             {
               var ack = await sender.Receive();
               if (ack.packet.AckNumber == 0)
               {
                 sender = UdpUser.ConnectTo(Credentials.IPAddress, received.Sender.Port);
                 CommunicationBase.Handshake = true;
               }
             });

            /* Envoyer fichier */
            byte[] bytes = System.IO.File.ReadAllBytes(fileName);
            byte[] fileNameString = Encoding.ASCII.GetBytes(Path.GetFileName(Path.Combine(fileName)));
            

            Packet packet = new Packet(0, 0, false, false, fileNameString.Length, fileNameString);
            sender.Send(packet.BuildPacket());

            while (!CommunicationBase.Handshake)
            {

            }

            Submission submission = new Submission();
            submission.submit(bytes, sender);
                  /*UdpUser sender = UdpUser.ConnectTo(received.Sender.Address.ToString(), received.Sender.Port);

                  byte[] bytes = System.IO.File.ReadAllBytes(System.Text.Encoding.UTF8.GetString(received.packet.DATA));
                  Submission submission = new Submission();
                  submission.submit(bytes, sender);*/
          }

                //server.Reply(new Packet(0, received.packet.SequenceNumber, false, true, 0, new byte[0]).BuildPacket(), received.Sender);
                Console.WriteLine(received.Message);
          if (received.Message == "quit")
            break;
        }
      });
      while (true)
      {
      }
    }
Exemplo n.º 4
0
    public void start()
    {
      int choiceTask;
      bool parsed;

      do
      {
        Console.WriteLine("\nQue souhaitez-vous faire?\n1) Envoyer un fichier\n2) Recevoir un fichier");
        parsed = Int32.TryParse(Console.ReadLine(), out choiceTask);
      } while (!parsed || (choiceTask != 1 && choiceTask != 2));

      string fileName;
      Console.WriteLine("\nEntrez le nom du fichier à traiter");
      fileName = Console.ReadLine();
      
      if (choiceTask == 1)
      {
        UdpUser socket = UdpUser.ConnectTo(Credentials.IPAddress, 32123);
        byte[] fileNameString = Encoding.ASCII.GetBytes(Path.GetFileName(Path.Combine(fileName)));
        //wait for reply messages from server and send them to console 
        Task.Factory.StartNew(async () =>
        {
          var received = await socket.Receive();
          if (received.packet.AckNumber == 0)
          {
            socket = UdpUser.ConnectTo(Credentials.IPAddress, BitConverter.ToInt32(received.packet.DATA, 0));
            CommunicationBase.Handshake = true;
          }
        });

        /* On vérifie si le fichier existe. Si non, on redemande un nom de fichier existant à l'infini */
        while (!File.Exists(Path.Combine(fileName)))
        {
          Console.WriteLine("\nFichier inexistant.\nEntrez le nom du fichier à traiter");
          fileName = Console.ReadLine();
        };

        /* Envoyer fichier */
        byte[] bytes = System.IO.File.ReadAllBytes(fileName);
        
        Submission submission = new Submission();

        Packet packet = new Packet(0, 0, false, false, fileNameString.Length, fileNameString);
        socket.Send(packet.BuildPacket());

        while (!CommunicationBase.Handshake)
        {

        }

        submission.submit(bytes, socket);
      }

      else
      {
        UdpListener receiver = new UdpListener();
        bool endReception = false;
        byte[] fileNameString = Encoding.ASCII.GetBytes(fileName);

        Packet packet = new Packet(0, 0, false, true, fileNameString.Length, fileNameString);
        receiver.Send(packet.BuildPacket(), Credentials.IPAddress, 32123);
        Task.Factory.StartNew(async () =>
        {
          Received received = await receiver.Receive();
          if (received.packet.SOR == false)
          {
            receiver.Reply(new Packet(0, received.packet.SequenceNumber, false, false, 0, new byte[0]).BuildPacket(), received.Sender);

            Reception reception = new Reception();
            endReception = reception.receive(receiver, System.Text.Encoding.UTF8.GetString(received.packet.DATA), received.packet.DataLength);
          }
        });
        while (!endReception)
        {

        }
      }
    }