Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- using System.Data.SqlClient;
- namespace LoginServer
- {
- class Program
- {
- public static SqlConnection sqlConnection;
- static void Main(string[] args)
- {
- IPAddress host = IPAddress.Parse("127.0.0.1");
- TcpListener serverSocket = new TcpListener(host, 666);
- TcpClient clientSocket = default(TcpClient);
- int connectedClients = 0;
- serverSocket.Start();
- Console.WriteLine("Login Server Started");
- sqlConnect();
- connectedClients = 0;
- Console.Title = "Login Server";
- while (true)
- {
- connectedClients += 1;
- clientSocket = serverSocket.AcceptTcpClient();
- Console.WriteLine("Client: " + connectedClients);
- handleClients client = new handleClients();
- client.startClient(clientSocket, Convert.ToString(connectedClients));
- }
- }
- public static void sqlConnect()
- {
- //SQL
- string connetionString = null;
- connetionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=LOGIN_SERVER_DATABASE;User ID=sa;Password=1234";
- sqlConnection = new SqlConnection(connetionString);
- try
- {
- sqlConnection.Open();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- //end sql
- }
- public class handleClients
- {
- TcpClient clientSocket;
- string clNo;
- SqlDataReader sqlDataReader = null;
- SqlCommand loginCheck;
- public void startClient(TcpClient iClientSocket, string clineNo)
- {
- this.clientSocket = iClientSocket;
- this.clNo = clineNo;
- Thread clientThread = new Thread(doLogin);
- clientThread.Start();
- }
- private void doLogin()
- {
- int loginRequestCount = 0;
- byte[] bytesFrom = new byte[10025];
- string dataClient = null;
- Byte[] sendData = null;
- string response = null;
- string user = null;
- string pass = null;
- string extraData = null;
- while ((true))
- {
- try
- {
- if (clientSocket.Connected)
- {
- NetworkStream networkStream = clientSocket.GetStream();
- networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
- dataClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
- dataClient = dataClient.Replace("\0", string.Empty);
- string[] someData = dataClient.Split(':');
- user = someData[0];
- pass = someData[1];
- extraData = someData[2];
- if (extraData == "Lolwtfmanwaaromhebikdit")
- {
- loginRequestCount++;
- loginCheck = new SqlCommand(@"select * from users where userId = '" + user + "'AND userPassword = '" + pass + "';", sqlConnection);
- sqlDataReader = loginCheck.ExecuteReader();
- Console.WriteLine("User: " + user + " tries to log in!");
- if (sqlDataReader.HasRows)
- {
- response = "ok";
- sendData = Encoding.ASCII.GetBytes(response);
- networkStream.Write(sendData, 0, sendData.Length);
- networkStream.Flush();
- Console.WriteLine("User " + user + " has logged in!");
- /*
- clientSocket.Close();
- networkStream.Close();
- sqlDataReader.Close();
- Thread.CurrentThread.Abort();
- break;
- */
- }
- else
- {
- response = "Declined";
- sendData = Encoding.ASCII.GetBytes(response);
- networkStream.Write(sendData, 0, sendData.Length);
- networkStream.Flush();
- }
- sqlDataReader.Close();
- sqlDataReader = null;
- }
- if (loginRequestCount > 5)
- {
- clientSocket.Close();
- networkStream.Close();
- Thread.CurrentThread.Abort();
- break;
- }
- networkStream.Flush();
- dataClient = null;
- }
- else
- {
- // clientSocket.Close();
- // Console.WriteLine("Disconnected");
- //Thread.CurrentThread.Abort();
- }
- }
- catch (Exception e)
- {
- clientSocket.Close();
- // Console.WriteLine(e);
- Console.WriteLine("User Disconnected");
- Thread.CurrentThread.Abort();
- break;
- }
- Thread.Sleep(300);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment