Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- namespace ServerFTP_IO
- {
- public class server
- {
- ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
- void ProcessIncomingData(object obj)
- {
- TcpClient client = (TcpClient)obj;
- StringBuilder sb = new StringBuilder();
- bool userauth = false;
- bool accessgranted = false;
- using (NetworkStream stream = client.GetStream())
- {
- int i;
- string[] tab = new string[2];
- while ((i = stream.ReadByte()) != 0)
- {
- sb.Append((char)i);
- }
- tab = sb.ToString().Split(' ');
- switch (tab[0].ToUpper())
- {
- case "USER":
- {
- accessgranted = true;
- userauth = true;
- Byte[] bytess = new Byte[256];
- string username = tab[1];
- if (username.Equals("anonymous"))
- {
- string rep = "331: Password is required to get access." + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- userauth = true;
- }
- else
- {
- string rep = "530: Access denied" + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- break;
- }
- case "PASS":
- {
- if (userauth)
- {
- string userpass = tab[1];
- if (userpass.Contains("@") && !(userpass.StartsWith("@")) && !(userpass.EndsWith("@")) && (userpass.EndsWith(".com") || userpass.EndsWith(".pl")))
- {
- string rep = " 230: Access granted. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- accessgranted = true;
- }
- else
- {
- string rep = " 530: Access denied. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- }
- else
- {
- string rep = " User has not been specified yet. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- userauth = false;
- break;
- }
- case "RETR":
- {
- if (accessgranted)
- {
- string path = @"C:\Users\Klaudia\Documents\hello.txt";
- byte[] fileData = GetFileData(path);
- stream.Write(fileData, 0, fileData.Length);
- }
- Console.WriteLine(sb.ToString());
- // string reply = "ack: " + sb.ToString() + '\0';
- // stream.Write(Encoding.ASCII.GetBytes(reply), 0, reply.Length);
- break;
- }
- }
- }
- }
- byte[] GetFileData(String path)
- {
- using (var sr = new StreamReader(path))
- {
- return ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd());
- }
- }
- void ProcessIncomingConnection(IAsyncResult ar)
- {
- TcpListener listener = (TcpListener)ar.AsyncState;
- TcpClient client = listener.EndAcceptTcpClient(ar);
- ThreadPool.QueueUserWorkItem(ProcessIncomingData, client);
- tcpClientConnected.Set();
- }
- public void start()
- {
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
- TcpListener listener = new TcpListener(endpoint);
- listener.Start();
- while (true)
- {
- tcpClientConnected.Reset();
- listener.BeginAcceptTcpClient(new AsyncCallback(ProcessIncomingConnection), listener);
- tcpClientConnected.WaitOne();
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- server s = new server();
- s.start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement