frmShare.cs
资源名称:P2P c#.rar [点击查看]
上传用户:hkhxjs
上传日期:2008-11-25
资源大小:155k
文件大小:42k
源码类别:
Internet/网络编程
开发平台:
C#
- namespace Client // Copyright 2001 Dreamtech Software India Inc.
- { // All rights reserved
- using System; // Provides the basic functionality of .NET
- using System.Drawing; // Provides the Drawing features, Used for cursors
- using System.Windows.Forms; // Provides the darwing of buttons, listviews etc
- using System.Net; // Provides the net related functionality
- using System.Net.Sockets; // Provides the functionality of sockets
- using System.Text; // Provides the text manipulation functions
- using System.IO; // Provides I/O features
- using WorkingWithXML; // Custom class
- using System.Collections; // Provides the different type of class collections
- using System.ComponentModel;// Provides the facility of using components
- /// <summary>
- /// Summary description for frmShare.
- /// </summary>
- public class frmShare : System.Windows.Forms.Form
- {
- private System.ComponentModel.IContainer components;
- private System.Windows.Forms.OpenFileDialog FileOpenDialog;
- public System.Windows.Forms.Button btnClose;
- public System.Windows.Forms.StatusBar sBar;
- private System.Windows.Forms.SaveFileDialog FileSaveDialog;
- private System.Windows.Forms.ToolTip toolTipText;
- public System.Windows.Forms.ListView lvFiles;
- public System.Windows.Forms.Button btnSearch;
- public System.Windows.Forms.Button btnDownload;
- public System.Windows.Forms.Button btnUpload;
- private System.Windows.Forms.ColumnHeader clhFilename;
- private System.Windows.Forms.ColumnHeader clhFileSize;
- private System.Windows.Forms.ColumnHeader clhType;
- /// <summary>
- /// User defined variables.
- /// </summary>
- /// <summary>
- /// stores the number of bytes written or read from any stream
- /// </summary>
- private int iBytes;
- /// <summary>
- /// These variables are used to store the name of the computer
- /// to which you have connected and the parent folder name.
- /// Parent folder name is the name of the folder of the contents
- /// which you are viewing in the window
- /// </summary>
- private string COMPUTERNAME,PARENTFOLDER;
- /// <summary>
- /// Stores a new created socket of type TCPClient(System defined class)
- /// used to communicate with the listener
- /// </summary>
- private TcpClient ClientSocket;
- /// <summary>
- /// creates a variable for XMLCreater(User defined class) to
- /// create XML requests for the listener
- /// </summary>
- private XMLCreater xmlCreate;
- /// <summary>
- /// StreamTCP points to the NetworkStream(System defined class)
- /// which is used for transfer data over the Socket connection
- /// </summary>
- private NetworkStream StreamTCP;
- /// <summary>
- /// fileStream is an object of type FileStream(System defined class)
- /// used to have I/O capabilities for files which are used
- /// in this program
- /// </summary>
- private FileStream fileStream;
- /// <summary>
- /// ReadBuffer and WriteBuffer Byte arrays used for
- /// Reading and Writing any file.
- /// </summary>
- private Byte[] ReadBuffer,WriteBuffer;
- /// <summary>
- /// xmlParser is of type XMLParser(User defined class)
- /// It is used to have the access for Parsing the XML file
- /// This class is present in WorkingWithXML
- /// </summary>
- private XMLParser xmlParser;
- /// <summary>
- /// xmlStruct is of type XMLSTRUCT(User defined structure)
- /// It is used to store the different records obtained
- /// from parsing the XML. This structure is present in WorkingWithXML
- /// </summary>
- private XMLSTRUCT xmlStruct;
- /// <summary>
- /// strArray if of Type __SHOWFILES of XMLSTRUCT Structure and
- /// used to save the corresponding Files/Folder which are seen in the
- /// the List view at run time
- /// </summary>
- private XMLSTRUCT.__SHOWFILES[] strArray;
- /// <summary>
- /// The below variables are readonly variables that u cannot
- /// assign a new value to these variables again. They are
- /// constant type variables
- /// </summary>
- /// <summary>
- /// REQUESTFILE have the name of the file which have to be created
- /// for a particular request
- /// </summary>
- public readonly string REQUESTFILE = Application.StartupPath + "\Request.xml";
- /// <summary>
- /// RESPONSEFILE have the name of the file which have to be created
- /// for a particular request
- /// </summary>
- public readonly string RESPONSEFILE = Application.StartupPath + "\Response.xml";
- private System.Windows.Forms.Label lblCopyright;
- /// <summary>
- /// MAX_SIZE defines the maximum size of the read or write buffer
- /// which is used for reading or wrting a file
- /// </summary>
- public readonly int MAX_SIZE = 512;
- /// <summary>
- /// This is the default contructor of the this class
- /// This is called from the View files button
- /// </summary>
- public frmShare()
- {
- //
- // Required for Windows Form Designer support
- //
- // Auto generated code line by the IDE
- InitializeComponent();
- // Puts the Computer.ico as the form icon
- this.Icon = new System.Drawing.Icon(Application.StartupPath + "\Computer.ico");
- // Changes the Caption of this dialog box
- this.Text = "Search Result";
- sBar.Text = "Root";
- COMPUTERNAME = null;
- PARENTFOLDER = null;
- }
- /// <summary>
- /// This is a user defined constructor called from the
- /// Open button in the frmClient
- /// Computername is passed to this function, which you
- /// have selected from the Main window
- /// </summary>
- /// <param name="Computername"> </param>
- public frmShare(string Computername)
- {
- //
- // Required for Windows Form Designer support
- // This line is not auto generated, instead it has been copied from
- // default constructor
- InitializeComponent();
- // Puts the Computer.ico as the form icon
- this.Icon = new System.Drawing.Icon(Application.StartupPath + "\Computer.ico");
- // COMPUTERNAME is a global variable used to store the name
- // and IP Address of the computer to which you are currently
- // connected
- COMPUTERNAME = Computername;
- // Open connection is a user defined function responsible for
- // opening a socket connection for listener. This function
- // returns a bool value
- if( OpenConnection(COMPUTERNAME) )
- {
- // This will creates a SHOWFILE request XML for seding it
- // to the listener
- CreateRequest("SHOWFILES","","");
- // This will actually sends the REQUESTFILE to the listener
- SendDataToListener(REQUESTFILE);
- // This will get the response of the above request from
- // listener and store that response in a RESPONSEFILE
- GetDataFromListener(RESPONSEFILE);
- // This will Parse that response XML File and results are
- // shown to the user
- Parsing(RESPONSEFILE);
- // Closes any Opened socket or stream connection
- CloseConnection();
- // Since this constructor is called at the root level
- // therefore no parent folder is associated with it
- PARENTFOLDER = null;
- // Changes the caption of this dialog box
- this.Text = "Shared contents on: " + Computername.ToUpper();
- // Sets the text for the Status bar
- sBar.Text = "Root";
- }
- }
- /// <summary>
- /// This is also a user defined constructor called from the
- /// Search button to view the search results
- /// This constructor is called from within this code only
- /// it is used to pass any type of request if needed at the time of
- /// its construction
- /// </summary>
- /// <param name="Computername stores the name of the computer to connect"> </param>
- /// <param name="Request stores what type of request you want to send to the listener"> </param>
- /// <param name="Scope scope value needed, if any"> </param>
- /// <param name="Mask mask value needed if any"> </param>
- public frmShare(string Computername, string Request, string Scope, string Mask)
- {
- // Required for Windows Form Designer support
- // This line is not auto generated, instead it has been copied from
- // default constructor
- InitializeComponent();
- // Puts the Computer.ico as the form icon
- this.Icon = new System.Drawing.Icon(Application.StartupPath + "\Computer.ico");
- // COMPUTERNAME is a global variable used to store the name
- // and IP Address of the computer to which you are currently
- // connected
- COMPUTERNAME = Computername;
- // Open connection is a user defined function responsible for
- // opening a socket connection for listener. This function
- // returns a bool value
- if( OpenConnection(COMPUTERNAME) )
- {
- // Creates an XML request with scope and mask
- CreateRequest(Request,Scope,Mask);
- // Sends this request to listener
- SendDataToListener(REQUESTFILE);
- // get the response from listener
- GetDataFromListener(RESPONSEFILE);
- // Parse that response for records and show those records
- Parsing(RESPONSEFILE);
- // Closes the active connection
- CloseConnection();
- // Assign scope to sText local variable
- string sText = Scope;
- // Extract the name of the PARENTFOLDER
- sText = sText.Substring(0, sText.LastIndexOf("\")+1);
- // changes the caption of this dialog box
- this.Text = sText + " on '" + Computername + "'";
- // Assign the value for PARENTFOLDER
- PARENTFOLDER = sText;
- // Changes the text of the status bar
- sBar.Text = sText;
- }
- }
- /// <summary>
- /// Clean up any resources being used.
- /// auto generated function by IDE
- /// </summary>
- public override void Dispose()
- {
- base.Dispose();
- components.Dispose();
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// These are also auto generated lines of code
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- this.clhType = new System.Windows.Forms.ColumnHeader();
- this.toolTipText = new System.Windows.Forms.ToolTip(this.components);
- this.btnClose = new System.Windows.Forms.Button();
- this.btnSearch = new System.Windows.Forms.Button();
- this.btnUpload = new System.Windows.Forms.Button();
- this.btnDownload = new System.Windows.Forms.Button();
- this.lvFiles = new System.Windows.Forms.ListView();
- this.clhFilename = new System.Windows.Forms.ColumnHeader();
- this.clhFileSize = new System.Windows.Forms.ColumnHeader();
- this.sBar = new System.Windows.Forms.StatusBar();
- this.FileOpenDialog = new System.Windows.Forms.OpenFileDialog();
- this.FileSaveDialog = new System.Windows.Forms.SaveFileDialog();
- this.lblCopyright = new System.Windows.Forms.Label();
- this.SuspendLayout();
- //
- // clhType
- //
- this.clhType.Text = "Type";
- this.clhType.Width = 108;
- //
- // btnClose
- //
- this.btnClose.BackColor = System.Drawing.Color.Chocolate;
- this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
- this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
- this.btnClose.ForeColor = System.Drawing.Color.White;
- this.btnClose.Location = new System.Drawing.Point(217, 5);
- this.btnClose.Name = "btnClose";
- this.btnClose.Size = new System.Drawing.Size(71, 31);
- this.btnClose.TabIndex = 3;
- this.btnClose.Text = "&Close";
- this.toolTipText.SetToolTip(this.btnClose, "Exist from this window");
- this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
- //
- // btnSearch
- //
- this.btnSearch.BackColor = System.Drawing.Color.Chocolate;
- this.btnSearch.Enabled = false;
- this.btnSearch.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
- this.btnSearch.ForeColor = System.Drawing.Color.White;
- this.btnSearch.Location = new System.Drawing.Point(146, 5);
- this.btnSearch.Name = "btnSearch";
- this.btnSearch.Size = new System.Drawing.Size(71, 31);
- this.btnSearch.TabIndex = 2;
- this.btnSearch.Text = "&Search";
- this.toolTipText.SetToolTip(this.btnSearch, "Search for file(s) or folder(s)");
- this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
- //
- // btnUpload
- //
- this.btnUpload.BackColor = System.Drawing.Color.Chocolate;
- this.btnUpload.Enabled = false;
- this.btnUpload.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
- this.btnUpload.ForeColor = System.Drawing.Color.White;
- this.btnUpload.Location = new System.Drawing.Point(4, 5);
- this.btnUpload.Name = "btnUpload";
- this.btnUpload.Size = new System.Drawing.Size(71, 31);
- this.btnUpload.TabIndex = 0;
- this.btnUpload.Text = "&Upload";
- this.toolTipText.SetToolTip(this.btnUpload, "Writes the file to current location");
- this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
- //
- // btnDownload
- //
- this.btnDownload.BackColor = System.Drawing.Color.Chocolate;
- this.btnDownload.Enabled = false;
- this.btnDownload.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
- this.btnDownload.ForeColor = System.Drawing.Color.White;
- this.btnDownload.Location = new System.Drawing.Point(75, 5);
- this.btnDownload.Name = "btnDownload";
- this.btnDownload.Size = new System.Drawing.Size(71, 31);
- this.btnDownload.TabIndex = 1;
- this.btnDownload.Text = "&Download";
- this.toolTipText.SetToolTip(this.btnDownload, "Download file to this computer");
- this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click);
- //
- // lvFiles
- //
- this.lvFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
- this.clhFilename,
- this.clhFileSize,
- this.clhType});
- this.lvFiles.ForeColor = System.Drawing.SystemColors.WindowText;
- this.lvFiles.FullRowSelect = true;
- this.lvFiles.HideSelection = false;
- this.lvFiles.Location = new System.Drawing.Point(3, 41);
- this.lvFiles.MultiSelect = false;
- this.lvFiles.Name = "lvFiles";
- this.lvFiles.Size = new System.Drawing.Size(502, 211);
- this.lvFiles.TabIndex = 4;
- this.toolTipText.SetToolTip(this.lvFiles, "Double click an entry to open");
- this.lvFiles.View = System.Windows.Forms.View.Details;
- this.lvFiles.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.lvFiles_KeyPress);
- this.lvFiles.DoubleClick += new System.EventHandler(this.lvFiles_DoubleClick);
- //
- // clhFilename
- //
- this.clhFilename.Text = "File / Folder";
- this.clhFilename.Width = 303;
- //
- // clhFileSize
- //
- this.clhFileSize.Text = "Size";
- this.clhFileSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
- this.clhFileSize.Width = 69;
- //
- // sBar
- //
- this.sBar.Location = new System.Drawing.Point(0, 259);
- this.sBar.Name = "sBar";
- this.sBar.Size = new System.Drawing.Size(508, 16);
- this.sBar.TabIndex = 5;
- //
- // FileOpenDialog
- //
- this.FileOpenDialog.Filter = "*.* (All files)|";
- this.FileOpenDialog.Title = "Select a file to upload";
- //
- // lblCopyright
- //
- this.lblCopyright.Location = new System.Drawing.Point(314, 14);
- this.lblCopyright.Name = "lblCopyright";
- this.lblCopyright.Size = new System.Drawing.Size(192, 14);
- this.lblCopyright.TabIndex = 6;
- this.lblCopyright.Text = "