public override void Thread() { FileStream fs; int nOctetsLus = 0, nTimeOuts = 0, nErreurACK = 0;//some should go into TFTP class try { if (!File.Exists(FullPath)) { Output.Text("File not found"); SendError(CodeErreur.FileNotFound, "File not found"); } else { fs = File.Open(FullPath, FileMode.Open, FileAccess.Read, FileShare.Read); do { nOctetsLus = fs.Read(m_tamponEnvoi, 4, 512); Send(); do { m_socket.SendTo(m_tamponEnvoi, nOctetsLus + 4, SocketFlags.None, m_PointDistant); if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead)) { nTimeOuts++; } else { m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant); if (Receive(m_tamponReception)) { m_lire = false; nErreurACK++; } else { m_lire = true; } } }while (m_lire == false && nTimeOuts < 10 && nErreurACK < 3); }while (nOctetsLus == 512 && nTimeOuts < 10 && nErreurACK < 3); Output.Text($" RRQ closed from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {m_strFichier}"); fs.Close(); m_socket.Close(); } } catch (SocketException se) { Output.Text(se.Message); } }
public TFTP() { //try catchhhhhhh needed m_pointLocal = new IPEndPoint(0, 0); try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_socket.Bind(m_pointLocal); m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000); } catch (SocketException se) { Output.Text(se.Message); } CreateDirectory(); m_tamponEnvoi = new byte[516]; m_tamponReception = new byte[516]; }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("TFTP Server by Pascal Canuel and Justin-Roberge Lavoie"); //ip address IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine($"Current IP: {ip}"); } } Console.Write("Please select an option : "); Output.Text("Start | Stop"); string input; bool started = false; Thread thread; Creator cr = new Creator(); Console.ForegroundColor = ConsoleColor.Yellow; //color for input while (true) { input = Console.ReadLine(); switch (input) { case "Start": { if (!started) { Output.Text("Server started"); thread = new Thread(new ThreadStart(cr.Listen)); thread.IsBackground = true; //not best thread.Start(); started = true; } else { Output.Text("Server already started"); } break; } case "Stop": { cr.Fin = true; Environment.Exit(0); break; } default: { Output.Text("You have not selected an option"); break; } } } }
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); } }
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); } }