📄 frmserverfilesend.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Example_2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmServerFileSend : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblPort;
private System.Windows.Forms.Label lblHost;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.TextBox txtHostID;
private System.Windows.Forms.Label lblFile;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.OpenFileDialog ofdFileDialog;
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.Button btnSelectFile;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmServerFileSend()
{
//
// 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.btnSelectFile = new System.Windows.Forms.Button();
this.lblPort = new System.Windows.Forms.Label();
this.lblHost = new System.Windows.Forms.Label();
this.txtFileName = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.txtHostID = new System.Windows.Forms.TextBox();
this.lblFile = new System.Windows.Forms.Label();
this.btnConnect = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.ofdFileDialog = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// btnSelectFile
//
this.btnSelectFile.Location = new System.Drawing.Point(294, 52);
this.btnSelectFile.Name = "btnSelectFile";
this.btnSelectFile.Size = new System.Drawing.Size(90, 25);
this.btnSelectFile.TabIndex = 0;
this.btnSelectFile.Text = "选择文件(&S)";
this.btnSelectFile.Click += new System.EventHandler(this.btnSelectFile_Click);
//
// lblPort
//
this.lblPort.Location = new System.Drawing.Point(216, 16);
this.lblPort.Name = "lblPort";
this.lblPort.Size = new System.Drawing.Size(48, 16);
this.lblPort.TabIndex = 1;
this.lblPort.Text = "端口:";
this.lblPort.Click += new System.EventHandler(this.label1_Click);
//
// lblHost
//
this.lblHost.Location = new System.Drawing.Point(8, 17);
this.lblHost.Name = "lblHost";
this.lblHost.Size = new System.Drawing.Size(77, 15);
this.lblHost.TabIndex = 2;
this.lblHost.Text = "主机 ID:";
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(80, 56);
this.txtFileName.Name = "txtFileName";
this.txtFileName.Size = new System.Drawing.Size(200, 21);
this.txtFileName.TabIndex = 3;
this.txtFileName.Text = "";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(264, 16);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(120, 21);
this.txtPort.TabIndex = 4;
this.txtPort.Text = "";
//
// txtHostID
//
this.txtHostID.Location = new System.Drawing.Point(80, 16);
this.txtHostID.Name = "txtHostID";
this.txtHostID.Size = new System.Drawing.Size(120, 21);
this.txtHostID.TabIndex = 5;
this.txtHostID.Text = "";
//
// lblFile
//
this.lblFile.Location = new System.Drawing.Point(8, 56);
this.lblFile.Name = "lblFile";
this.lblFile.Size = new System.Drawing.Size(69, 16);
this.lblFile.TabIndex = 6;
this.lblFile.Text = "选择文件:";
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(192, 96);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(88, 25);
this.btnConnect.TabIndex = 7;
this.btnConnect.Text = "连接(&C)";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(304, 96);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(80, 25);
this.btnExit.TabIndex = 8;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// frmServerFileSend
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 133);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.lblFile);
this.Controls.Add(this.txtHostID);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.lblHost);
this.Controls.Add(this.lblPort);
this.Controls.Add(this.btnSelectFile);
this.MaximizeBox = false;
this.Name = "frmServerFileSend";
this.Text = "发送文件到客户端的服务器程序";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmServerFileSend());
}
private void label1_Click(object sender, System.EventArgs e)
{
}
private void btnSelectFile_Click(object sender, System.EventArgs e)
{
if(this.ofdFileDialog.ShowDialog() == DialogResult.OK)
{
this.txtFileName.Text = ofdFileDialog.FileName.ToString();
}
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
//TcpClient object specifying the Host name and Port
TcpClient objTcpClient = new TcpClient(txtHostID.Text, Int32.Parse(this.txtPort.Text));
//Creating a new stream for the TcpClient object
NetworkStream objNetworkStream = objTcpClient.GetStream();
//Adding the file in open mode
FileStream objFileStream = File.Open(this.txtFileName.Text,FileMode.Open);
//Reading the bytes
int data =objFileStream.ReadByte();
while(data != -1)
{
//Writing bytes to the stream
objNetworkStream.WriteByte((byte)data);
data = objFileStream.ReadByte();
}
//Closing all open streams
objFileStream.Close();
objNetworkStream.Close();
objTcpClient.Close();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -