Пример #1
0
        public override void Thread()
        {
            FileStream fs;
            int        nOctetsLus = 0, nTimeOuts = 0, nErreurACK = 0;

            try
            {
                if (File.Exists(FullPath))
                {
                    Output.Text("File already exist");
                    SendError(CodeErreur.FileExisting, "File already exists");
                }
                else
                {
                    fs = File.Open(FullPath, FileMode.CreateNew, FileAccess.Write, FileShare.Write);


                    m_tamponEnvoi[0] = (byte)((ushort)CodeOP.ACK >> 8);
                    m_tamponEnvoi[1] = (byte)((ushort)CodeOP.ACK & 0xFF);
                    m_tamponEnvoi[2] = (byte)(m_noBloc >> 8);
                    m_tamponEnvoi[3] = (byte)(m_noBloc & 0xFF);
                    m_socket.SendTo(m_tamponEnvoi, 4, SocketFlags.None, m_PointDistant);

                    do
                    {
                        do
                        {
                            if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead))
                            {
                                nTimeOuts++; //TODO réenvoyer trame
                            }
                            else
                            {
                                nOctetsLus = m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant);
                                if (!Receive(m_tamponReception))
                                {
                                    m_lire = false;
                                    nErreurACK++;
                                }
                                else
                                {
                                    m_lire = true;

                                    fs.Write(m_tamponReception, 4, nOctetsLus - 4);

                                    Send();
                                    m_socket.SendTo(m_tamponEnvoi, 4, SocketFlags.None, m_PointDistant);
                                }
                            }
                        }while (m_lire == false && nTimeOuts < 10 && nErreurACK < 3);
                    }while (nOctetsLus == 516 && nTimeOuts < 10 && nErreurACK < 3);
                    Output.Text($"  WRQ closed from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {m_strFichier}");
                    fs.Close();
                    m_socket.Close();
                }
            }
            catch (SocketException se)
            {
                Output.Text(se.Message);
            }
        }
Пример #2
0
        public void Listen()
        {
            EndPoint PointLocal   = new IPEndPoint(0, 69);
            EndPoint PointDistant = new IPEndPoint(0, 0);

            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(PointLocal);

                byte[] bTamponReception = new byte[516];

                while (!m_fin)
                {
                    if (socket.Available > 0)
                    {
                        socket.ReceiveFrom(bTamponReception, ref PointDistant);

                        switch (ValiderTrame(bTamponReception))
                        {
                        case 1:
                            //créer thread rrq
                            Output.Text($"Request RRQ received from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {FindFile(bTamponReception)}");

                            TFTP rrq = new RRQ
                            {
                                PointDistant = PointDistant,
                                StrFichier   = FindFile(bTamponReception)
                            };

                            Thread threadRRQ = new Thread(new ThreadStart(rrq.Thread));
                            threadRRQ.Start();
                            break;

                        case 2:
                            //créer thread wrq
                            Output.Text($"Request WRQ received from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {FindFile(bTamponReception)}");

                            TFTP wrq = new WRQ
                            {
                                PointDistant = PointDistant,
                                StrFichier   = FindFile(bTamponReception)
                            };

                            Thread threadWRQ = new Thread(new ThreadStart(wrq.Thread));
                            threadWRQ.Start();
                            break;

                        case -1:
                            Output.Text($"Request not identified received from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port}");

                            break;
                        }
                    }
                }
                socket.Close();
            }
            catch (SocketException se)
            {
                Output.Text(se.Message);
            }
        }