📄 loginform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using CommonClassLibrary;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace PlayCardClient
{
public partial class LoginForm : Form
{
private TcpClient Client;
private byte[] recByte;
private SplitBytes sb;
public LoginForm()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (txtUserID.Text.Trim().Length > 0 && txtPassword.Text.Trim().Length > 0)
{
Authenticate();
}
else
{
MessageBox.Show("Enter a valid username and password.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
DialogResult = DialogResult.None;
}
}
public static DialogResult ShowLogin()
{
LoginForm frm = new LoginForm();
return frm.ShowDialog();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void LoginForm_Load(object sender, EventArgs e)
{
sb = new SplitBytes();
recByte = new byte[1024];
}
private void Authenticate()
{
Cursor = Cursors.WaitCursor;
Refresh();
txtUserID.ReadOnly = false;
txtPassword.ReadOnly = false;
btnSubmit.Enabled = false;
btnExit.Enabled = false;
try
{
string IP = LocalIP();
//Connect to server
Client = SocketHelper.Instance().Connection;
//Start Reading
AsyncCallback GetMsgCallback = new AsyncCallback(GetMsg);
(Client.GetStream()).BeginRead(recByte, 0, 1024, GetMsgCallback, null);
LoginUser u = new LoginUser();
u.Protocol = "501";
u.UserID = txtUserID.Text.Trim();
u.Password = txtPassword.Text.Trim();
u.IP = IP;
SendText(SerializationFormatter.GetSerializationBytes(u));
}
catch(Exception ex)
{
Disconnect();
MessageBox.Show(ex.Message);
}
finally
{
Cursor = Cursors.Default;
txtUserID.ReadOnly = true;
txtPassword.ReadOnly = true;
btnSubmit.Enabled = true;
btnExit.Enabled = true;
}
}
private string LocalIP()
{
string localHostName;
IPHostEntry localHostEntry;
IPAddress[] addrList;
int i;
string strTemp = "";
localHostName = Dns.GetHostName();
localHostEntry = Dns.GetHostByName(localHostName);
addrList = localHostEntry.AddressList;
for (i = 0; i < addrList.Length; i++)
{
strTemp = strTemp + addrList[i].ToString() + " ";
}
return strTemp.Trim();
}
private void Disconnect()
{
if (Client != null)
{
Client.Close();
Client = null;
}
}
private void GetMsg(IAsyncResult ar)
{
int numberOfBytesRead;
lock (Client.GetStream())
{
numberOfBytesRead = Client.GetStream().EndRead(ar);
if (numberOfBytesRead < 1)
{
Disconnect();
MessageBox.Show("Disconnected!!");
return;
}
}
sb.AddBytes(recByte, numberOfBytesRead);
recByte = new byte[1024];
if (Client.GetStream().DataAvailable)
{
Client.GetStream().BeginRead(recByte, 0, recByte.Length, new AsyncCallback(GetMsg), Client.GetStream());
}
else
{
BuildText(sb.ReceiveAllByte);
sb.Dispose();
}
}
private void BuildText(byte[] dataByte)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(dataByte);
CommonProtocol obj = (CommonProtocol)formatter.Deserialize(stream);
stream.Close();
string Protocol = obj.Protocol;
switch (Protocol)
{
case "502":
LoginUser u = (LoginUser)obj;
if (u.IsLogin)
{
UserSettings.Instance.UserID = txtUserID.Text;
UserSettings.Instance.Password = txtPassword.Text;
UserSettings.Instance.UserName = u.UserName;
UserSettings.Instance.IP = u.IP;
DialogResult = DialogResult.OK;
}
else
{
SocketHelper.Instance().Dispose();
MessageBox.Show("Make sure you UserID and password is right.");
this.DialogResult = DialogResult.Retry;
}
break;
}
}
private void SendText(byte[] byteMessage)
{
BinaryWriter writer = new BinaryWriter(Client.GetStream()); ;
writer.Write(byteMessage);
writer.Flush();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -