Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- using MySql.Data.MySqlClient;
- //new
- using System.IO
- namespace Incursion_Server
- {
- public partial class Registration_Server : Form
- {
- tcp_server MyServer = new tcp_server();
- public Registration_Server()
- {
- InitializeComponent();
- }
- class tcp_server
- {
- private TcpListener tcpListener;
- private Thread listenThread;
- public tcp_server()
- {
- this.tcpListener = new TcpListener(IPAddress.Any, 3000);
- this.listenThread = new Thread(new ThreadStart(ListenForClients));
- this.listenThread.Start();
- }
- private void ListenForClients()
- {
- this.tcpListener.Start();
- while (true)
- {
- //blocks until a client has connected to the server
- TcpClient client = this.tcpListener.AcceptTcpClient();
- MessageBox.Show("Client Connected.");
- //create a thread to handle communication
- //with connected client
- Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
- clientThread.Start(client);
- }
- }
- private void HandleClientComm(object client)
- {
- TcpClient tcpClient = (TcpClient)client;
- NetworkStream clientStream = tcpClient.GetStream();
- using (BinaryReader reader = new BinaryReader(clientStream))
- {
- string username, pw, email;
- while (true)
- {
- #region stuff....
- ////////////////////////////
- // MYSQL CONNECTION INFO //
- //////////////////////////
- string connString = "Server=196.220.60.236;Port=3307;Database=gamedb;Uid=testuser;password=testpass;"; // INTERNET DATABASE
- MySqlConnection conn = new MySqlConnection(connString);
- MySqlCommand command = conn.CreateCommand();
- ////////////////////////////
- // MYSQL CONNECTION INFO //
- //////////////////////////
- //////////////////////////////////////
- // READ BYTES RECIEVED FROM CLIENT //
- ////////////////////////////////////
- try
- {
- //blocks until a client sends a message
- username = reader.ReadString();//THE ORDER MATTERS!!!
- pw = reader.ReadString();
- email = reader.ReadString();
- }
- catch
- {
- //PROBABLY a socket error has occured
- break;
- }
- ///////////////////////
- // INSERT STATEMENT //
- /////////////////////
- if (username == null || username.Length == 0)
- {
- //throw?
- }
- else if (pw == null || pw.Length == 0)
- {
- //throw?
- }
- else if (email == null || email.Length == 0)
- {
- //throw?
- }
- else
- {
- command.CommandText = "Insert into players (id,username,email,password) values('',@username,@email,@pw)";
- command.Parameters.AddWithValue("@username", username);
- command.Parameters.AddWithValue("@email", email);
- command.Parameters.AddWithValue("@pw", password);
- try
- {
- conn.Open();
- command.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- MessageBox.Show("DONE!");
- }
- ///////////////////////
- // INSERT STATEMENT //
- /////////////////////
- /////////////////////////////
- // CLOSE MYSQL CONNECTION //
- ///////////////////////////
- conn.Close();
- /////////////////////////////
- // CLOSE MYSQL CONNECTION //
- ///////////////////////////
- #endregion
- }
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.Visible = false;
- }
- }
- }
Add Comment
Please, Sign In to add comment