FileSendClient :

Form1.cs

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms; namespace FileSendClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FileDialog fDg = new OpenFileDialog();
if (fDg.ShowDialog() == DialogResult.OK)
{
FTClientCode.SendFile(fDg.FileName);
}
} private void timer1_Tick(object sender, EventArgs e)
{
label3.Text = FTClientCode.curMsg;
}
} //FILE TRANSFER USING C#.NET SOCKET - CLIENT
class FTClientCode
{
public static string curMsg = "Idle";
public static void SendFile(string fileName)
{
try
{
// IPAddress ipAddress = Dns.GetHostEntry("109.52.62.59").AddressList[0]; IPAddress ipAddress2 = Dns.GetHostAddresses("109.52.62.59")[]; IPEndPoint ipEnd = new IPEndPoint(ipAddress2, );
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); string filePath = ""; fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -)
{
filePath += fileName.Substring(, fileName.IndexOf("/") + );
fileName = fileName.Substring(fileName.IndexOf("/") + );
} byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > * )
{
curMsg = "File size is more than 850kb, please try with small file.";
return;
} curMsg = "Buffering ...";
byte[] fileData = File.ReadAllBytes(filePath + fileName);
byte[] clientData = new byte[ + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length); fileNameLen.CopyTo(clientData, );
fileNameByte.CopyTo(clientData, );
fileData.CopyTo(clientData, + fileNameByte.Length); curMsg = "Connection to server ...";
clientSock.Connect(ipEnd); curMsg = "File sending...";
clientSock.Send(clientData); curMsg = "Disconnecting...";
clientSock.Close();
curMsg = "File transferred."; }
catch (Exception ex)
{
if (ex.Message == "No connection could be made because the target machine actively refused it")
curMsg = "File Sending fail. Because server not running.";
else
curMsg = "File Sending fail." + ex.Message;
} } public static IPAddress GetLocalIP()
{
IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress ipAddress in addressList)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
return ipAddress;
}
return addressList[];
}
}
}

Form1.Designer.cs

namespace FileSendClient
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "Select File to Send";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.label1.ForeColor = System.Drawing.Color.Green;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "That program can transfer small file. I\'ve test up to 1.5MB file";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "Present Status:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "Idle";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = ;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Timer timer1;
}
}

FileSendServer

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms; namespace FileSendServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FTServerCode.receivedPath = "";
} private void button1_Click(object sender, EventArgs e)
{
if (FTServerCode.receivedPath.Length > )
backgroundWorker1.RunWorkerAsync();
else
MessageBox.Show("Please select file receiving path");
} private void timer1_Tick(object sender, EventArgs e)
{
label5.Text = FTServerCode.receivedPath;
label3.Text = FTServerCode.curMsg;
} FTServerCode obj = new FTServerCode();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{ obj.StartServer();
} private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
FTServerCode.receivedPath = fd.SelectedPath;
}
}
}
//FILE TRANSFER USING C#.NET SOCKET - SERVER
class FTServerCode
{
IPEndPoint ipEnd;
Socket sock;
public FTServerCode()
{
ipEnd = new IPEndPoint(IPAddress.Any, );
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
}
public static string receivedPath;
public static string curMsg = "Stopped";
public void StartServer()
{
try
{
curMsg = "Starting...";
sock.Listen(); curMsg = "Running and waiting to receive file.";
Socket clientSock = sock.Accept(); byte[] clientData = new byte[ * ]; int receivedBytesLen = clientSock.Receive(clientData);
curMsg = "Receiving data..."; int fileNameLen = BitConverter.ToInt32(clientData, );
string fileName = Encoding.ASCII.GetString(clientData, , fileNameLen); BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + "/" + fileName, FileMode.Append)); ;
bWrite.Write(clientData, + fileNameLen, receivedBytesLen - - fileNameLen); curMsg = "Saving file..."; bWrite.Close();
clientSock.Close();
curMsg = "Reeived & Saved file; Server Stopped.";
}
catch (Exception ex)
{
curMsg = "File Receving error.";
}
}
}
}

Form1.Designer.cs

namespace FileSendServer
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "Start Server";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "Server Status:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "Stopped";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.label4.ForeColor = System.Drawing.Color.ForestGreen;
this.label4.Location = new System.Drawing.Point(, );
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(, );
this.label4.TabIndex = ;
this.label4.Text = "That program can transfer small file. I\'ve test up to 1.5MB file";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = ;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// backgroundWorker1
//
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
//
// button2
//
this.button2.Location = new System.Drawing.Point(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
this.button2.Text = "Select Receiving Path";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(, );
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(, );
this.label5.TabIndex = ;
this.label5.Text = " ";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(, );
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(, );
this.label6.TabIndex = ;
this.label6.Text = "File receiving path:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label5);
this.Controls.Add(this.label6);
this.Controls.Add(this.button2);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
}
}

最新文章

  1. 【python】安装指定模块
  2. IOS手动添加CoreData
  3. 有关嵌入式linux的注意点总结
  4. SQL手工注入
  5. 如何使用axis2 构建 Android 服务器后端--- 工具准备与环境配置
  6. .NET性能优化方面的总结
  7. Spring 入门 Ioc-Annotation
  8. thinkpad x230i U盘启动
  9. 十、Hadoop学习笔记————Hive与Hbase以及RDBMS(关系型数据库)的关系
  10. POJ [P2289] Jamie&#39;s Contact Groups
  11. 2018-2019-2 网络对抗技术 20165232 Exp3 免杀原理与实践
  12. 使用sphinx制作接口文档并托管到readthedocs
  13. Windows下python安装运行
  14. swift hidesBottomBarWhenPushed 设置界面
  15. ABP框架系列之二十七:(Feature-Management-特征管理)
  16. [MapReduce_add_3] MapReduce 通过分区解决数据倾斜
  17. oracle 11gR2 ASM添加和删除磁盘
  18. HDU 1846 Brave Game (巴什博弈)
  19. java 如何对由json对象构成的数组形式的字符串进行遍历?
  20. java是如何编码解码的

热门文章

  1. 分析技术在PMP中的应用
  2. HTML5标签canvas制作平面图
  3. spring boot工程打成JAR包到服务器上运行
  4. Batch Normalization 学习笔记
  5. 一个非常好的C#字符串操作处理类StringHelper.cs
  6. Android基础部分再学习---activity的状态保存
  7. Cordova 问题点备忘
  8. python中常用的知识
  9. 国内CDN加速现状
  10. Streaming 101