浏览 10756 次
锁定老帖子 主题:MSNP 实现
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-02  
 using System.Text;
 using System.Net;
 using System.Net.Sockets;

public class MSocket
 {
     private Socket mSock;
     private IPEndPoint epServer;
     private byte[] m_byBuff = new byte[257];
     private string m_Data;
     public event On_DatarecievedEventHandler On_Datarecieved;
     public delegate void On_DatarecievedEventHandler(string data);
     public event On_ConnectEventHandler On_Connect;
     public delegate void On_ConnectEventHandler();

     public MSocket(string server, int port)
     {
         IPHostEntry iphe;
         IPAddress ipad;
         AsyncCallback oc;
         mSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         iphe = Dns.Resolve(server);
         foreach (string ipad in iphe.AddressList) {
             epServer = new IPEndPoint(ipad, port);
         }
         mSock.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
         mSock.Blocking = false;
         oc = new AsyncCallback(OnConnect);
         mSock.BeginConnect(epServer, oc, mSock);
     }

     public object Write(string sData)
     {
         byte[] msg;
         msg = Encoding.ASCII.GetBytes(sData);
         mSock.Send(msg);
     }

     private object SetupRecieveCallback(Socket sock)
     {
         AsyncCallback receiveData;
         receiveData = new AsyncCallback(OnReceivedData);
         sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, receiveData, sock);
     }

     private void OnConnect(IAsyncResult ar)
     {
         Socket sock;
         sock = (Socket)ar.AsyncState;
         if (sock.Connected == true) {
             SetupRecieveCallback(sock);
             if (On_Connect != null) {
                 On_Connect();
             }
         }
     }

     private void OnReceivedData(IAsyncResult ar)
     {
         int nBytesRec;
         Socket sock;
         string sRecieved;
         sock = (Socket)ar.AsyncState;
         nBytesRec = sock.EndReceive(ar);
         if (nBytesRec > 0) {
             sRecieved = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
             if (On_DataRecieved != null) {
                 On_DataRecieved(sRecieved);
             }
             m_Data = sRecieved;
             SetupRecieveCallback(sock);
         }
         else {
             sock.Shutdown(SocketShutdown.Both);
             sock.Close();
         }
     }
 }
0 请登录后投票
   发表时间:2007-09-02  
fyi:http://atlas-connect-forum.web.cern.ch/Atlas-connect-forum/documents_page.htm
0 请登录后投票
   发表时间:2007-09-02  
/// File: MsnSharp\TestMsn\Form1.cs
/// 
/// ------------------------------------------------------------
/// Copyright (c) 2004
///   Antonio Cisternino (cisterni.unipi.it),
///   Diego Colombo      (colombod.unipi.it),
///   Giorgio Ennas      (   ennas.unipi.it),
///   Daniele Picciaia   (picciaia.unipi.it)
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Msn;

namespace TestMsn
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private Msn.Manager msnManager = null;
		private Msn.Session msnSession = null;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtEmail;
		private System.Windows.Forms.Button cmdSingin;
		private System.Windows.Forms.Button cmdinvite;
		private System.Windows.Forms.ListBox lstUsers;
		private System.Windows.Forms.Button cmdSignout;
		private System.Windows.Forms.TextBox txtPwd;
		private System.Windows.Forms.Button cmdSend;
		private System.Windows.Forms.TextBox txtMsg;
		private System.Windows.Forms.TextBox txtOut;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			msnManager = new Manager();
		}

		/// <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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtEmail = new System.Windows.Forms.TextBox();
			this.cmdSingin = new System.Windows.Forms.Button();
			this.cmdinvite = new System.Windows.Forms.Button();
			this.lstUsers = new System.Windows.Forms.ListBox();
			this.cmdSignout = new System.Windows.Forms.Button();
			this.txtPwd = new System.Windows.Forms.TextBox();
			this.cmdSend = new System.Windows.Forms.Button();
			this.txtMsg = new System.Windows.Forms.TextBox();
			this.txtOut = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "email";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "password";
			// 
			// txtEmail
			// 
			this.txtEmail.Location = new System.Drawing.Point(104, 6);
			this.txtEmail.Name = "txtEmail";
			this.txtEmail.Size = new System.Drawing.Size(104, 20);
			this.txtEmail.TabIndex = 2;
			this.txtEmail.Tag = " ";
			this.txtEmail.Text = "";
			// 
			// cmdSingin
			// 
			this.cmdSingin.Location = new System.Drawing.Point(224, 4);
			this.cmdSingin.Name = "cmdSingin";
			this.cmdSingin.Size = new System.Drawing.Size(56, 24);
			this.cmdSingin.TabIndex = 4;
			this.cmdSingin.Text = "Sing in";
			this.cmdSingin.Click += new System.EventHandler(this.cmdSingin_Click);
			// 
			// cmdinvite
			// 
			this.cmdinvite.Location = new System.Drawing.Point(224, 104);
			this.cmdinvite.Name = "cmdinvite";
			this.cmdinvite.Size = new System.Drawing.Size(56, 32);
			this.cmdinvite.TabIndex = 6;
			this.cmdinvite.Text = "Invite User";
			this.cmdinvite.Click += new System.EventHandler(this.cmdinvite_Click);
			// 
			// lstUsers
			// 
			this.lstUsers.Location = new System.Drawing.Point(8, 64);
			this.lstUsers.Name = "lstUsers";
			this.lstUsers.Size = new System.Drawing.Size(200, 147);
			this.lstUsers.TabIndex = 7;
			// 
			// cmdSignout
			// 
			this.cmdSignout.Location = new System.Drawing.Point(224, 32);
			this.cmdSignout.Name = "cmdSignout";
			this.cmdSignout.Size = new System.Drawing.Size(56, 24);
			this.cmdSignout.TabIndex = 5;
			this.cmdSignout.Text = "Sing out";
			// 
			// txtPwd
			// 
			this.txtPwd.Location = new System.Drawing.Point(104, 30);
			this.txtPwd.Name = "txtPwd";
			this.txtPwd.PasswordChar = '*';
			this.txtPwd.Size = new System.Drawing.Size(104, 20);
			this.txtPwd.TabIndex = 3;
			this.txtPwd.Tag = " ";
			this.txtPwd.Text = "";
			// 
			// cmdSend
			// 
			this.cmdSend.Location = new System.Drawing.Point(224, 304);
			this.cmdSend.Name = "cmdSend";
			this.cmdSend.Size = new System.Drawing.Size(56, 24);
			this.cmdSend.TabIndex = 9;
			this.cmdSend.Text = "Send";
			this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
			// 
			// txtMsg
			// 
			this.txtMsg.Location = new System.Drawing.Point(8, 304);
			this.txtMsg.Multiline = true;
			this.txtMsg.Name = "txtMsg";
			this.txtMsg.PasswordChar = '*';
			this.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.txtMsg.Size = new System.Drawing.Size(200, 32);
			this.txtMsg.TabIndex = 8;
			this.txtMsg.Tag = " ";
			this.txtMsg.Text = "";
			// 
			// txtOut
			// 
			this.txtOut.Location = new System.Drawing.Point(8, 224);
			this.txtOut.Multiline = true;
			this.txtOut.Name = "txtOut";
			this.txtOut.PasswordChar = '*';
			this.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.txtOut.Size = new System.Drawing.Size(280, 72);
			this.txtOut.TabIndex = 10;
			this.txtOut.Tag = " ";
			this.txtOut.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 342);
			this.Controls.Add(this.txtOut);
			this.Controls.Add(this.cmdSend);
			this.Controls.Add(this.txtMsg);
			this.Controls.Add(this.lstUsers);
			this.Controls.Add(this.cmdinvite);
			this.Controls.Add(this.cmdSignout);
			this.Controls.Add(this.cmdSingin);
			this.Controls.Add(this.txtPwd);
			this.Controls.Add(this.txtEmail);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void cmdSingin_Click(object sender, System.EventArgs e)
		{
			msnManager.LogIn(txtEmail.Text,txtPwd.Text);
			msnSession = msnManager.CreateSession();
			msnSession.CallbackMethod = new SessionCallback(MessageArrived);
		}

		private void cmdinvite_Click(object sender, System.EventArgs e)
		{
			msnSession.InviteUser("litianbao@hotmail.com");
		}

		private void cmdSend_Click(object sender, System.EventArgs e)
		{
			msnSession.SendMessage(txtMsg.Text);
		}

		private void MessageArrived(Msn.SessionEvent msnEvent, string msg)
		{
			txtOut.Text += msg + "\r\n";
		}
	}
}

[url]
268435500 %3Cmsnobj%20Creator%3D%22dasein1982%40hotmail.com%22%20Size%3D%2219712%22%20Type%3D%223%22%20Location%3D%22TFR715.tmp%22%20Friendly%3D%22AAA%3D%22%20SHA1D%3D%22VXRmTeZ3ivxmFNtkgipnm4gY7XE%3D%22%20SHA1C%3D%22h%2FwlPba3obYUbK9FsehrnTaDPP8%3D%22%2F%3E\r\n
[/url]
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics