多线程写文件

原创 2012年03月30日 18:03:24

创建命令实体类:Command

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

namespace EMNewsInfo
{
    /// <summary>
    /// 命令实体
    /// </summary>
    public class Command : ICloneable
    {
        public const string COPY = "copy";
        public const string DELETE = "delete";
        public const string RENAME = "rename";

        /// <summary>
        /// 命令编号
        /// </summary>
        public string Id
        {
            get;
            set;
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CTime
        {
            get;
            set;
        }

        /// <summary>
        /// 本地路径
        /// </summary>
        public string LocalPath
        {
            get;
            set;
        }

        /// <summary>
        /// 本地Ip
        /// </summary>
        public string ServerIp
        {
            get;
            set;
        }

        public int Port
        {
            get;
            set;
        }

        public byte[] Data
        {
            get;
            set;
        }

        /// <summary>
        /// 服务器路径
        /// </summary>
        public string ServerPath
        {
            get;
            set;
        }

        /// <summary>
        /// 总字节数
        /// </summary>
        public long Count
        {
            get;
            set;
        }

        /// <summary>
        /// 命令类型
        /// </summary>
        public string Type
        {
            get;
            set;
        }

        /// <summary>
        /// 状态 
        /// 0.未处理
        /// 1.已处理
        /// </summary>
        public int State
        {
            get;
            set;
        }

        #region ICloneable 成员

        public object Clone()
        {
            var t = new Command();
            t.Count = this.Count;
            t.CTime = this.CTime;
            t.Id = this.Id;
            t.LocalPath = this.LocalPath;
            t.ServerIp = this.ServerIp;
            t.ServerPath = this.ServerPath;
            t.Type = this.Type;
            return t;
        }

        #endregion
    }
}

创建连接池相关类:ConnectionPool

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

namespace EMNewsInfo
{
    public class ConnectionPool
    {
        private static System.Timers.Timer timer = new System.Timers.Timer();
        static ConnectionPool()
        {
            timer.Interval = Keep_Alive * 1000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(ClearOldCon);
            timer.Start();
        }

        private static void ClearOldCon(object sender, EventArgs e)
        {
            lock (dicCon)
            {
                foreach (var key in dicCon.Keys)
                {
                    var temp = dicCon[key];
                    for (int i = temp.Lst.Count - 1; i >= 0; i--)
                    {
                        var c = temp.Lst[i];
                        if (c.State == State.free && c.UpdateTime < DateTime.Now.AddSeconds(-Keep_Alive))
                        {
                            try
                            {
                                temp.Lst[i].CloseSocket();
                            }
                            catch
                            {
                            }
                            temp.Lst.RemoveAt(i);
                        }
                    }
                }
            }
        }

        private static int Keep_Alive = 30;

        private static Dictionary<string, ConnectionCollection> dicCon = new Dictionary<string, ConnectionCollection>();

        public static Dictionary<string, ConnectionCollection> DicCon
        {
            get
            {
                return dicCon;
            }
        }


        private static Mutex m_mutex = new Mutex();

        public static Connection GetConnection(string ip, int port)
        {
            try
            {
                m_mutex.WaitOne();
                lock (dicCon)
                {
                    string key = ip + "_" + port;
                    Connection con = null;
                    if (dicCon.ContainsKey(key))
                    {
                        var conCollection = dicCon[key];
                        con = conCollection.GetConnection();

                    }
                    else
                    {
                        ConnectionCollection conCollection = new ConnectionCollection();
                        conCollection.Ip = ip;
                        conCollection.Port = port;
                        dicCon.Add(key, conCollection);
                        con = conCollection.GetConnection();
                    }
                    if (con != null)
                    {
                        con.UpdateTime = DateTime.Now;
                        con.State = State.busy;
                    }
                    return con;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                m_mutex.ReleaseMutex();
            }
        }

        public Connection getCon(string key, Connection con)
        {
            var tt = key.Split('_');
            IPAddress ipAddress = IPAddress.Parse(tt[0]);
            int port = int.Parse(tt[1]);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
            // Create a TCP/IP socket.
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Connect to the remote endpoint.
            client.Connect(remoteEP);
            if (con == null)
                con = new Connection();
            con.State = State.free;
            con.SetSocket(client);
            return con;
        }
    }

    public class ConnectionCollection
    {
        private System.Timers.Timer timer = new System.Timers.Timer();
        int Max_Conntions = 10;

        public ConnectionCollection()
        {
            FlagHealth = true;
            Lst = new List<Connection>();
            timer.Interval = 5000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(CheckHealth);
            timer.Start();
        }

        private Socket GetSocket()
        {
            IPAddress ipAddress = IPAddress.Parse(Ip);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                client.Connect(remoteEP);
                FlagHealth = true;
            }
            catch
            {
                FlagHealth = false;
            }
            return client;
        }

        public Connection GetConnection()
        {
            if (!FlagHealth)
            {
                return null;
            }
            lock (Lst)
            {
                for (int i = 0; i < Lst.Count; i++)
                {
                    var c = Lst[i];
                    if (c.State == State.free)
                    {
                        if (c.IsSocketed())
                        {
                            return c;
                        }
                        else
                        {
                            c.SetSocket( GetSocket());
                            return c;
                        }
                    }
                }
                while (true)
                {
                    var temp = Lst.Where(x => x.State == State.free).ToArray();
                    if (temp.Length > 0)
                    {
                        var c = temp[0];
                        if (c.IsSocketed() == true)
                        {
                            return c;
                        }
                        else
                        {
                            c.SetSocket(GetSocket());
                            return c;
                        }
                    }
                    if (Lst.Count < Max_Conntions)
                    {
                        Connection con = new Connection();
                        con.SetSocket(GetSocket());
                        con.State = State.free;
                        Lst.Add(con);
                        return con;
                    }
                    Thread.Sleep(50);
                }
            }
        }



        void CheckHealth(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Ip) && Port != 0)
            {
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    client.Connect(Ip, Port);
                    FlagHealth = true;
                }
                catch
                {
                    FlagHealth = false;
                }
                finally
                {
                    if (client != null)
                    {
                        try
                        {
                            client.Disconnect(false);
                            client.Close();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }


        public bool IsHealth
        {
            get
            {
                return FlagHealth;
            }
        }

        private bool FlagHealth
        {
            get;
            set;
        }

        public string Key
        {
            get
            {
                return Ip + "_" + Port;
            }
        }

        public string Ip
        {
            get;
            set;
        }

        public int Port
        {
            get;
            set;
        }

        public List<Connection> Lst
        {
            get;
            set;
        }


    }

    public class Connection
    {
        public Connection()
        {
            State = State.free; ;
        }



        public State State
        {
            get;
            set;
        }

        public int SendFailReset(byte[] buffer)
        {
            try
            {
                return Socket.Send(buffer);
            }
            catch
            {
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(Socket.RemoteEndPoint);
                Socket = client;
                return Socket.Send(buffer);
            }
        }

        public int Send(byte [] buffer)
        {
            return Socket.Send(buffer);
        }

        public int Receive(byte [] buffer)
        {
            return Socket.Receive(buffer);
        }

        public bool IsSocketed()
        {
            return Socket.Connected;
        }

        public void CloseSocket()
        {
             Socket.Disconnect(false);
             Socket.Close();
        }

        public void SetSocket(Socket socket)
        {
            Socket = socket;
        }

        private Socket Socket
        {
            get;
            set;
        }

        public DateTime UpdateTime
        {
            get;
            set;
        }

        public void Close()
        {
            State = State.free;
        }
    }

    public enum State
    {
        free,
        busy
    }
}

创建IO文件操作相关类:FileDeliver

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

namespace EMNewsInfo
{
    // State object for receiving data from remote device.
    public class StateObject
    {
        // Client socket.
        public Socket workSocket = null;
        // Size of receive buffer.
        public const int BufferSize = 1024;
        // Receive buffer.
        public byte[] buffer = new byte[BufferSize];
    }

    public class FileDeliver
    {
        // ManualResetEvent instances signal completion.
        private ManualResetEvent connectDone =
            new ManualResetEvent(false);
        private ManualResetEvent sendDone =
            new ManualResetEvent(false);
        private ManualResetEvent receiveDone =
            new ManualResetEvent(false);

        private ManualResetEvent receiveResult = new ManualResetEvent(false);
        // The response from the remote device.
        private String response = String.Empty;

        private bool StartClient(object command, out string msg)
        {
            msg = "";
            if (command == null)
            {
                return false;
            }
            Command c = command as Command;
            Connection client = null;
            // Connect to a remote device.
            try
            {

                client = ConnectionPool.GetConnection(c.ServerIp, c.Port);
                if (client == null)
                {
                    msg = c.ServerIp + ":" + c.Port + "建立链接失败";
                    return false;
                }
                // Send test data to the remote device.
                var sendCmd = new { Id = c.Id, LocalPath = c.ServerPath, Count = c.Count, Type = c.Type };
                Send(client, "<START>" + Newtonsoft.Json.JsonConvert.SerializeObject(sendCmd) + "<EOF>", true);
                // Receive the response from the remote device.
                Receive(client);
                // Write the response to the console.
                if (sendCmd.Type == Command.COPY)
                {
                    //要同步的一端已经完成了同步
                    if (response == "true")
                    {
                        return true;
                    }
                }
                int startIndex = int.Parse(response);
                Send(client, c.Data);
                Receive(client);
                if (response == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                msg = e.Message + Environment.NewLine + e.StackTrace;
                return false;
            }
            finally
            {
                try
                {
                    if (client != null)
                    {
                        client.Close();
                    }
                }
                catch
                {

                }
            }
        }


        private void Receive(Connection client)
        {

            // Create the state object.
            StateObject state = new StateObject();
            // Begin receiving the data from the remote device.
            int bytesRead = client.Receive(state.buffer);
            response = Encoding.GetEncoding("gbk").GetString(state.buffer, 0, bytesRead);
            Console.WriteLine("传回数据:" + response);
        }

        private void Send(Connection client, string data, bool failResetSocket)
        {
            if (failResetSocket == false)
            {
                Send(client, data);
            }
            else
            {
                // Convert the string data to byte data using ASCII encoding.
                byte[] byteData = Encoding.GetEncoding("gbk").GetBytes(data);
                // Begin sending the data to the remote device.
                //client.BeginSend(byteData, 0, byteData.Length, 0,
                //    new AsyncCallback(SendCallback), client);
                Send(client, byteData, failResetSocket);
                sendDone.Set();
            }
        }

        private void Send(Connection client, String data)
        {
            // Convert the string data to byte data using ASCII encoding.
            byte[] byteData = Encoding.GetEncoding("gbk").GetBytes(data);
            // Begin sending the data to the remote device.
            //client.BeginSend(byteData, 0, byteData.Length, 0,
            //    new AsyncCallback(SendCallback), client);
            Send(client, byteData);
            sendDone.Set();
        }

        private void Send(Connection client, string path, long startIndex)
        {
            FileStream fs = null;
            byte[] buffer = new byte[102400];
            FileInfo sendFile = new FileInfo(path);

            using (fs = sendFile.OpenRead())
            {
                fs.Position = startIndex;
                int tmpcount = 0;
                while ((tmpcount = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    if (tmpcount != buffer.Length)
                    {
                        byte[] endbuffer = new byte[tmpcount];
                        Array.Copy(buffer, endbuffer, tmpcount);
                        Send(client, endbuffer);
                        break;
                    }
                    Send(client, buffer);
                }
            }
        }

        private void Send(Connection client, byte[] data, bool failResetClient)
        {
            if (failResetClient == false)
            {
                Send(client, data);
            }
            else
            {
                client.SendFailReset(data);
            }
        }

        private void Send(Connection client, byte[] data)
        {
            client.Send(data);
        }


        public static bool StartTranCommand(byte[] data, string serverPath, out string msg)
        {
            string fileServer = System.Configuration.ConfigurationManager.AppSettings["FileServer"];
            string[] servers = fileServer.Split('|');
            return StartTranCommand(data, serverPath, servers, out msg);
        }


        /// <summary>
        /// 传送一个文件
        /// </summary>
        /// <param name="data">要传送文件的字节数组</param>
        /// <param name="serverPath">服务器端的路劲</param>
        /// <param name="servers">服务器列表</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public static bool StartTranCommand(byte[] data, string serverPath, string[] servers, out string msg)
        {
            msg = string.Empty;
            FileDeliver tt = new FileDeliver();
            foreach (var c in servers)
            {
                var t = c.Split(':');
                var server = new { ip = t[0], port = int.Parse(t[1]) };
                Command command = new Command();
                command.Id = Guid.NewGuid().ToString();
                command.Count = data.Length;
                command.CTime = DateTime.Now;
                command.Data = data;
                command.Port = server.port;
                command.ServerIp = server.ip;
                command.ServerPath = serverPath;
                command.Type = "copy";
                if (!tt.StartClient(command, out msg))
                {
                    return false;
                }
            }
            return true;
        }

    }
}

写文件方法:

/// <summary>
/// 创建列表信息文件
/// </summary>
/// <param name="_path"></param>
/// <param name="content"></param>
private void CreateListFile(string _path, string content)
{
      byte[] datas = System.Text.Encoding.GetEncoding("utf-8").GetBytes(content);
      string msg = "";
      FileDeliver.StartTranCommand(datas, _path, out msg);
}
多线程方法:

        List<string> codes = new List<string>();
        object locker = new object();
        /// <summary>
        /// 多线程生成文件
        /// </summary>
        private void Woker()
        {
            string surl = System.Configuration.ConfigurationManager.AppSettings["ServerUrl"];
            string _dir = "D:\\wwwroot\\down\\xg\\";
            bool bWork = true;
            while (bWork)
            {
                string code = "";
                lock (codes)
                {
                    if (codes == null || codes.Count == 0)
                    {
                        bWork = false;
                    }
                    else
                    {
                        code = codes[0];
                        codes.RemoveAt(0);
                    }
                }
                if (code != "")
                    CreateStockNews(code, surl, _dir);
            }
            lock (locker)
            {
                ClearInfo();
            }
        }

/// <summary>
        /// 生成个股
        /// </summary>
        /// <param name="code"></param>
        private void CreateStockNews(string code, string surl, string _dir)
        {
            Console.WriteLine("获取" + code + "的个股资讯");
            string url = surl + "/news/EPNewsInfo.ashx?action=GetStockNewsInfo&code=" + code;
            string resuls = OperMethod.GetPageByUrl(url, "gb2312");
            List<EMNewsInfoModel> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EMNewsInfoModel>>(resuls);
            string stockfile = _dir + code + "\\";
            Console.WriteLine("生成" + code + "的个股资讯详细信息");
            if (list != null && list.Count > 0)
            {
                if (!File.Exists(stockfile))
                {
                    Directory.CreateDirectory(stockfile);
                }
                CreateFile(stockfile, list);
            }
}

/// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearInfo()
        {
            Console.WriteLine("清除缓存资讯信息");
            string _url = "http://ccms.chinacache.com/index.jsp?user=emstock&pswd=Emstock123&ok=ok&dirs=http://down3.emstock.com.cn/xg/";
            string _content = OperMethod.GetPageByUrl(_url, "Gb2312");
            _url = "http://ccms.chinacache.com/index.jsp?user=emstock&pswd=Emstock123&ok=ok&dirs=http://down3.emstock.com.cn/dingpan/";
            _content = OperMethod.GetPageByUrl(_url, "Gb2312");
        }



linux下多进程多线程写文件

  • 2015年10月04日 18:17
  • 4KB
  • 下载

c#x写的多线程文件搜索查找

  • 2009年11月03日 11:07
  • 65KB
  • 下载

java多线程读写文件实例

Java多线程读文件: package com.myjava; import java.io.BufferedReader; import java.io.FileNotFoundException...

多线程通信读写文件

  • 2014年05月12日 13:27
  • 5.18MB
  • 下载

多线程写文件

  • 2014年12月19日 11:49
  • 15KB
  • 下载

C语言多线程的应用--局域网中多台计算机对同一文件读写

项目背景:生产制造型公司有多台生产测试设备同时运行,测试中产品错误代码记录在本地,现在需要将多台设备测试过程中出现的产品错误代码保存在局域网某台主机的公共文           件中 步骤1: 测...
  • kelery
  • kelery
  • 2016年05月26日 09:58
  • 437

3-1_12-17-2014(今天无作业)—文件读写-多线程

文件读写-多线程

JAVA多线程之两个线程同时写一个文件

1.多线程    线程是程序执行流的最小单元。是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所...

Python多线程写文件实例

转载自:http://michael8335.iteye.com/blog/1738485      由于最近一段时间,学了一门脚本语言-Python,原因是一、公司需要;二、只掌握Java感觉还是...
  • lhyhr
  • lhyhr
  • 2012年11月30日 11:20
  • 1399

VB.NET多线程文件读写

现在,流行的下载工具都使用了多线程的方式,这样比单线程下载快了很多。   不过那些通常都不是用VB.NET写的,网上可以找到的这方面的资料也比较少。自己琢磨了几天,同时也借助于网友的帮助,解决了这...
  • robake
  • robake
  • 2012年02月29日 10:06
  • 654
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:多线程写文件
举报原因:
原因补充:

(最多只允许输入30个字)