Guest User

Untitled

a guest
Jul 5th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.30 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Net;
  9. using System.Data.SqlClient;
  10.  
  11. namespace LoginServer
  12. {
  13.     class Program
  14.     {
  15.         public static SqlConnection sqlConnection;
  16.         static void Main(string[] args)
  17.         {
  18.             IPAddress host = IPAddress.Parse("127.0.0.1");
  19.             TcpListener serverSocket = new TcpListener(host, 666);
  20.             TcpClient clientSocket = default(TcpClient);
  21.  
  22.             int connectedClients = 0;
  23.  
  24.             serverSocket.Start();
  25.             Console.WriteLine("Login Server Started");
  26.             sqlConnect();
  27.             connectedClients = 0;
  28.             Console.Title = "Login Server";
  29.             while (true)
  30.             {
  31.                 connectedClients += 1;
  32.                 clientSocket = serverSocket.AcceptTcpClient();
  33.                 Console.WriteLine("Client: " + connectedClients);
  34.                 handleClients client = new handleClients();
  35.                 client.startClient(clientSocket, Convert.ToString(connectedClients));
  36.             }
  37.         }
  38.         public static void sqlConnect()
  39.         {
  40.             //SQL
  41.             string connetionString = null;
  42.            
  43.             connetionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=LOGIN_SERVER_DATABASE;User ID=sa;Password=1234";
  44.             sqlConnection = new SqlConnection(connetionString);
  45.             try
  46.             {
  47.                 sqlConnection.Open();
  48.             }
  49.             catch (Exception ex)
  50.             {
  51.                 Console.WriteLine(ex);
  52.             }
  53.             //end sql
  54.         }
  55.         public class handleClients
  56.         {
  57.             TcpClient clientSocket;
  58.             string clNo;
  59.             SqlDataReader sqlDataReader = null;
  60.             SqlCommand loginCheck;
  61.             public void startClient(TcpClient iClientSocket, string clineNo)
  62.             {
  63.                 this.clientSocket = iClientSocket;
  64.                 this.clNo = clineNo;
  65.                 Thread clientThread = new Thread(doLogin);
  66.                 clientThread.Start();
  67.             }
  68.             private void doLogin()
  69.             {
  70.                 int loginRequestCount = 0;
  71.                 byte[] bytesFrom = new byte[10025];
  72.                 string dataClient = null;
  73.                 Byte[] sendData = null;
  74.                 string response = null;
  75.  
  76.                 string user = null;
  77.                 string pass = null;
  78.                 string extraData = null;
  79.                 while ((true))
  80.                 {
  81.                     try
  82.                     {
  83.                         if (clientSocket.Connected)
  84.                         {
  85.                             NetworkStream networkStream = clientSocket.GetStream();
  86.                             networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  87.                             dataClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  88.                             dataClient = dataClient.Replace("\0", string.Empty);
  89.                             string[] someData = dataClient.Split(':');
  90.                             user = someData[0];
  91.                             pass = someData[1];
  92.                             extraData = someData[2];
  93.                             if (extraData == "Lolwtfmanwaaromhebikdit")
  94.                             {
  95.                                 loginRequestCount++;
  96.                                 loginCheck = new SqlCommand(@"select * from users where userId = '" +  user   + "'AND userPassword = '" + pass + "';", sqlConnection);
  97.                                 sqlDataReader = loginCheck.ExecuteReader();
  98.                                 Console.WriteLine("User: " + user + " tries to log in!");
  99.                                 if (sqlDataReader.HasRows)
  100.                                 {
  101.                                     response = "ok";
  102.                                     sendData = Encoding.ASCII.GetBytes(response);
  103.                                     networkStream.Write(sendData, 0, sendData.Length);
  104.                                     networkStream.Flush();
  105.                                     Console.WriteLine("User " + user + " has logged in!");
  106.                                     /*
  107.                                     clientSocket.Close();
  108.                                     networkStream.Close();
  109.                                     sqlDataReader.Close();
  110.                                     Thread.CurrentThread.Abort();
  111.                                     break;
  112.                                      */
  113.                                 }
  114.                                 else
  115.                                 {
  116.                                     response = "Declined";
  117.                                     sendData = Encoding.ASCII.GetBytes(response);
  118.                                     networkStream.Write(sendData, 0, sendData.Length);
  119.                                     networkStream.Flush();
  120.                                 }
  121.                                 sqlDataReader.Close();
  122.                                 sqlDataReader = null;
  123.                             }
  124.                             if (loginRequestCount > 5)
  125.                             {
  126.                                 clientSocket.Close();
  127.                                 networkStream.Close();
  128.                                 Thread.CurrentThread.Abort();
  129.                                 break;
  130.                             }
  131.                             networkStream.Flush();
  132.                             dataClient = null;
  133.                         }
  134.                         else
  135.                         {
  136.                            // clientSocket.Close();
  137.                            // Console.WriteLine("Disconnected");
  138.                             //Thread.CurrentThread.Abort();
  139.                         }
  140.                     }
  141.                     catch (Exception e)
  142.                     {
  143.                         clientSocket.Close();
  144.                        // Console.WriteLine(e);
  145.                         Console.WriteLine("User Disconnected");
  146.                         Thread.CurrentThread.Abort();
  147.                         break;
  148.                     }
  149.                     Thread.Sleep(300);
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment