Пример #1
0
        public int get(string host, ushort port, string fileName, string savePath, ref string err, OnData onData)
        {
            byte[] byteHead;
            byte[] byteData;
            string pathFile = savePath;
            string fullPath = savePath;

            if (fullPath[fullPath.Length - 1] != '\\')
            {
                fullPath += "\\";
            }
            else
            {
                pathFile = savePath.Substring(0, savePath.Length - 1);
            }
            fullPath += fileName;

            try
            {
                if (File.Exists(pathFile))
                {
                    File.Delete(pathFile);
                }

                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                TcpClient tclient = new TcpClient();
                tclient.Connect(host, port);
                NetworkStream stream  = tclient.GetStream();
                ReqFileData   req     = new ReqFileData();
                int           sendLen = Marshal.SizeOf(req);
                sendLen += fileName.Length + 1;

                byte[] byteFileName = Encoding.ASCII.GetBytes(fileName);
                byte[] byteSend     = new byte[sendLen];
                byteFileName.CopyTo(byteSend, Marshal.SizeOf(req));
                byteSend[sendLen - 1] = 0;

                //req.req.fileName = byteSend;
                req.head.key   = 0;
                req.head.magic = IPAddress.HostToNetworkOrder((int)LtNet.LT_MAGIC);
                req.head.job   = IPAddress.HostToNetworkOrder((int)JobId.JOB_FILE);
                req.head.len   = IPAddress.HostToNetworkOrder(sendLen);
                req.req.len    = IPAddress.HostToNetworkOrder(byteFileName.Length + 1);
                byte[] byteHeadSend = StructToBytes(req);
                byteHeadSend.CopyTo(byteSend, 0);

                stream.Write(byteSend, 0, sendLen);
                stream.Flush();

                //读取头部
                ResData res      = new ResData();
                int     headSize = Marshal.SizeOf(res);
                byteHead = new byte[headSize];
                int count = stream.Read(byteHead, 0, headSize);
                if (count < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    err = "更新数据失败:\n";
                    return(-1);
                }

                res            = (ResData)BytesToStruct(byteHead, res.GetType());
                res.head.key   = IPAddress.NetworkToHostOrder(res.head.key);
                res.head.magic = IPAddress.NetworkToHostOrder(res.head.magic);
                res.head.job   = IPAddress.NetworkToHostOrder(res.head.job);
                res.head.len   = IPAddress.NetworkToHostOrder(res.head.len);
                res.result     = IPAddress.NetworkToHostOrder(res.result);
                if (res.head.magic != LtNet.LT_MAGIC || res.head.job != (int)JobId.JOB_FILE || res.head.len < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    err = "更新数据失败:错误的响应\n";
                    return(-1);
                }

                if (res.result != 0)
                {
                    stream.Close();
                    tclient.Close();
                    err = "更新数据失败:服务器错误\n";
                    return(-1);
                }

                if (res.head.len == headSize)
                {
                    err = "更新数据失败:文件为空\n";
                    stream.Close();
                    tclient.Close();
                    return(-1);
                }

                int dataLen = res.head.len - headSize;
                byteData = new byte[LtNet.RCV_BUF_LEN];
                int recvLen = 0;

                StreamWriter  objWriter = new StreamWriter(fullPath, false);
                ASCIIEncoding encoding  = new ASCIIEncoding();

                //读取文件内容
                while (recvLen < dataLen)
                {
                    int recvCount = stream.Read(byteData, 0, (int)LtNet.RCV_BUF_LEN);
                    if (recvCount < 0)
                    {
                        stream.Close();
                        tclient.Close();
                        err = "更新数据失败:\n";
                        return(-1);
                    }
                    else if (recvCount == 0)
                    {
                        break;
                    }

                    if (onData != null)
                    {
                        onData(recvCount);
                    }

                    recvLen += recvCount;
                    objWriter.BaseStream.Write(byteData, 0, recvCount);
                }

                stream.Close();
                tclient.Close();
                objWriter.Close();
            }
            catch (System.Exception ex)
            {
                err = "更新数据失败:" + ex.Message;
                return(-1);
            }

            return(0);
        }
Пример #2
0
        public static void get_new(object ltd)
        {
            LtData ltData = (LtData)ltd;

            int tryTimes = 3;
            int start    = 2003001;
            int stop     = 2020999;

            byte[] byteHead;
            byte[] byteData;


            if (ltData.ltSet.Count > 0)
            {
                start  = ((LottoryItem)ltData.ltSet[ltData.ltSet.Count - 1]).id;
                start += 1;
            }
RETRY:
            try
            {
                TcpClient tclient = new TcpClient();
                tclient.Connect("42.121.193.75", 5000);
                NetworkStream stream = tclient.GetStream();

                ReqHisData req = new ReqHisData();
                req.head.key   = 0;
                req.head.magic = IPAddress.HostToNetworkOrder((int)LtNet.LT_MAGIC);
                req.head.job   = IPAddress.HostToNetworkOrder((int)JobId.JOB_HISTORY);
                req.head.len   = IPAddress.HostToNetworkOrder(Marshal.SizeOf(req));
                req.req.start  = IPAddress.HostToNetworkOrder(start);
                req.req.stop   = IPAddress.HostToNetworkOrder(stop);

                stream.Write(StructToBytes(req), 0, Marshal.SizeOf(req));
                stream.Flush();

                //读取头部
                ResData res      = new ResData();
                int     headSize = Marshal.SizeOf(res);
                byteHead = new byte[headSize];
                int count = stream.Read(byteHead, 0, headSize);
                if (count < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:\n");
                    return;
                }

                res            = (ResData)BytesToStruct(byteHead, res.GetType());
                res.head.key   = IPAddress.NetworkToHostOrder(res.head.key);
                res.head.magic = IPAddress.NetworkToHostOrder(res.head.magic);
                res.head.job   = IPAddress.NetworkToHostOrder(res.head.job);
                res.head.len   = IPAddress.NetworkToHostOrder(res.head.len);
                res.result     = IPAddress.NetworkToHostOrder(res.result);
                if (res.head.magic != LtNet.LT_MAGIC || res.head.job != (int)JobId.JOB_HISTORY || res.head.len < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:错误的响应\n");
                    return;
                }

                if (res.result != 0)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:服务器错误\n");
                    return;
                }

                if (res.head.len == headSize)
                {
                    stream.Close();
                    tclient.Close();
                    return;
                }

                int dataLen = res.head.len - headSize;
                byteData = new byte[dataLen];
                int recvLen = 0;

                while (recvLen < dataLen)
                {
                    int recvCount = stream.Read(byteData, recvLen, dataLen - recvLen);
                    if (recvCount <= 0)
                    {
                        stream.Close();
                        tclient.Close();
                        MessageBox.Show("更新数据失败:\n");
                        return;
                    }
                    recvLen += recvCount;
                }
            }
            catch (System.Exception ex)
            {
                tryTimes--;
                if (tryTimes > 0)
                {
                    goto RETRY;
                }

                //MessageBox.Show("更新数据失败:" + ex.Message);
                return;
            }

            StreamWriter  objWriter = new StreamWriter(DEFINE.HISTORY_FILE, true);
            ASCIIEncoding encoding  = new ASCIIEncoding();
            string        strLt     = encoding.GetString(byteData);
            int           itemAdd   = 0;

            //objWriter.Write(strLt);

            string[] items = strLt.Split('\n');
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Length < 10)
                {
                    continue;
                }

                LottoryItem item = new LottoryItem();
                if (string_to_item(items[i], item) == 0)
                {
                    ltData.ltSet.Add(item);
                    //if (i < (items.Length - 1))
                    {
                        items[i] += "\n";
                    }
                    objWriter.Write(items[i]);
                    itemAdd++;
                }
            }
            objWriter.Close();

            if (itemAdd > 0)
            {
                Thread.Sleep(1000);
                ltData.form.otherUpdateDatas();
            }
        }