`
qq173684423
  • 浏览: 28081 次
社区版块
存档分类
最新评论

c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口

阅读更多
服务器用来接收文件,不停的监听端口,有发送文件就马上开始接收文件
服务端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Net;
using System.Threading;
using System.Net.Sockets;

using System.IO;

namespace TestSocketServerHSTF
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();


			//不显示出dataGridView1的最后一行空白
			dataGridView1.AllowUserToAddRows = false;
		}


		#region 定义变量


		#endregion



		#region 进入窗体即启动服务

		private void Form1_Load(object sender, EventArgs e)
		{
			//开启接收线程
			Thread TempThread = new Thread(new ThreadStart(this.StartReceive));
			TempThread.Start();
		}

		
		#endregion



		#region 功能函数

		private void StartReceive()
		{
			//创建一个网络端点
			IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse("2005"));

			//MessageBox.Show(IPAddress.Any);

			//创建一个套接字
			Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

			//绑定套接字到端口
			server.Bind(ipep);

			//开始侦听(并堵塞该线程)
			server.Listen(10);

			//确认连接
			Socket client = server.Accept();

			//获得客户端节点对象
			IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
			


			//获得[文件名]
			string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
			//MessageBox.Show("文件名" + SendFileName);

			//获得[包的大小]
			string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
			//MessageBox.Show("包大小" + bagSize);

			//获得[包的总数量]
			int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
			//MessageBox.Show("包的总数量" + bagCount);

			//获得[最后一个包的大小]
			string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
			//MessageBox.Show("最后一个包的大小" + bagLast);

			//创建一个新文件
			FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write);

			//已发送包的个数
			int SendedCount = 0;

			while (true)
			{
				byte[] data = TransferFiles.ReceiveVarData(client);
				if (data.Length == 0)
				{
					break;
				}
				else
				{
					SendedCount++;
					//将接收到的数据包写入到文件流对象
					MyFileStream.Write(data, 0, data.Length);
					//显示已发送包的个数
					//MessageBox.Show("已发送包个数"+SendedCount.ToString());
				}
			}

			//关闭文件流
			MyFileStream.Close();
			//关闭套接字
			client.Close();

			//填加到dgv里
			//文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小
			this.dataGridView1.Rows.Add(bagSize, clientep.Address, SendedCount, SendFileName, bagCount, bagLast);

			//MessageBox.Show("文件接收完毕!");

		}


		#endregion



		#region   拦截Windows消息,关闭窗体时执行
		protected override void WndProc(ref   Message m)
		{
			const int WM_SYSCOMMAND = 0x0112;
			const int SC_CLOSE = 0xF060;
			if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
			{//捕捉关闭窗体消息   
				//   User   clicked   close   button   
				//this.WindowState = FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化

				ServiceStop();
			}
			base.WndProc(ref   m);
		}
		#endregion


		#region 停止服务

		//停止服务
		private void ServiceStop()
		{
			try
			{

			}
			catch { }

			try
			{

			}
			catch { }
		}

		#endregion

	}
}

客户端用来发送文件,选择文件后点发送按钮发送文件
客户端代码:
////////////////////////////////////////////////////////////////////////////////
//title: 点对点文件传输程序                                           //
////////////////////////////////////////////////////////////////////////////////

//////////////////////////Begin-发送端//////////////////////////////////
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;
using System.Threading;

namespace 发送端
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.TextBox textBox7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox textBox8;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox textBox9;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox textBox10;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.textBox8 = new System.Windows.Forms.TextBox();
			this.textBox9 = new System.Windows.Forms.TextBox();
			this.textBox7 = new System.Windows.Forms.TextBox();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.textBox10 = new System.Windows.Forms.TextBox();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.button2 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.textBox3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.button1);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(416, 96);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "文件信息";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(80, 40);
			this.textBox2.Name = "textBox2";
			this.textBox2.ReadOnly = true;
			this.textBox2.Size = new System.Drawing.Size(232, 21);
			this.textBox2.TabIndex = 3;
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(80, 64);
			this.textBox3.Name = "textBox3";
			this.textBox3.ReadOnly = true;
			this.textBox3.Size = new System.Drawing.Size(136, 21);
			this.textBox3.TabIndex = 3;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 72);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "文件大小:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "文件名:";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(320, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(88, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = "浏览";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(304, 21);
			this.textBox1.TabIndex = 0;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(224, 72);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(96, 16);
			this.label6.TabIndex = 2;
			this.label6.Text = "(单位:字节)";
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Filter = "所有文件|*.*";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.textBox6);
			this.groupBox2.Controls.Add(this.textBox5);
			this.groupBox2.Controls.Add(this.label4);
			this.groupBox2.Controls.Add(this.textBox4);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Controls.Add(this.label5);
			this.groupBox2.Controls.Add(this.label9);
			this.groupBox2.Location = new System.Drawing.Point(0, 96);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(416, 72);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "系统设置";
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(96, 40);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(72, 21);
			this.textBox6.TabIndex = 3;
			this.textBox6.Text = "50000";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(320, 16);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(80, 21);
			this.textBox5.TabIndex = 3;
			this.textBox5.Text = "2005";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(256, 24);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(100, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "传输端口:";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(96, 16);
			this.textBox4.Name = "textBox4";
			this.textBox4.ReadOnly = true;
			this.textBox4.Size = new System.Drawing.Size(144, 21);
			this.textBox4.TabIndex = 1;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "本机IP地址:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(24, 48);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(88, 16);
			this.label5.TabIndex = 2;
			this.label5.Text = "包的大小:";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(176, 48);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(224, 16);
			this.label9.TabIndex = 2;
			this.label9.Text = "(范围:10000 - 60000 单位:字节)";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.textBox8);
			this.groupBox3.Controls.Add(this.textBox9);
			this.groupBox3.Controls.Add(this.textBox7);
			this.groupBox3.Controls.Add(this.progressBar1);
			this.groupBox3.Controls.Add(this.label7);
			this.groupBox3.Controls.Add(this.label8);
			this.groupBox3.Controls.Add(this.label10);
			this.groupBox3.Controls.Add(this.label11);
			this.groupBox3.Controls.Add(this.label12);
			this.groupBox3.Controls.Add(this.textBox10);
			this.groupBox3.Location = new System.Drawing.Point(0, 168);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(416, 168);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "状态信息";
			// 
			// textBox8
			// 
			this.textBox8.Location = new System.Drawing.Point(120, 40);
			this.textBox8.Name = "textBox8";
			this.textBox8.ReadOnly = true;
			this.textBox8.Size = new System.Drawing.Size(160, 21);
			this.textBox8.TabIndex = 1;
			// 
			// textBox9
			// 
			this.textBox9.Location = new System.Drawing.Point(120, 64);
			this.textBox9.Name = "textBox9";
			this.textBox9.ReadOnly = true;
			this.textBox9.Size = new System.Drawing.Size(80, 21);
			this.textBox9.TabIndex = 1;
			// 
			// textBox7
			// 
			this.textBox7.Location = new System.Drawing.Point(120, 16);
			this.textBox7.Name = "textBox7";
			this.textBox7.ReadOnly = true;
			this.textBox7.Size = new System.Drawing.Size(160, 21);
			this.textBox7.TabIndex = 1;
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(8, 136);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(400, 23);
			this.progressBar1.Step = 1;
			this.progressBar1.TabIndex = 3;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(32, 24);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(96, 16);
			this.label7.TabIndex = 2;
			this.label7.Text = "接收端IP地址:";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(40, 48);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(80, 16);
			this.label8.TabIndex = 2;
			this.label8.Text = "包的总数量:";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 72);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(120, 16);
			this.label10.TabIndex = 2;
			this.label10.Text = "最后一个包的大小:";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(200, 72);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(96, 16);
			this.label11.TabIndex = 2;
			this.label11.Text = "(单位:字节)";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(16, 96);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(104, 16);
			this.label12.TabIndex = 2;
			this.label12.Text = "已发送包的数量:";
			// 
			// textBox10
			// 
			this.textBox10.Location = new System.Drawing.Point(120, 88);
			this.textBox10.Name = "textBox10";
			this.textBox10.ReadOnly = true;
			this.textBox10.Size = new System.Drawing.Size(80, 21);
			this.textBox10.TabIndex = 1;
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.button2);
			this.groupBox4.Location = new System.Drawing.Point(0, 336);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(416, 48);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "系统控制";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(16, 16);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 23);
			this.button2.TabIndex = 0;
			this.button2.Text = "开始发送";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(416, 389);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "点对点文件传输软体发送端";
			this.Load += new System.EventHandler(this.EzoneSend_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			this.groupBox2.ResumeLayout(false);
			this.groupBox2.PerformLayout();
			this.groupBox3.ResumeLayout(false);
			this.groupBox3.PerformLayout();
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//选择要进行传输的文件
			if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
			{
				FileInfo EzoneFile=new FileInfo(this.openFileDialog1.FileName);
				this.textBox1.Text=EzoneFile.FullName;
				this.textBox2.Text=EzoneFile.Name;
				this.textBox3.Text=EzoneFile.Length.ToString();
				
			}
		}


		private void StartSend()
		{			
			//创建一个文件对象
			FileInfo EzoneFile=new FileInfo(this.textBox1.Text);
			//打开文件流
			FileStream EzoneStream=EzoneFile.OpenRead();
			//包的大小
			int PacketSize=int.Parse(this.textBox6.Text);
			//包的数量
			int PacketCount=(int)(EzoneStream.Length/((long)PacketSize));
			this.textBox8.Text=PacketCount.ToString();
			this.progressBar1.Maximum=PacketCount;
			//最后一个包的大小
			int LastDataPacket=(int)(EzoneStream.Length-((long)(PacketSize*PacketCount)));
			this.textBox9.Text=LastDataPacket.ToString();
			
			////创建一个网络端点
			//IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));

			////MessageBox.Show(IPAddress.Any);

			////创建一个套接字
			//Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

			//MessageBox.Show(server.ToString());



			//指向远程服务端节点
			IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));
			//创建套接字
			Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			//连接到发送端
			client.Connect(ipep);





			////绑定套接字到端口
			//client.Bind(ipep);

			//MessageBox.Show(ipep.ToString());

			////开始侦听(并堵塞该线程)
			//server.Listen(10);
			//确认连接
			//Socket client = server.Accept();

			//MessageBox.Show(client.ToString());

			

			



			//获得客户端节点对象
			IPEndPoint clientep=(IPEndPoint)client.RemoteEndPoint;
			//获得客户端的IP地址
			//this.textBox7.Text=clientep.Address.ToString();
			//发送[文件名]到客户端
			CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
			//发送[包的大小]到客户端
			CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
			//发送[包的总数量]到客户端
			CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
			//发送[最后一个包的大小]到客户端
			CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));

			//数据包
			byte[] data=new byte[PacketSize];
			//开始循环发送数据包
			for(int i=0;i<PacketCount;i++)
			{
				//从文件流读取数据并填充数据包
				EzoneStream.Read(data,0,data.Length);
				//发送数据包
				CommonModule.EzoneModule.SendVarData(client,data);
				//显示发送数据包的个数
				this.textBox10.Text=((int)(i+1)).ToString();
				//进度条值的显示
				this.progressBar1.PerformStep();
			}

			//如果还有多余的数据包,则应该发送完毕!
			if(LastDataPacket!=0)
			{
				data=new byte[LastDataPacket];
				EzoneStream.Read(data,0,data.Length);
				CommonModule.EzoneModule.SendVarData(client,data);
				this.progressBar1.Value=this.progressBar1.Maximum;
			}

			//关闭套接字
			client.Close();
			//server.Close();
			//关闭文件流
			EzoneStream.Close();
			this.button2.Enabled=true;
			MessageBox.Show("文件传输完毕!");
		}


		private void button2_Click(object sender, System.EventArgs e)
		{
			//开启文件传输子线程
			Thread TempThread=new Thread(new ThreadStart(this.StartSend));
			TempThread.Start();
			this.button2.Enabled=false;
		}

		private void EzoneSend_Load(object sender, System.EventArgs e)
		{
			//获得本机的IP地址
			this.textBox4.Text=Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
		}
	}
}

公共类,服务端客户端都需要用到

////////////////////////////Begin-公共模块//////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace TestSocketServerHSTF
{
	class TransferFiles
	{
		public TransferFiles()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}



		public static int SendData(Socket s, byte[] data)
		{
			int total = 0;
			int size = data.Length;
			int dataleft = size;
			int sent;

			while (total < size)
			{
				sent = s.Send(data, total, dataleft, SocketFlags.None);
				total += sent;
				dataleft -= sent;
			}

			return total;
		}

		public static byte[] ReceiveData(Socket s, int size)
		{
			int total = 0;
			int dataleft = size;
			byte[] data = new byte[size];
			int recv;
			while (total < size)
			{
				recv = s.Receive(data, total, dataleft, SocketFlags.None);
				if (recv == 0)
				{
					data = null;
					break;
				}

				total += recv;
				dataleft -= recv;
			}
			return data;
		}

		public static int SendVarData(Socket s, byte[] data)
		{
			int total = 0;
			int size = data.Length;
			int dataleft = size;
			int sent;
			byte[] datasize = new byte[4];
			datasize = BitConverter.GetBytes(size);
			sent = s.Send(datasize);

			while (total < size)
			{
				sent = s.Send(data, total, dataleft, SocketFlags.None);
				total += sent;
				dataleft -= sent;
			}

			return total;
		}

		public static byte[] ReceiveVarData(Socket s)
		{
			int total = 0;
			int recv;
			byte[] datasize = new byte[4];
			recv = s.Receive(datasize, 0, 4, SocketFlags.None);
			int size = BitConverter.ToInt32(datasize, 0);
			int dataleft = size;
			byte[] data = new byte[size];
			while (total < size)
			{
				recv = s.Receive(data, total, dataleft, SocketFlags.None);
				if (recv == 0)
				{
					data = null;
					break;
				}
				total += recv;
				dataleft -= recv;
			}
			return data;
		}
	}
}




/////////////////////////////End-公共模块///////////////////////////////////////

黑色头发:http://heisetoufa.iteye.com
  • 大小: 27.9 KB
分享到:
评论

相关推荐

    c#winformsocket网络编程,点对点传输文件,socket文件传输,监听端口.pdf

    在C# WinForm环境中进行网络编程,特别是在实现点对点文件传输时,Socket扮演着核心角色。Socket是一种网络通信协议,允许两个网络应用程序之间进行数据交换。在这个案例中,我们关注的是通过Socket进行文件传输以及...

    C# Winform socket点对点信息传输mode

    以上就是关于"C# Winform socket点对点信息传输mode"的基本介绍。通过理解和实践这些知识点,你可以构建一个功能完备的点对点通信系统。共同学习,不断提升我们的IT技能,是我们在IT领域不断进步的关键。

    C# Winform 即时通信实例(基于Socket )

    本项目“C# Winform 即时通信实例(基于Socket)”提供了一个使用C#编程语言和Windows Forms(Winform)界面设计工具创建的简单即时通信系统。这个系统的核心是Socket编程,它是网络通信的基础,适用于构建各种网络...

    C# socket TCP 大文件传输同时实现断点续传

    在C#编程语言中,利用Socket API进行TCP通信可以构建高效、稳定的文件传输系统,特别是在处理大文件时,断点续传功能则显得尤为必要。下面,我们将深入探讨如何使用C#的Socket实现TCP大文件传输并支持断点续传。 ...

    c# WinForm 使用Socket实现简单实现服务端与客户端连接,实现消息发送,服务器客户端重连

    在C#编程环境中,WinForm应用常常用于构建用户界面,而Socket则被用来处理网络通信。本示例中,我们探讨的是如何利用C#的WinForm来创建一个基于Socket的服务端和客户端,实现两者之间的消息传递,以及在服务器断线后...

    C# WinForm 局域网文件传输

    本主题聚焦于利用C#实现WinForm界面进行局域网内的文件传输,这是一种点对点(P2P)的通信方式,使得在同一网络环境下的计算机可以直接交换数据。 一、C# WinForm基础 C# WinForm是.NET Framework提供的一种用于...

    C# winform实现一个服务端和多个客户端进行通信

    TCP(传输控制协议)和Socket是网络编程中常用的技术,用于建立可靠的、面向连接的数据传输。让我们逐一解析这个项目的关键知识点。 首先,TCP是一种面向连接的协议,它在数据传输前先建立连接,确保数据按照发送...

    c#winform端口监听和文件的传输.pdf

    整个过程就是这样,通过C#的Socket编程,我们可以构建一个简单的WinForm应用程序,实现端口监听和文件的接收。这个例子展示了基本的TCP通信流程,但实际应用可能需要更复杂的错误处理和性能优化。在开发这样的系统时...

    C#网络编程 Winform设计

    根据给定的文件信息,我们可以深入探讨C#网络编程与Winform设计的相关知识点,特别是针对仿QQ界面设计的实现。 ### C#网络编程基础 C#作为一种强大的编程语言,不仅继承了C和C++的强大功能,还融合了Java的面向...

    C#winform开发的局域网聊天程序

    这个程序的核心功能是实现不同客户端之间的信息收发,充分利用了C#的面向对象特性、Winform的用户界面设计以及Socket网络编程技术。让我们逐一解析这些关键知识点。 首先,C#是一种由微软开发的面向对象的编程语言...

    C#winform 局域网IP和端口扫描

    总的来说,"C# winform 局域网IP和端口扫描"是一个涵盖网络编程基础、C#语言特性和系统级网络API的综合项目,对于想要提升网络编程技能的开发者来说,这是一个很好的学习案例。通过实践和理解这个项目,不仅可以提升...

    10个C#Socket编程代码示例.zip

    局域网(LAN)通信是Socket编程常见的应用场景,例如文件"局域网聊天小工具源码.rar_4251917.rar"、"c#局域网聊天工具制作过程讲解与原代码讲解_4251337.doc"和"[推荐源码]C++飞鸽传书服务器客户端源码及传输协议...

    C# socket winform 同步和异步通信 点对点通信 聊天 互相学习

    C# socket winform 同步和异步通信。 只供个人学习。 请不要发布到网上,纯手工制作。 同步:socket通信,客户端是WinCe项目,Windows上也可以运行,需要打开exe.不能在解决方案中调试。 异步:公共通信类 ...

    C#网络聊天程序 socket编程

    《C#网络聊天程序:基于Socket编程的即时通讯实现》 C#网络聊天程序的构建是计算机科学领域中的一项重要实践,它涉及到网络通信、数据存储以及用户界面等多个方面的技术。本程序使用了Socket编程来实现客户端与...

    C#_Winform源码Socket客户端

    通过理解以上知识点,你可以分析并学习`SocketClient(C#作Socket客户端程序源码)`提供的示例代码,从而提升自己的C#网络编程能力。这个示例代码不仅展示了如何创建一个简单的Socket客户端,还涵盖了客户端应用设计的...

    C#异步聊天(socket)网络编程.rar

    总的来说,"C#异步聊天(socket)网络编程.rar"项目涵盖了C#网络编程的核心知识点,包括异步Socket通信、TCP连接、Winform界面设计以及线程管理和错误处理等。通过学习这个项目,开发者能够掌握构建高效、稳定的网络...

    C# Winform简易聊天(Chat Box)

    5. **事件驱动编程**:C# Winform是事件驱动的,我们需要监听用户的键盘输入(如回车键按下)和网络事件(如数据接收)。例如,当用户点击发送按钮时,触发发送事件;当服务端接收到新消息时,触发更新UI的事件。 6...

    局域网点对点文件传输

    在点对点文件传输中,通常需要创建两个Socket,一个用于监听客户端的连接请求(服务器端),另一个用于向服务器发送数据(客户端)。 Winform是C#中的一个用户界面框架,它允许开发者创建桌面应用程序,并提供了...

    C#通过Socket类远程登录

    在IT行业中,网络通信是至关重要的一个领域,而C#作为一种强大的编程语言,提供了丰富的类库来支持网络编程。在这个场景中,我们将深入探讨如何利用C#中的Socket类实现远程登录功能,特别是针对路由器这样的设备进行...

Global site tag (gtag.js) - Google Analytics