📄 msntest.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
namespace MSNTestNamespace
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MSNTest : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblUsername;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.TextBox txtUsername;
private System.Windows.Forms.Label lblScreenName;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MSNTest()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.btnConnect = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.lblUsername = new System.Windows.Forms.Label();
this.txtPassword = new System.Windows.Forms.TextBox();
this.txtUsername = new System.Windows.Forms.TextBox();
this.lblScreenName = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.InactiveBorder;
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.lblUsername);
this.panel1.Controls.Add(this.txtPassword);
this.panel1.Controls.Add(this.txtUsername);
this.panel1.Location = new System.Drawing.Point(8, 16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(192, 144);
this.panel1.TabIndex = 5;
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(204, 232);
this.btnConnect.Name = "btnConnect";
this.btnConnect.TabIndex = 0;
this.btnConnect.Text = "Connect Me";
this.btnConnect.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(6, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 8;
this.label2.Text = "Password";
//
// lblUsername
//
this.lblUsername.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblUsername.Location = new System.Drawing.Point(6, 8);
this.lblUsername.Name = "lblUsername";
this.lblUsername.TabIndex = 7;
this.lblUsername.Text = "Username";
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(8, 88);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(168, 20);
this.txtPassword.TabIndex = 6;
this.txtPassword.Text = "";
//
// txtUsername
//
this.txtUsername.Location = new System.Drawing.Point(8, 32);
this.txtUsername.Name = "txtUsername";
this.txtUsername.Size = new System.Drawing.Size(168, 20);
this.txtUsername.TabIndex = 5;
this.txtUsername.Text = "";
//
// lblScreenName
//
this.lblScreenName.Location = new System.Drawing.Point(8, 168);
this.lblScreenName.Name = "lblScreenName";
this.lblScreenName.Size = new System.Drawing.Size(272, 48);
this.lblScreenName.TabIndex = 6;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.lblScreenName);
this.Controls.Add(this.panel1);
this.Controls.Add(this.btnConnect);
this.Name = "Form1";
this.Text = "MSN Test MSNP9";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MSNTest());
}
private void button1_Click(object sender, System.EventArgs e)
{
MSNConnection MSN = new MSNConnection();
string username = txtUsername.Text;
string password = txtPassword.Text;
if (username != "" && password != "")
{
lblScreenName.Text = "Connecting please wait";
lblScreenName.Update();
int result = MSN.Connect(username, password);
lblScreenName.Text = "";
lblScreenName.Update();
if (result == 0)
{
MessageBox.Show("Connected as '" + MSN._UserName + "'", "MSN Test",MessageBoxButtons.OK, MessageBoxIcon.Information);
lblScreenName.Text = "Your screenname: " + MSN._ScreenName.Replace("%20", " ");
}
else if (result == 401)
{
MessageBox.Show("Invalid credentials, please try again", "MSN Test",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Unknow error occured while connecting", "MSN Test",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Invalid credentials, please try again", "MSN Test",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public class MSNConnection
{
// These are the numbers where the addresses wil appear in the arraylist
// it is not the niced solution, but it works a lot faster
// you can use some kind of IDictionary object to put the value behind a key name
//
// PASPORTURLS
public int DARREALM = 0;
public int DALOGIN = 1;
public int DAREG = 2;
public int PROPERTIES = 3;
public int GENERALDIR = 4;
public int HELP = 5;
public int CONFIGVERSION = 6;
public ArrayList PassportUrls;
/// <summary>
/// Every message from a client has a unique transactionID
/// With every message this number will be increased
/// </summary>
private long _transactionID = 0;
private TcpClient _socket;
private NetworkStream _stream;
private StreamReader _reader;
private StreamWriter _writer;
public string _UserName;
public string _ScreenName;
protected bool DataAvailable
{
get { return _stream.DataAvailable; }
}
public bool Connected
{
get { return _socket != null; }
}
/// <summary>
/// Make a socket connection and streamers
/// </summary>
/// <param name="host">IP host to connect to</param>
/// <param name="port">IP port to connect to</param>
protected void ConnectSocket(string host, int port)
{
Console.WriteLine("Connecting to " + host + ":" + port);
_transactionID = 0;
_socket = new TcpClient(host, port);
_stream = _socket.GetStream();
_reader = new StreamReader(_stream, Encoding.ASCII);
_writer = new StreamWriter(_stream, Encoding.ASCII);
_writer.AutoFlush = true;
}
/// <summary>
/// Write a message to the current socket, this functions raises the transactionID
/// </summary>
/// <param name="line">The Message</param>
/// <param name="writeNewLine">Determine if you write a line, or only a message (with or withhout ending character</param>
private void WriteLine(string line, bool writeNewLine)
{
Console.WriteLine("Writing: " + line);
if (writeNewLine)
_writer.WriteLine(line);
else
_writer.Write(line);
// raise the transactionId
_transactionID++;
}
/// <summary>
/// Write a command to the server
/// </summary>
/// <param name="command">The first command</param>
/// <param name="parameters">The parameters that have to be send to the server</param>
/// <param name="bSendId">Sometimes you don't have to send an transactionID</param>
protected void WriteCommand(string command,string parameters,bool bSendId)
{
string line;
// check what type of format it should be
if (bSendId)
line = string.Format("{0} {1} {2}", command, _transactionID, parameters);
else
line = string.Format("{0} {1}", command, parameters);
// Write the line
WriteLine(line, true);
}
/// <summary>
/// This function read the information from the streamreader
/// and if it read something it returns a new ServerCommand object
/// </summary>
/// <returns>if there is something return new ServerCommand object</returns>
protected ServerCommand ReadCommand()
{
string line = _reader.ReadLine();
Console.WriteLine("Reading: " + line);
if (line == null)
{
Console.WriteLine("Nothing received");
return new ServerCommand();
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -