Example #1
0
        /// <summary>
        /// 发送mod文件
        /// </summary>
        public void SendMod(string modName)
        {
            string      ip          = ProgramTools.GetXmlNoteValue("ip");
            int         port        = int.Parse(ProgramTools.GetXmlNoteValue("sendModPort"));
            IPAddress   ipAdr       = IPAddress.Parse(ip);
            IPEndPoint  hosts       = new IPEndPoint(ipAdr, port);
            TcpListener tcpLisyener = new TcpListener(hosts);

            tcpLisyener.Start();
            Thread thread = new Thread((object obj) =>             // 同时有多个客户端监听此端口,会怎样?
            {
                TcpListener tcpListener = obj as TcpListener;
                while (true)
                {
                    try
                    {
                        // 接收请求
                        TcpClient tcpClient = tcpListener.AcceptTcpClient();
                        if (tcpClient.Connected)
                        {
                            // 要传输的流
                            NetworkStream stream = tcpClient.GetStream();

                            string modPath = ProgramTools.GetXmlNoteValue("modPath") + @"/" + modName;

                            FileStream fileStream = new FileStream(modPath, FileMode.Open);

                            int fileReadSize = 0;
                            long fileLength  = 0;

                            while (fileLength < fileStream.Length)  //将文件流写入到 NetworkStream 中。问题:当流中数据小于2048(buffer.Length)时,取出的数据是否有问题
                            {
                                byte[] buffer = new byte[2048];
                                fileReadSize  = fileStream.Read(buffer, 0, buffer.Length);
                                stream.Write(buffer, 0, fileReadSize);
                                fileLength += fileReadSize;
                            }
                            fileStream.Flush();
                            stream.Flush();
                            fileStream.Close();
                            stream.Close();
                            tcpListener.Stop();
                            Console.WriteLine("发送成功!");
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("发送失败" + e.Message);
                        //Thread.CurrentThread.Abort();
                    }
                }
            });

            thread.Start(tcpLisyener);
            thread.IsBackground = true;
        }
Example #2
0
        static void Main(string[] args)
        {
            // 初始化方法
            ProgramTools.SetXmlRoot();

            // 变量
            string ip;
            int    port;

            //string currenPath = System.IO.Directory.GetCurrentDirectory();
            //XDocument document = XDocument.Load(currenPath + "\\Application.xml");
            //XElement root = document.Root;
            //XElement ele = root.Element("Setting");

            ip   = ProgramTools.GetXmlNoteValue("ip");
            port = int.Parse(ProgramTools.GetXmlNoteValue("port"));//int.Parse(ele.Attribute("port").Value);

            Console.WriteLine("服务器启动中,IP:{0},Port:{1}", ip, port);
            Serv serv = new Serv();

            serv.Start(ip, port);

            while (true)
            {
                string str = Console.ReadLine();
                switch (str)
                {
                case "quit":
                    return;

                case "updata":
                    ProgramTools.GetFileNameList(ProgramTools.GetXmlNoteValue("modPath"));
                    break;
                }
            }


            //IPAddress ipAdr = IPAddress.Parse("172.17.0.5");
            //IPEndPoint hosts = new IPEndPoint(ipAdr, 2556);
            //TcpListener tcpLisyener = new TcpListener(hosts);
            //tcpLisyener.Start();
            //Console.WriteLine("开始监听...");
            //while (true)
            //{
            //    string str = Console.ReadLine();
            //    switch(str)
            //    {
            //        case "quit":
            //            return;
            //        case "send":
            //            Thread thread = new Thread(SendFileFunc);
            //            thread.Start(tcpLisyener);
            //            thread.IsBackground = true;
            //            break;
            //    }
            //}
        }
Example #3
0
        /// <summary>
        /// 发送mod列表文件
        /// </summary>
        public void SendModListFile()
        {
            string ip   = ProgramTools.GetXmlNoteValue("ip");
            int    port = int.Parse(ProgramTools.GetXmlNoteValue("sendPort"));

            Console.WriteLine("ip : port" + ip + " : " + port);
            IPAddress   ipAdr       = IPAddress.Parse(ip);
            IPEndPoint  hosts       = new IPEndPoint(ipAdr, port);
            TcpListener tcpLisyener = new TcpListener(hosts);

            tcpLisyener.Start();
            Thread thread = new Thread(SendModTextFileFunc);

            thread.Start(tcpLisyener);
            thread.IsBackground = true;
        }