资源简介

拿过来就可用的Sockets通信框架

资源截图

代码片段和文件信息

//Send Large File By .NET Socket Demo
//Coded By Xiangxiang xxqq0824@gmail.com
//any questionu can contact me!
//www.artmtech.com
//sock.SendFile method is not supported with large file.
//before use this demoplease replace your own test file name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace FileSockClient
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 8080);
            Socket sock = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            sock.Connect(ip);
            FileStream fs = new FileStream(@“D:\\SQLServer2005SP2-KB921896-x86-CHS.exe“ FileMode.Open FileAccess.Read FileShare.Read);

            #region 使用sock发送大文件
            byte[] buffer = new byte[1024 * 1024*10];
            int tmpcount = 0;
            long sendedcounts = 0;
            while ((tmpcount = fs.Read(buffer 0 1024 * 1024*10)) > 0)
            {
                if (tmpcount != 1024 * 1024 * 10)
                {
                    byte[] endbuffer = new byte[tmpcount];
                    Array.Copy(bufferendbuffertmpcount);
                    sock.Send(endbuffer);
                    break;
                }
                sock.Send(buffer);
                sendedcounts = sendedcounts + tmpcount;
                Console.WriteLine(“发送文件字节数:“ + (sendedcounts/1024).ToString() + “ 已经发送“ + (((double)sendedcounts / ((double)fs.Length/100)).ToString(“0.00“)) + “%“);
                
            }
            sock.Close();
            Console.WriteLine(“文件已经发送完毕“);
            #endregion

            #region 不支持大文件发送
            //byte[] bytes = new byte[fs.Length];
            //fs.Read(bytes 0 (int)fs.Length);
            //sock.Send(bytes);
            //sock.SendTimeout = 100000;
            //sock.SendBufferSize = 1024 * 1024;
            //sock.SendFile(@“D:\\甜蜜蜜22.rmvb“ null null TransmitFileOptions.WriteBehind);
            #endregion
            sock.Close();
            Console.ReadLine();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6144  2008-04-11 16:19  SendFileSockServer\bin\Debug\SendFileSockServer.exe

     文件      15872  2008-04-11 16:19  SendFileSockServer\bin\Debug\SendFileSockServer.pdb

     文件      14328  2008-04-11 16:09  SendFileSockServer\bin\Debug\SendFileSockServer.vshost.exe

     文件        490  2007-07-21 01:33  SendFileSockServer\bin\Debug\SendFileSockServer.vshost.exe.manifest

     文件        728  2008-04-11 16:09  SendFileSockServer\obj\Debug\SendFileSockServer.csproj.FileListAbsolute.txt

     文件       6144  2008-04-11 16:19  SendFileSockServer\obj\Debug\SendFileSockServer.exe

     文件      15872  2008-04-11 16:19  SendFileSockServer\obj\Debug\SendFileSockServer.pdb

     文件       3473  2008-04-11 16:19  SendFileSockServer\Program.cs

     文件       1378  2008-04-10 10:36  SendFileSockServer\Properties\AssemblyInfo.cs

     文件       2509  2008-04-10 10:58  SendFileSockServer\SendFileSockServer.csproj

     文件       5632  2008-04-11 16:19  FileSockClient\bin\Debug\FileSockClient.exe

     文件      13824  2008-04-11 16:19  FileSockClient\bin\Debug\FileSockClient.pdb

     文件      14328  2008-04-11 16:19  FileSockClient\bin\Debug\FileSockClient.vshost.exe

     文件        490  2007-07-21 01:33  FileSockClient\bin\Debug\FileSockClient.vshost.exe.manifest

     文件       2501  2008-04-10 10:58  FileSockClient\FileSockClient.csproj

     文件        692  2008-04-11 16:19  FileSockClient\obj\Debug\FileSockClient.csproj.FileListAbsolute.txt

     文件       5632  2008-04-11 16:19  FileSockClient\obj\Debug\FileSockClient.exe

     文件      13824  2008-04-11 16:19  FileSockClient\obj\Debug\FileSockClient.pdb

     文件       2371  2008-04-11 16:19  FileSockClient\Program.cs

     文件       1370  2008-04-10 10:29  FileSockClient\Properties\AssemblyInfo.cs

     目录          0  2008-04-10 10:36  SendFileSockServer\obj\Debug\TempPE

     目录          0  2008-04-10 10:29  FileSockClient\obj\Debug\TempPE

     目录          0  2008-04-10 11:03  SendFileSockServer\bin\Debug

     目录          0  2008-04-11 16:19  SendFileSockServer\obj\Debug

     目录          0  2008-04-10 15:17  FileSockClient\bin\Debug

     目录          0  2008-04-11 16:19  FileSockClient\obj\Debug

     目录          0  2008-04-10 10:36  SendFileSockServer\bin

     目录          0  2008-04-10 10:36  SendFileSockServer\obj

     目录          0  2008-04-10 10:36  SendFileSockServer\Properties

     目录          0  2008-04-10 10:29  FileSockClient\bin

............此处省略7个文件信息

评论

共有 条评论