Guest User

Untitled

a guest
Jul 7th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 39.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ConsoleApplication1;
  6. using Lidgren.Network;
  7. using System.Net;
  8. using System.Threading;
  9. using N = System.Net;
  10. using System.Collections;
  11. using System.Net.Sockets;
  12. using System.Data;
  13. using MySql.Data.MySqlClient;
  14.  
  15.  
  16. namespace GameServer
  17. {
  18.     class Server
  19.     {
  20.         public TcpListener m_Socket;
  21.         public int count = 0;
  22.         static int objectsToCreate = 1000;
  23.         Lobby[] lobbies = new Lobby[objectsToCreate];
  24.  
  25.  
  26.         public MySqlConnection database()
  27.         {
  28.             String connString = "Server=127.0.0.1;Port=3306;Database=rpc;Uid=server;password=Derp123;";
  29.             MySqlConnection conn = new MySqlConnection(connString);
  30.             //MySqlCommand command = conn.CreateCommand();
  31.             /*
  32.             command.CommandText = "Select * from user";
  33.             try
  34.             {
  35.                 conn.Open();
  36.             }
  37.             catch (Exception ex)
  38.             {
  39.                 Console.WriteLine(ex);
  40.             }
  41.             MySqlDataReader reader = command.ExecuteReader();
  42.             while (reader.Read())
  43.             {
  44.                 Console.WriteLine(reader["username"].ToString() + " " + reader["Password"].ToString());
  45.  
  46.             }
  47.             //Console.ReadLine();
  48.             */
  49.             return conn;
  50.         }
  51.  
  52.         private void Server_start()
  53.         {
  54.             Thread acceptClients = new Thread(new ThreadStart(acceptConnections));
  55.             acceptClients.Start();
  56.         }
  57.  
  58.         private void acceptConnections()
  59.         {
  60.             try
  61.             {
  62.                 IPAddress ipConfig = IPAddress.Parse("192.168.1.124");
  63.                 m_Socket = new TcpListener(ipConfig, 6969);
  64.                 m_Socket.Start();
  65.                 Console.WriteLine("listening");
  66.  
  67.                 while (true)
  68.                 {
  69.                     TcpClient m_Client = m_Socket.AcceptTcpClient();
  70.                     //sendData(m_Client[count], "connected");
  71.                     Console.WriteLine("new client");
  72.                     Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
  73.                     clientThread.Start(m_Client);
  74.  
  75.                 }
  76.             }
  77.             catch (Exception error)
  78.             {
  79.                 Console.WriteLine(error.Message);
  80.             }
  81.         }
  82.  
  83.         private void HandleClientComm(object client)
  84.         {
  85.             TcpClient tcpClient = (TcpClient)client;
  86.             NetworkStream clientStream = tcpClient.GetStream();
  87.  
  88.             byte[] message = new byte[4096];
  89.             int bytesRead;
  90.             String client_msg;
  91.             String username = null;
  92.  
  93.             while (true)
  94.             {
  95.                 bytesRead = 0;
  96.                 #region read_msg
  97.                 try
  98.                 {
  99.                     //blocks until a client sends a message
  100.                     bytesRead = clientStream.Read(message, 0, 4096);
  101.                     Console.WriteLine(bytesRead);
  102.                 }
  103.                 catch
  104.                 {
  105.                     //a socket error has occured
  106.                     break;
  107.                 }
  108.  
  109.                 if (bytesRead == 0)
  110.                 {
  111.                     //the client has disconnected from the server
  112.                     Console.WriteLine("errorr1!!!!");
  113.                     break;
  114.                 }
  115.  
  116.                 if (bytesRead > 0)
  117.                 {
  118.                     ASCIIEncoding encoder = new ASCIIEncoding();
  119.                     client_msg = encoder.GetString(message, 0, bytesRead);
  120.                     //Console.WriteLine(client_msg);
  121.                     byte[] buffer;
  122.                     #region client_msg switch
  123.                     switch (client_msg)
  124.                     {
  125.                         case "Login":
  126.                             if (username == null)
  127.                             {
  128.                                 Console.WriteLine("Login\n");
  129.                                 username = Login(clientStream);
  130.                             }
  131.                             else
  132.                             {
  133.                                 Console.WriteLine("already logged in!\n");
  134.                                 buffer = encoder.GetBytes("alrdy_log");
  135.                                 clientStream.Write(buffer, 0, buffer.Length);
  136.                             }
  137.                             continue;
  138.                         case "UpdateProfile":
  139.                             Console.WriteLine("UpdateProfile\n");
  140.                             UpdateProfile(clientStream, username);
  141.                             continue;
  142.                         case "GetProfile":
  143.                             if (username != null)
  144.                             {
  145.                                 Console.WriteLine("GetProfile\n");
  146.                                 GetProfile(clientStream, username);
  147.                                 continue;
  148.                             }
  149.                             else
  150.                             {
  151.                                 Console.WriteLine("Not Logged in get profile area!\n");
  152.                                 buffer = encoder.GetBytes("no_login");
  153.                                 clientStream.Write(buffer, 0, buffer.Length);
  154.                             }
  155.  
  156.                             continue;
  157.                         case "HostLobby":
  158.                             if (username != null)
  159.                             {
  160.                                 Console.WriteLine("HostLobby\n");
  161.                                 HostLobby(clientStream, username);
  162.                                 continue;
  163.                             }
  164.                             else
  165.                             {
  166.                                 Console.WriteLine("Not Logged in Hostlobby area!\n");
  167.                                 buffer = encoder.GetBytes("no_login");
  168.                                 clientStream.Write(buffer, 0, buffer.Length);
  169.                             }
  170.                             continue;
  171.                         case "JoinLobby":
  172.                             Console.WriteLine("JoinLobby\n");
  173.                             JoinLobby(clientStream, username);
  174.                             continue;
  175.                         default:
  176.                             Console.WriteLine("invalid! " + client_msg + "\n");
  177.                             buffer = encoder.GetBytes("invalid");
  178.                             clientStream.Write(buffer, 0, buffer.Length);
  179.                             continue;
  180.  
  181.                     }
  182.                     #endregion
  183.                 #endregion
  184.  
  185.  
  186.                 }
  187.                 //message has successfully been received
  188.                 //ASCIIEncoding encoder = new ASCIIEncoding();
  189.                 //Console.WriteLine(encoder.GetString(message, 0, bytesRead));
  190.             }
  191.  
  192.             tcpClient.Close();
  193.         }
  194.  
  195.         private void sendData(TcpClient soc, String strData)
  196.         {
  197.             try
  198.             {
  199.  
  200.                 if (soc.Connected && soc.Client.Poll(3000, SelectMode.SelectWrite))
  201.                 {
  202.                     {
  203.                         char c = (char)0;
  204.                         byte[] msg = System.Text.Encoding.ASCII.GetBytes(strData + c);
  205.                         int i = soc.Client.Send(msg, 0, msg.Length, SocketFlags.None);
  206.                     }
  207.                 }
  208.                 else
  209.                 {
  210.  
  211.                 }
  212.             }
  213.             catch (Exception ex)
  214.             {
  215.  
  216.             }
  217.  
  218.         }
  219.  
  220.         private void startGame(NetworkStream clientStream)
  221.         {
  222.  
  223.             Console.WriteLine("startGame area");
  224.             byte[] buffer;
  225.             ASCIIEncoding encoder = new ASCIIEncoding();
  226.             buffer = encoder.GetBytes("gamestart");
  227.             Console.WriteLine("gamestart");
  228.             clientStream.Write(buffer, 0, buffer.Length);
  229.             bool gotProfile = false;
  230.            
  231.             List<String> player1 = new List<String>();
  232.            
  233.             byte[] message = new byte[4096];
  234.             int bytesRead;
  235.             String client_msg = null;
  236.             bool rdy = false;
  237.  
  238.             #region Clients read
  239.             while (true)
  240.             {
  241.                 bytesRead = 0;
  242.                 try
  243.                 {
  244.                     //blocks until a client sends a message
  245.                     bytesRead = clientStream.Read(message, 0, 4096);
  246.                     Console.WriteLine(bytesRead);
  247.                 }
  248.                 catch
  249.                 {
  250.                     //a socket error has occured
  251.                     break;
  252.                 }
  253.                 if (bytesRead == 0)
  254.                 {
  255.                     //the client has disconnected from the server
  256.                     Console.WriteLine("errorr1!!!!");
  257.                     break;
  258.                 }
  259.  
  260.                 if (bytesRead > 0)
  261.                 {
  262.                     client_msg = encoder.GetString(message, 0, bytesRead);
  263.                     Console.WriteLine("rdy area");
  264.                     Console.WriteLine("client msg1 " + client_msg);
  265.                     #region client 1 rdy
  266.                     if (client_msg.ToLower().Equals("rdy"))
  267.                     {
  268.                         rdy = true;
  269.                         String command = "profile";
  270.                         int count = 0;
  271.  
  272.                         buffer = encoder.GetBytes(command);
  273.                         clientStream.Write(buffer, 0, buffer.Length);
  274.  
  275.                         while (true)
  276.                         {
  277.                            
  278.                             try
  279.                             {
  280.                                 //blocks until a client sends a message
  281.                                 bytesRead = clientStream.Read(message, 0, 4096);
  282.                                 Console.WriteLine(bytesRead);
  283.                             }
  284.                             catch
  285.                             {
  286.                                 //a socket error has occured
  287.                                 break;
  288.                             }
  289.                             if (bytesRead == 0)
  290.                             {
  291.                                 //the client has disconnected from the server
  292.                                 Console.WriteLine("errorr1!!!!");
  293.                                 break;
  294.                             }
  295.  
  296.                             if (bytesRead > 0)
  297.                             {
  298.                                
  299.                                 bytesRead = clientStream.Read(message, 0, 65536);
  300.                                
  301.  
  302.  
  303.                             }
  304.  
  305.                         }
  306.                         gotProfile = true;
  307.                     }
  308.                     #endregion
  309.                     #region send profile
  310.                     if (gotProfile & gotProfile2)
  311.                     {
  312.                         //send profile
  313.                         int count = 0;
  314.                         while (true)
  315.                         {
  316.                             buffer = encoder.GetBytes("sendpro");
  317.                             clientStream.Write(buffer, 0, buffer.Length);
  318.                             clientStream2.Write(buffer, 0, buffer.Length);
  319.                             try
  320.                             {
  321.                                 //blocks until a client sends a message
  322.                                 bytesRead = clientStream.Read(message, 0, 4096);
  323.                                 bytesRead2 = clientStream2.Read(message2, 0, 4096);
  324.                                 Console.WriteLine(bytesRead);
  325.                                 Console.WriteLine(bytesRead2);
  326.                             }
  327.                             catch
  328.                             {
  329.                                 //a socket error has occured
  330.                                 break;
  331.                             }
  332.                             if (bytesRead == 0)
  333.                             {
  334.                                 //the client has disconnected from the server
  335.                                 Console.WriteLine("errorr1!!!!");
  336.                                 break;
  337.                             }
  338.  
  339.                             if ((bytesRead > 0) && (bytesRead2 > 0))
  340.                             {
  341.                                 bytesRead = clientStream.Read(message, 0, 65536);
  342.                                 bytesRead2 = clientStream2.Read(message2, 0, 65536);
  343.                                 client_msg = encoder.GetString(message, 0, bytesRead);
  344.                                 client_msg2 = encoder.GetString(message2, 0, bytesRead2);
  345.                                 #region both alive
  346.                                 while (((client_msg.ToLower().Equals("alive")) && (client_msg2.ToLower().Equals("alive"))) && (count < player1.Count()) && (count < player2.Count()))
  347.                                 {
  348.                                     buffer = encoder.GetBytes(player1[count]);
  349.                                     buffer2 = encoder.GetBytes(player2[count]);
  350.                                     clientStream.Write(buffer2, 0, buffer2.Length);
  351.                                     clientStream2.Write(buffer, 0, buffer.Length);
  352.                                     try
  353.                                     {
  354.                                         //blocks until a client sends a message
  355.                                         bytesRead = clientStream.Read(message, 0, 4096);
  356.                                         bytesRead2 = clientStream2.Read(message2, 0, 4096);
  357.                                         Console.WriteLine(bytesRead);
  358.                                         Console.WriteLine(bytesRead2);
  359.                                     }
  360.                                     catch (Exception ex)
  361.                                     {
  362.                                         //a socket error has occured
  363.                                         Console.WriteLine(ex);
  364.                                         break;
  365.                                     }
  366.                                     if ((bytesRead == 0) || (bytesRead2 == 0))
  367.                                     {
  368.                                         //the client has disconnected from the server
  369.                                         Console.WriteLine("errorr1!!!!");
  370.                                         break;
  371.                                     }
  372.  
  373.                                     if ((bytesRead > 0) && (bytesRead2 > 0))
  374.                                     {
  375.                                         bytesRead = clientStream.Read(message, 0, 65536);
  376.                                         bytesRead2 = clientStream2.Read(message2, 0, 65536);
  377.                                         client_msg = encoder.GetString(message, 0, bytesRead);
  378.                                         client_msg2 = encoder.GetString(message2, 0, bytesRead2);
  379.                                        
  380.                                     }
  381.                                     count++;
  382.                                 }
  383.                                 #endregion
  384.  
  385.  
  386.                                 buffer = encoder.GetBytes("sendmove");
  387.                                 buffer2 = encoder.GetBytes("sendmove");
  388.  
  389.  
  390.                                 while (true)
  391.                                 {
  392.                                     clientStream.Write(buffer2, 0, buffer2.Length);
  393.                                     clientStream2.Write(buffer, 0, buffer.Length);
  394.                                     try
  395.                                     {
  396.                                         //blocks until a client sends a message
  397.                                         bytesRead = clientStream.Read(message, 0, 4096);
  398.                                         bytesRead2 = clientStream2.Read(message2, 0, 4096);
  399.                                         Console.WriteLine(bytesRead);
  400.                                         Console.WriteLine(bytesRead2);
  401.                                     }
  402.                                     catch (Exception ex)
  403.                                     {
  404.                                         //a socket error has occured
  405.                                         Console.WriteLine(ex);
  406.                                         break;
  407.                                     }
  408.                                     if ((bytesRead == 0) || (bytesRead2 == 0))
  409.                                     {
  410.                                         //the client has disconnected from the server
  411.                                         Console.WriteLine("errorr1!!!!");
  412.                                         break;
  413.                                     }
  414.  
  415.                                     if ((bytesRead > 0) && (bytesRead2 > 0))
  416.                                     {
  417.  
  418.  
  419.                                         if ((client_msg.ToLower().Equals("gameov")) && (client_msg2.ToLower().Equals("gameov")))
  420.                                         {
  421.                                             Console.WriteLine("game over!");
  422.                                             break;
  423.                                         }
  424.                                         else
  425.                                         {
  426.                                             bytesRead = clientStream.Read(message, 0, 65536);
  427.                                             bytesRead2 = clientStream2.Read(message2, 0, 65536);
  428.                                             client_msg = encoder.GetString(message, 0, bytesRead);
  429.                                             client_msg2 = encoder.GetString(message2, 0, bytesRead2);
  430.                                             buffer = encoder.GetBytes(client_msg);
  431.                                             buffer2 = encoder.GetBytes(client_msg2);
  432.                                         }
  433.  
  434.                                     }
  435.  
  436.                                 }
  437.  
  438.  
  439.                             }
  440.  
  441.                         }
  442.                     }
  443.                     #endregion
  444.  
  445.                 }
  446.             #endregion
  447.             }
  448.         }
  449.  
  450.         private void HostLobby(NetworkStream clientStream, String username)
  451.         {
  452.             Console.WriteLine("hostlobby area");
  453.             byte[] buffer;
  454.             ASCIIEncoding encoder = new ASCIIEncoding();
  455.             buffer = encoder.GetBytes("LobbyName");
  456.             clientStream.Write(buffer, 0, buffer.Length);
  457.  
  458.             byte[] message = new byte[4096];
  459.             int bytesRead;
  460.             String client_msg = null;
  461.  
  462.             #region Client read
  463.             while (true)
  464.             {
  465.                 bytesRead = 0;
  466.                 try
  467.                 {
  468.                     //blocks until a client sends a message
  469.                     bytesRead = clientStream.Read(message, 0, 4096);
  470.                     Console.WriteLine(bytesRead);
  471.                 }
  472.                 catch
  473.                 {
  474.                     //a socket error has occured
  475.                     break;
  476.                 }
  477.                 if (bytesRead == 0)
  478.                 {
  479.                     //the client has disconnected from the server
  480.                     Console.WriteLine("errorr1!!!!");
  481.                     break;
  482.                 }
  483.  
  484.                 if (bytesRead > 0)
  485.                 {
  486.                     client_msg = encoder.GetString(message, 0, bytesRead);
  487.                     Console.WriteLine("hi");
  488.                     break;
  489.                 }
  490.            
  491.  
  492.             }
  493.             #endregion
  494.             if (client_msg.Length > 0)
  495.             {
  496.                 Console.WriteLine("hi1");
  497.                 //buffer = encoder.GetBytes("Next Command!");
  498.                 lobbies[count] = new Lobby()
  499.                 {
  500.                     objectNumber = count,
  501.                     players = 1,
  502.                     tcpPlayer1 = clientStream,
  503.                     lobby_name = client_msg
  504.                    
  505.                 };
  506.                 lobbies[count].tcpPlayer1 = clientStream;
  507.                 int current_lobby = count;
  508.                 count++;
  509.                 #region waiting for game start alive statements
  510.                 while (true)
  511.                 {
  512.                     buffer = encoder.GetBytes("alive");
  513.                     try
  514.                     {
  515.                         lobbies[current_lobby].tcpPlayer1.Write(buffer, 0, buffer.Length);
  516.                         #region Clientread
  517.                         while (true)
  518.                         {
  519.                             bytesRead = 0;
  520.                             try
  521.                             {
  522.                                 //blocks until a client sends a message
  523.                                 bytesRead = clientStream.Read(message, 0, 4096);
  524.                                 Console.WriteLine(bytesRead);
  525.                             }
  526.                             catch
  527.                             {
  528.                                 //a socket error has occured
  529.                                 break;
  530.                             }
  531.                             if (bytesRead == 0)
  532.                             {
  533.                                 //the client has disconnected from the server
  534.                                 Console.WriteLine("errorr1!!!!");
  535.                                 break;
  536.                             }
  537.  
  538.                             if (bytesRead > 0)
  539.                             {
  540.                                 client_msg = encoder.GetString(message, 0, bytesRead);
  541.                                 if ((client_msg == "alive") && (lobbies[current_lobby].ready))
  542.                                 {
  543.                                     //game start stuff?
  544.                                     startGame(lobbies[current_lobby].tcpPlayer1);
  545.                                 }
  546.                                 else
  547.                                 {
  548.                                     buffer = encoder.GetBytes("alive");
  549.                                     lobbies[current_lobby].tcpPlayer1.Write(buffer, 0, buffer.Length);
  550.                                     Console.WriteLine("alive recieved");
  551.                                 }
  552.  
  553.                             }
  554.                         }
  555.                     }
  556.                     catch (Exception ex)
  557.                     {
  558.                         Console.WriteLine(ex);
  559.                         break;
  560.                     }
  561.  
  562.                     #endregion
  563.                 }
  564.                 #endregion
  565.  
  566.             }
  567.         }
  568.  
  569.         private void JoinLobby(NetworkStream clientStream, String username)
  570.         {
  571.             Console.WriteLine("Join Lobby area");
  572.             byte[] buffer;
  573.             byte[] buffer1;
  574.             ASCIIEncoding encoder = new ASCIIEncoding();
  575.             buffer = encoder.GetBytes("LobbyName");
  576.             clientStream.Write(buffer, 0, buffer.Length);
  577.  
  578.             byte[] message = new byte[4096];
  579.             int bytesRead;
  580.             String client_msg = null;
  581.  
  582.             if (count == 0)
  583.             {
  584.                 buffer = encoder.GetBytes("no_lobbies");
  585.                 clientStream.Write(buffer, 0, buffer.Length);
  586.             }
  587.             else
  588.             {
  589.                 buffer = encoder.GetBytes("enum_lobbies_EOF");
  590.                 int i = 0;
  591.                 String lobby_list;
  592.  
  593.                 while (i < count)
  594.                 {
  595.                     lobby_list = "Lobby name: " + lobbies[i].lobby_name + " number of players: " + lobbies[i].players + " Lobbie number: " + lobbies[i].objectNumber + "\n";
  596.                     Console.WriteLine(lobby_list);
  597.                     buffer1 = encoder.GetBytes(lobby_list);
  598.                     clientStream.Write(buffer1, 0, buffer1.Length);
  599.                     i++;
  600.                 }
  601.                 clientStream.Write(buffer, 0, buffer.Length);
  602.  
  603.                 #region Client read
  604.                 while (true)
  605.                 {
  606.                     bytesRead = 0;
  607.                     try
  608.                     {
  609.                         //blocks until a client sends a message
  610.                         bytesRead = clientStream.Read(message, 0, 4096);
  611.                         Console.WriteLine(bytesRead);
  612.                     }
  613.                     catch
  614.                     {
  615.                         //a socket error has occured
  616.                         break;
  617.                     }
  618.                     if (bytesRead == 0)
  619.                     {
  620.                         //the client has disconnected from the server
  621.                         Console.WriteLine("errorr1!!!!");
  622.                         break;
  623.                     }
  624.  
  625.                     if (bytesRead > 0)
  626.                     {
  627.                         client_msg = encoder.GetString(message, 0, bytesRead);
  628.                         Console.WriteLine("Join lobby command " + client_msg);
  629.                         switch (client_msg.ToLower())
  630.                         {
  631.                             case "join":
  632.                                 ///blah
  633.                                 buffer1 = encoder.GetBytes("lobbynum");
  634.                                 clientStream.Write(buffer1, 0, buffer1.Length);
  635.                                 #region Read from client
  636.                                 while (true)
  637.                                 {
  638.                                     bytesRead = 0;
  639.                                     try
  640.                                     {
  641.                                         //blocks until a client sends a message
  642.                                         bytesRead = clientStream.Read(message, 0, 4096);
  643.                                         Console.WriteLine(bytesRead);
  644.                                     }
  645.                                     catch
  646.                                     {
  647.                                         //a socket error has occured
  648.                                         break;
  649.                                     }
  650.                                     if (bytesRead == 0)
  651.                                     {
  652.                                         //the client has disconnected from the server
  653.                                         Console.WriteLine("errorr1!!!!");
  654.                                         break;
  655.                                     }
  656.  
  657.                                     if (bytesRead > 0)
  658.                                     {
  659.                                         client_msg = encoder.GetString(message, 0, bytesRead);
  660.                                         Console.WriteLine("Join Lobby: " + client_msg);
  661.                                         i = 0;
  662.                                         while (i < count)
  663.                                         {
  664.                                             try
  665.                                             {
  666.                                                 if (lobbies[i].objectNumber == Convert.ToInt32(client_msg))
  667.                                                 {
  668.                                                     buffer1 = encoder.GetBytes("joined lobby: " + lobbies[i].objectNumber);
  669.                                                     Console.WriteLine("i'm here!");
  670.                                                     clientStream.Write(buffer1, 0, buffer1.Length);
  671.                                                     lobbies[i].ready = true;
  672.                                                     lobbies[i].tcpPlayer2 = clientStream;
  673.                                                     lobbies[i].players = 2;
  674.                                                     //lobbies[i].start_game();
  675.                                                     startGame(lobbies[i].tcpPlayer1, lobbies[i].tcpPlayer2);
  676.  
  677.                                                 }
  678.                                             }
  679.                                             catch (Exception ex)
  680.                                             {
  681.                                                 Console.WriteLine(ex);
  682.  
  683.                                             }
  684.                                         i++;
  685.                                         }
  686.                                         buffer1 = encoder.GetBytes("unable to join lobby");
  687.                                         try
  688.                                         {
  689.                                             clientStream.Write(buffer1, 0, buffer1.Length);
  690.                                         }
  691.                                         catch (Exception ex)
  692.                                         {
  693.                                             Console.WriteLine(ex);
  694.                                         }
  695.                                     }
  696.                                 }
  697.                                 #endregion
  698.                                 break;
  699.                             default:
  700.                                 buffer1 = encoder.GetBytes("invalid");
  701.                                 clientStream.Write(buffer1, 0, buffer1.Length);
  702.                                 continue;
  703.  
  704.                         }
  705.                     }
  706.                 #endregion
  707.                 }
  708.             }
  709.         }
  710.  
  711.         private String Login(NetworkStream clientStream)
  712.         {
  713.             MySqlConnection conn = database();
  714.             Console.WriteLine("Login Screen");
  715.             byte[] buffer;
  716.             byte[] buffer1;
  717.             ASCIIEncoding encoder = new ASCIIEncoding();
  718.             buffer = encoder.GetBytes("UserName");
  719.             clientStream.Write(buffer, 0, buffer.Length);
  720.             bool un = false;
  721.             byte[] message = new byte[4096];
  722.             int bytesRead;
  723.             String client_msg = null;
  724.  
  725.             //buffer = encoder.GetBytes("enum_lobbies_EOF");
  726.             int i = 0;
  727.             while (true)
  728.             {
  729.                 bytesRead = 0;
  730.                 try
  731.                 {
  732.                     //blocks until a client sends a message
  733.                     bytesRead = clientStream.Read(message, 0, 4096);
  734.                     Console.WriteLine(bytesRead);
  735.                 }
  736.                 catch
  737.                 {
  738.                     //a socket error has occured
  739.                     break;
  740.                 }
  741.                 if (bytesRead == 0)
  742.                 {
  743.                     //the client has disconnected from the server
  744.                     Console.WriteLine("errorr1!!!!");
  745.                     break;
  746.                 }
  747.                 #region username and password
  748.                 if (bytesRead > 0)
  749.                 {
  750.                     client_msg = encoder.GetString(message, 0, bytesRead);
  751.                     String client_cmd = client_msg;
  752.                     Console.WriteLine("login");
  753.                     switch (client_msg.ToLower())
  754.                     {
  755.                         case "username":
  756.                             ///blah
  757.                             buffer1 = encoder.GetBytes("send username");
  758.                             clientStream.Write(buffer1, 0, buffer1.Length);
  759.                             #region Read from client
  760.                             while (true)
  761.                             {
  762.                                 bytesRead = 0;
  763.                                 try
  764.                                 {
  765.                                     //blocks until a client sends a message
  766.                                     bytesRead = clientStream.Read(message, 0, 4096);
  767.                                     Console.WriteLine(bytesRead);
  768.                                 }
  769.                                 catch
  770.                                 {
  771.                                     //a socket error has occured
  772.                                     break;
  773.                                 }
  774.                                 if (bytesRead == 0)
  775.                                 {
  776.                                     //the client has disconnected from the server
  777.                                     Console.WriteLine("errorr1!!!!");
  778.                                     break;
  779.                                 }
  780.  
  781.                                 if (bytesRead > 0)
  782.                                 {
  783.                                     client_msg = encoder.GetString(message, 0, bytesRead);
  784.                                     Console.WriteLine("User name: " + client_msg);
  785.                                     i = 0;
  786.                                     MySqlCommand command = conn.CreateCommand();
  787.                                     MySqlDataReader reader;
  788.  
  789.                                     #region username
  790.                                     Console.WriteLine("i'm here!");
  791.                                     command.CommandText = "Select * from user Where UserName='" + client_msg + "'";
  792.  
  793.                                     try
  794.                                     {
  795.                                         conn.Open();
  796.                                         reader = command.ExecuteReader();
  797.                                         Console.WriteLine("i'm here!2");
  798.                                         while (reader.Read()) //take the sql and make sure the username exists
  799.                                         {
  800.                                             Console.WriteLine("i'm here!3");
  801.                                             Console.WriteLine(reader["UserName"] + " : " + client_msg);
  802.                                             String result = reader["UserName"].ToString();
  803.                                             if (client_msg.Equals(result, StringComparison.Ordinal))
  804.                                             {
  805.                                                 Console.WriteLine("woot! matching username!");
  806.                                                 buffer1 = encoder.GetBytes("send password");
  807.                                                 clientStream.Write(buffer1, 0, buffer1.Length);
  808.                                                 try
  809.                                                 {
  810.                                                     //conn.Open();
  811.                                                     //reader.Close();
  812.                                                     //reader = command.ExecuteReader();
  813.                                                     while (true)
  814.                                                     {
  815.                                                         bytesRead = 0;
  816.                                                         try
  817.                                                         {
  818.                                                             //blocks until a client sends a message
  819.                                                             bytesRead = clientStream.Read(message, 0, 4096);
  820.                                                             Console.WriteLine(bytesRead);
  821.                                                         }
  822.                                                         catch
  823.                                                         {
  824.                                                             //a socket error has occured
  825.                                                             break;
  826.                                                         }
  827.                                                         if (bytesRead == 0)
  828.                                                         {
  829.                                                             //the client has disconnected from the server
  830.                                                             Console.WriteLine("errorr1!!!!");
  831.                                                             break;
  832.                                                         }
  833.  
  834.                                                         if (bytesRead > 0)
  835.                                                         {
  836.                                                             client_msg = encoder.GetString(message, 0, bytesRead);
  837.                                                             String password = reader["Password"].ToString();
  838.                                                             Console.WriteLine("password: " + client_msg + " : " + password);
  839.                                                             if (password.Equals(client_msg, StringComparison.Ordinal))
  840.                                                             {
  841.                                                                 Console.WriteLine("woot! matching password!");
  842.                                                                 buffer1 = encoder.GetBytes("login_suc");
  843.                                                                 clientStream.Write(buffer1, 0, buffer1.Length);
  844.                                                                 return reader["UserName"].ToString();
  845.                                                                 // do some profile stuff here?
  846.                                                                 break;
  847.                                                             }
  848.                                                             else
  849.                                                             {
  850.                                                                 return "INVALID";
  851.                                                             }
  852.                                                            
  853.                                                         }
  854.                                                     }
  855.                                                     break;
  856.  
  857.                                                 }
  858.                                                 catch (Exception ex)
  859.                                                 {
  860.                                                     Console.WriteLine(ex);
  861.                                                 }
  862.                                             }
  863.                                         }
  864.                                         break;
  865.  
  866.                                     }
  867.                                     catch (Exception ex)
  868.                                     {
  869.                                         Console.WriteLine(ex);
  870.                                     }
  871.                                    
  872.                                     #endregion
  873.                                    
  874.                                    
  875.                                    
  876.                                 }
  877.                             }
  878.                             #endregion
  879.                             break;
  880.                         default:
  881.                             buffer1 = encoder.GetBytes("invalid");
  882.                             clientStream.Write(buffer1, 0, buffer1.Length);
  883.                             continue;
  884.  
  885.                     }
  886.                 }
  887.                 #endregion
  888.  
  889.                 break;
  890.             }
  891.             return "INVALID";
  892.         }
  893.  
  894.         private void UpdateProfile(NetworkStream clientStream, String username)
  895.         {
  896.  
  897.         }
  898.  
  899.         private void GetProfile(NetworkStream clientStream, String username)
  900.         {
  901.  
  902.         }
  903.  
  904.         //Player client1 = new Player();
  905.         //Player client2 = new Player();
  906.  
  907.  
  908.  
  909.  
  910.         private static void Main(string[] args)
  911.         {
  912.             Server derp = new Server();
  913.             derp.Server_start();
  914.             //derp.database();
  915.  
  916.         }
  917.  
  918.     }
  919. }
Add Comment
Please, Sign In to add comment