Guest User

Untitled

a guest
Feb 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 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. command.CommandText = "Insert into players (id,username,email,password) values('',@username,@email,@pw)";
  100. command.Parameters.AddWithValue("@username", username);
  101. command.Parameters.AddWithValue("@email", email);
  102. command.Parameters.AddWithValue("@pw", pw);
  103. try
  104. {
  105. conn.Open();
  106. command.ExecuteNonQuery();
  107. }
  108. catch (Exception ex)
  109. {
  110. MessageBox.Show(ex.Message);
  111. }
  112. MessageBox.Show("DONE!");
  113. ///////////////////////
  114. // INSERT STATEMENT //
  115. /////////////////////
  116.  
  117. /////////////////////////////
  118. // CLOSE MYSQL CONNECTION //
  119. ///////////////////////////
  120. conn.Close();
  121. /////////////////////////////
  122. // CLOSE MYSQL CONNECTION //
  123. ///////////////////////////
  124. #endregion
  125. }
  126. }
  127. }
  128.  
  129. }
  130.  
  131. private void button1_Click(object sender, EventArgs e)
  132. {
  133. this.Visible = false;
  134. }
  135. }
  136. }
Add Comment
Please, Sign In to add comment