• 大小: 94KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签:

资源简介

C#实现局域网文件传送 服务器端和客户端

资源截图

代码片段和文件信息

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
{
        //IPEndPoint ipep;
        //Socket client;
        //IPEndPoint clientep;
public Form1()
{
InitializeComponent();
dataGridView1.AllowUserToAddRows = false;
}
private void StartReceive()
{
            string dp = textBox1.Text;
           
//创建一个网络端点
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any int.Parse(dp));//*************************************



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

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

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

            //确认连接
            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);
            MessageBox.Show(SendFileName+“已经接受!“);
//已发送包的个数
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();
//填加到dgv里
//文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小
this.dataGridView1.Rows.Add(bagSize clientep.Address SendedCount SendFileName bagCount bagLast);
           
}
        /// 
        /// 关闭
        /// 

        /// 
        /// 
        private void button1_Click(object sender EventArgs e)
        {
            Application.Exit(); 
        }
        /// 
        /// 加载
        /// 

        /// 
        /// 
        private void Form1_Load(object sen

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2011-03-28 01:16  文件传送\
     目录           0  2011-03-28 01:16  文件传送\TestSocketServerHSTF\
     目录           0  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\
     文件         949  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF.sln
     文件       16384  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF.suo
     目录           0  2011-03-28 01:16  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\bin\
     目录           0  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\bin\Debug\
     文件       24576  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\bin\Debug\TestSocketServerHSTF.exe
     文件       26112  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\bin\Debug\TestSocketServerHSTF.pdb
     文件        5632  2005-11-11 22:25  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\bin\Debug\TestSocketServerHSTF.vshost.exe
     文件        3334  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Form1.cs
     文件        6947  2011-03-28 11:55  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Form1.Designer.cs
     文件        7478  2011-03-28 11:55  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Form1.resx
     目录           0  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\
     目录           0  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\
     目录           0  2008-10-23 16:48  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TempPE\
     文件         842  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TestSocketServerHSTF.csproj.GenerateResource.Cache
     文件       24576  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TestSocketServerHSTF.exe
     文件         180  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TestSocketServerHSTF.Form1.resources
     文件       26112  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TestSocketServerHSTF.pdb
     文件         180  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\Debug\TestSocketServerHSTF.Properties.Resources.resources
     文件         359  2008-10-23 16:42  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\TestSocketServerHSTF.csproj.FileList.txt
     文件         999  2011-03-28 11:57  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\obj\TestSocketServerHSTF.csproj.FileListAbsolute.txt
     文件         401  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Program.cs
     目录           0  2011-03-28 01:16  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\
     文件        1188  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\AssemblyInfo.cs
     文件        2547  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\Resources.Designer.cs
     文件        5612  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\Resources.resx
     文件        1026  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\Settings.Designer.cs
     文件         249  2008-10-23 15:10  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\Properties\Settings.settings
     文件        3291  2008-10-23 15:20  文件传送\TestSocketServerHSTF\TestSocketServerHSTF\TestSocketServerHSTF.csproj
............此处省略39个文件信息

评论

共有 条评论

相关资源