服务器用来接收文件,不停的监听端口,有发送文件就马上开始接收文件 服务端代码:
-
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.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"));
-
-
-
-
-
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));
-
-
-
-
string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
-
-
-
-
int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
-
-
-
-
string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
-
-
-
-
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);
-
-
- }
- }
-
-
- MyFileStream.Close();
-
- client.Close();
-
-
-
-
this.dataGridView1.Rows.Add(bagSize, clientep.Address, SendedCount, SendFileName, bagCount, bagLast);
-
-
-
-
}
-
-
-
#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)
-
{
-
-
-
- ServiceStop();
- }
-
base.WndProc(ref m);
-
}
-
#endregion
-
-
-
#region 停止服务
-
-
-
private void ServiceStop()
- {
-
try
- {
-
- }
-
catch { }
-
-
try
- {
-
- }
-
catch { }
-
}
-
-
#endregion
-
- }
- }
客户端用来发送文件,选择文件后点发送按钮发送文件 客户端代码:
-
-
-
-
-
-
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 发送端
- {
-
-
-
-
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;
-
-
-
-
private System.ComponentModel.Container components = null;
-
-
public Form1()
- {
-
-
-
- InitializeComponent();
-
-
-
-
- }
-
-
-
-
-
protected override void Dispose( bool disposing )
- {
-
if( disposing )
- {
-
if (components != null)
- {
- components.Dispose();
- }
- }
-
base.Dispose( disposing );
-
}
-
-
#region Windows 窗体设计器生成的代码
-
-
-
-
-
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();
-
-
-
-
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 = "文件信息";
-
-
-
-
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;
-
-
-
-
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;
-
-
-
-
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 = "文件大小:";
-
-
-
-
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 = "文件名:";
-
-
-
-
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);
-
-
-
-
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;
-
-
-
-
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 = "(单位:字节)";
-
-
-
-
this.openFileDialog1.Filter = "所有文件|*.*";
-
-
-
-
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 = "系统设置";
-
-
-
-
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";
-
-
-
-
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";
-
-
-
-
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 = "传输端口:";
-
-
-
-
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;
-
-
-
-
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地址:";
-
-
-
-
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 = "包的大小:";
-
-
-
-
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 单位:字节)";
-
-
-
-
this.groupBox3.Controls.Add(this.textBox8);
-
|
请发表评论