Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:4k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.IO;
  10. namespace Wrox.ProCSharp.InternetAccess.TcpSend
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Button button1;
  18. private System.Windows.Forms.Label label1;
  19. private System.Windows.Forms.Label label2;
  20. private System.Windows.Forms.TextBox txtHost;
  21. private System.Windows.Forms.TextBox txtPort;
  22. /// <summary>
  23. /// Required designer variable.
  24. /// </summary>
  25. private System.ComponentModel.Container components = null;
  26. public Form1()
  27. {
  28. //
  29. // Required for Windows Form Designer support
  30. //
  31. InitializeComponent();
  32. //
  33. // TODO: Add any constructor code after InitializeComponent call
  34. //
  35. }
  36. /// <summary>
  37. /// Clean up any resources being used.
  38. /// </summary>
  39. protected override void Dispose( bool disposing )
  40. {
  41. if( disposing )
  42. {
  43. if (components != null) 
  44. {
  45. components.Dispose();
  46. }
  47. }
  48. base.Dispose( disposing );
  49. }
  50. #region Windows Form Designer generated code
  51. /// <summary>
  52. /// Required method for Designer support - do not modify
  53. /// the contents of this method with the code editor.
  54. /// </summary>
  55. private void InitializeComponent()
  56. {
  57. this.txtHost = new System.Windows.Forms.TextBox();
  58. this.txtPort = new System.Windows.Forms.TextBox();
  59. this.button1 = new System.Windows.Forms.Button();
  60. this.label1 = new System.Windows.Forms.Label();
  61. this.label2 = new System.Windows.Forms.Label();
  62. this.SuspendLayout();
  63. // 
  64. // txtHost
  65. // 
  66. this.txtHost.Location = new System.Drawing.Point(8, 32);
  67. this.txtHost.Name = "txtHost";
  68. this.txtHost.Size = new System.Drawing.Size(104, 20);
  69. this.txtHost.TabIndex = 0;
  70. this.txtHost.Text = "localhost";
  71. // 
  72. // txtPort
  73. // 
  74. this.txtPort.Location = new System.Drawing.Point(136, 32);
  75. this.txtPort.Name = "txtPort";
  76. this.txtPort.Size = new System.Drawing.Size(40, 20);
  77. this.txtPort.TabIndex = 1;
  78. this.txtPort.Text = "2112";
  79. // 
  80. // button1
  81. // 
  82. this.button1.Location = new System.Drawing.Point(56, 64);
  83. this.button1.Name = "button1";
  84. this.button1.Size = new System.Drawing.Size(88, 32);
  85. this.button1.TabIndex = 2;
  86. this.button1.Text = "Send File";
  87. this.button1.Click += new System.EventHandler(this.button1_Click);
  88. // 
  89. // label1
  90. // 
  91. this.label1.Location = new System.Drawing.Point(136, 8);
  92. this.label1.Name = "label1";
  93. this.label1.TabIndex = 3;
  94. this.label1.Text = "Port";
  95. // 
  96. // label2
  97. // 
  98. this.label2.Location = new System.Drawing.Point(8, 8);
  99. this.label2.Name = "label2";
  100. this.label2.TabIndex = 4;
  101. this.label2.Text = "Hostname";
  102. // 
  103. // Form1
  104. // 
  105. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  106. this.ClientSize = new System.Drawing.Size(200, 109);
  107. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  108.   this.label2,
  109.   this.label1,
  110.   this.button1,
  111.   this.txtPort,
  112.   this.txtHost});
  113. this.Name = "Form1";
  114. this.Text = "TcpSend";
  115. this.ResumeLayout(false);
  116. }
  117. #endregion
  118. /// <summary>
  119. /// The main entry point for the application.
  120. /// </summary>
  121. [STAThread]
  122. static void Main() 
  123. {
  124. Application.Run(new Form1());
  125. }
  126. private void button1_Click(object sender, System.EventArgs e)
  127. {
  128. TcpClient tcpClient = new TcpClient(txtHost.Text, Int32.Parse(txtPort.Text));
  129. NetworkStream ns = tcpClient.GetStream();
  130. FileStream fs = File.Open("..\..\form1.cs", FileMode.Open);
  131.     
  132. int data = fs.ReadByte();
  133. while(data != -1)
  134. {
  135. ns.WriteByte((byte)data);
  136. data = fs.ReadByte();
  137. }
  138. fs.Close();    
  139. ns.Close();
  140. tcpClient.Close();
  141. }
  142. }
  143. }