frmfilereceive.cs

来自「Winform_OnlineTraning 我们培训机构的在线培训部分」· CS 代码 · 共 164 行

CS
164
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace Example_3
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmFileReceive : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnReadFile;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Label lblFileData;
		private System.Windows.Forms.TextBox txtFileData;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmFileReceive()
		{
			//
			// 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.btnReadFile = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.lblFileData = new System.Windows.Forms.Label();
			this.txtFileData = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// btnReadFile
			// 
			this.btnReadFile.Location = new System.Drawing.Point(298, 9);
			this.btnReadFile.Name = "btnReadFile";
			this.btnReadFile.Size = new System.Drawing.Size(90, 24);
			this.btnReadFile.TabIndex = 0;
			this.btnReadFile.Text = "读取文件(&R)";
			this.btnReadFile.Click += new System.EventHandler(this.btnReadFile_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(298, 215);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(90, 25);
			this.btnExit.TabIndex = 0;
			this.btnExit.Text = "退出(&E)";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// lblFileData
			// 
			this.lblFileData.Location = new System.Drawing.Point(19, 17);
			this.lblFileData.Name = "lblFileData";
			this.lblFileData.Size = new System.Drawing.Size(120, 17);
			this.lblFileData.TabIndex = 2;
			this.lblFileData.Text = "文件数据:";
			// 
			// txtFileData
			// 
			this.txtFileData.Location = new System.Drawing.Point(19, 43);
			this.txtFileData.Multiline = true;
			this.txtFileData.Name = "txtFileData";
			this.txtFileData.ReadOnly = true;
			this.txtFileData.Size = new System.Drawing.Size(365, 164);
			this.txtFileData.TabIndex = 3;
			this.txtFileData.Text = "";
			// 
			// frmFileReceive
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(393, 246);
			this.Controls.Add(this.txtFileData);
			this.Controls.Add(this.lblFileData);
			this.Controls.Add(this.btnReadFile);
			this.Controls.Add(this.btnExit);
			this.MaximizeBox = false;
			this.Name = "frmFileReceive";
			this.Text = "读取文件";
			this.ResumeLayout(false);

		}
		#endregion

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

		private void btnReadFile_Click(object sender, System.EventArgs e)
		{
			//Specifying the IPaddress of the machine
			IPAddress objIPAddress = IPAddress.Parse("192.168.2.77");
			//Creating a new Listener
			TcpListener objTcpListener = new TcpListener(objIPAddress,8221);
			//Starting the Listener object
			objTcpListener.Start();
			
			//Accepting the Client Request
			TcpClient objTcpClient = objTcpListener.AcceptTcpClient();

			//getting the stream
			NetworkStream objNetworkStream = objTcpClient.GetStream();
	
			StreamReader objStreamReader = new StreamReader(objNetworkStream);

			//Reading the stream
			string result = objStreamReader.ReadToEnd();
			
			//Displaying the content into the text box
			this.txtFileData.Text = result.ToString();

			//Closing the connections
			objTcpClient.Close();
			objTcpListener.Stop();

		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?