Guest User

Untitled

a guest
May 8th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.IO;
  13.  
  14. namespace Cpanel_Server
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             Thread tStartListen = new Thread(new ThreadStart(Listen));
  26.             tStartListen.IsBackground = true;
  27.             tStartListen.Start();
  28.         }
  29.  
  30.         Socket client;
  31.  
  32.         private void Listen()
  33.         {            
  34.             Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  35.             listener.Bind(new IPEndPoint(IPAddress.Any, 4444));
  36.             listener.Listen(100);
  37.  
  38.             while(true)
  39.             {
  40.                 client = listener.Accept();
  41.                 Thread tStartInteract = new Thread(new ThreadStart(Interact));
  42.                 tStartInteract.Start();
  43.             }
  44.         }
  45.  
  46.         private void Interact()
  47.         {
  48.             UserControl control = new UserControl();
  49.             Socket newClient = client;
  50.             NetworkStream stream = new NetworkStream(newClient);
  51.             byte[] buffer = new byte[newClient.SendBufferSize];
  52.  
  53.             int bytesRead = newClient.Receive(buffer);
  54.             byte[] formatted = new byte[bytesRead];
  55.  
  56.             for (int i = 0; i < bytesRead; i++)
  57.             {
  58.                 formatted[i] = buffer[i];
  59.             }
  60.             string rawText = Encoding.ASCII.GetString(formatted);
  61.  
  62.             string userName = rawText.Substring(0, rawText.IndexOf("-//SPLIT\\-"));
  63.             string password = rawText.Substring(rawText.IndexOf("-//SPLIT\\-"));          
  64.  
  65.             control.userName = userName;
  66.             if (control.ReadUser())
  67.             {
  68.                 if (password == control.password)
  69.                 {
  70.                     string loginResult = "Login Successful!";
  71.                     if (stream.CanWrite)
  72.                     {
  73.                         stream.Write(Encoding.ASCII.GetBytes(loginResult), 0, loginResult.Length);
  74.                         stream.Flush();
  75.                     }
  76.                 }
  77.                 else
  78.                 {
  79.                     string loginResult = "Wrong Password!";
  80.                     if (stream.CanWrite)
  81.                     {
  82.                         stream.Write(Encoding.ASCII.GetBytes(loginResult), 0, loginResult.Length);
  83.                         stream.Flush();
  84.                     }
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 string loginResult = "Wrong Username!";
  90.                 if (stream.CanWrite)
  91.                 {
  92.                     stream.Write(Encoding.ASCII.GetBytes(loginResult), 0, loginResult.Length);
  93.                     stream.Flush();
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment