Guest User

Untitled

a guest
Sep 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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.Net.Sockets;
  10. using System.Threading;
  11. using System.Net;
  12. using MySql.Data.MySqlClient;
  13.  
  14. //new
  15. using System.IO
  16.  
  17. namespace Incursion_Server
  18. {
  19. public partial class Registration_Server : Form
  20. {
  21. tcp_server MyServer = new tcp_server();
  22.  
  23. public Registration_Server()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. class tcp_server
  29. {
  30. private TcpListener tcpListener;
  31. private Thread listenThread;
  32.  
  33. public tcp_server()
  34. {
  35. this.tcpListener = new TcpListener(IPAddress.Any, 3000);
  36. this.listenThread = new Thread(new ThreadStart(ListenForClients));
  37. this.listenThread.Start();
  38. }
  39.  
  40. private void ListenForClients()
  41. {
  42. this.tcpListener.Start();
  43.  
  44. while (true)
  45. {
  46. //blocks until a client has connected to the server
  47. TcpClient client = this.tcpListener.AcceptTcpClient();
  48. MessageBox.Show("Client Connected.");
  49.  
  50. //create a thread to handle communication
  51. //with connected client
  52. Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
  53. clientThread.Start(client);
  54. }
  55. }
  56.  
  57. private void HandleClientComm(object client)
  58. {
  59. TcpClient tcpClient = (TcpClient)client;
  60. NetworkStream clientStream = tcpClient.GetStream();
  61. using (BinaryReader reader = new BinaryReader(clientStream))
  62. {
  63.  
  64. string username, pw, email;
  65. while (true)
  66. {
  67. #region stuff....
  68. ////////////////////////////
  69. // MYSQL CONNECTION INFO //
  70. //////////////////////////
  71. string connString = "Server=196.220.60.236;Port=3307;Database=gamedb;Uid=testuser;password=testpass;"; // INTERNET DATABASE
  72. MySqlConnection conn = new MySqlConnection(connString);
  73. MySqlCommand command = conn.CreateCommand();
  74. ////////////////////////////
  75. // MYSQL CONNECTION INFO //
  76. //////////////////////////
  77.  
  78. //////////////////////////////////////
  79. // READ BYTES RECIEVED FROM CLIENT //
  80. ////////////////////////////////////
  81. try
  82. {
  83.  
  84. //blocks until a client sends a message
  85. username = reader.ReadString();//THE ORDER MATTERS!!!
  86. pw = reader.ReadString();
  87. email = reader.ReadString();
  88. }
  89. catch
  90. {
  91. //PROBABLY a socket error has occured
  92. break;
  93. }
  94.  
  95.  
  96. ///////////////////////
  97. // INSERT STATEMENT //
  98. /////////////////////
  99. if (username == null || username.Length == 0)
  100. {
  101. //throw?
  102. }
  103. else if (pw == null || pw.Length == 0)
  104. {
  105. //throw?
  106. }
  107. else if (email == null || email.Length == 0)
  108. {
  109. //throw?
  110. }
  111. else
  112. {
  113. command.CommandText = "Insert into players (id,username,email,password) values('',@username,@email,@pw)";
  114. command.Parameters.AddWithValue("@username", username);
  115. command.Parameters.AddWithValue("@email", email);
  116. command.Parameters.AddWithValue("@pw", password);
  117. try
  118. {
  119. conn.Open();
  120. command.ExecuteNonQuery();
  121. }
  122. catch (Exception ex)
  123. {
  124. MessageBox.Show(ex.Message);
  125. }
  126. MessageBox.Show("DONE!");
  127. }
  128. ///////////////////////
  129. // INSERT STATEMENT //
  130. /////////////////////
  131.  
  132. /////////////////////////////
  133. // CLOSE MYSQL CONNECTION //
  134. ///////////////////////////
  135. conn.Close();
  136. /////////////////////////////
  137. // CLOSE MYSQL CONNECTION //
  138. ///////////////////////////
  139. #endregion
  140. }
  141. }
  142. }
  143.  
  144. }
  145.  
  146. private void button1_Click(object sender, EventArgs e)
  147. {
  148. this.Visible = false;
  149. }
  150. }
  151. }
Add Comment
Please, Sign In to add comment