コード例 #1
0
        private InfoFileNew GetInformationAboutFile(string pathToFile)
        {
            try
            {
                pathToFile = string.Format(@"E:\MyOneDrive\" + pathToFile);

                bool isFile = false;
                isFile = File.Exists(pathToFile);
                if (isFile)
                {
                    {
                        InfoFileNew fileNew = new InfoFileNew();
                        fileNew._fileName = Path.GetFileName(pathToFile);
                        fileNew._path     = pathToFile;
                        fileNew._sizeFile = File.ReadAllBytes(pathToFile).Length;

                        return(fileNew);
                    }
                }
                else
                {
                    throw new NullReferenceException("Файл не знайдено!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        public void StartServer()
        {
            for (int i = 0; i < 1; i++)
            {
                // Назначаем сокет локальной конечной точке и слушаем входящие сокеты
                try
                {
                    sListener.Bind(ipEndPoint);
                    sListener.Listen(10);

                    // Начинаем слушать соединения
                    while (true)
                    {
                        Console.WriteLine("Ожидаем соединение через порт {0}", ipEndPoint);

                        // Программа приостанавливается, ожидая входящее соединение
                        Socket handler = sListener.Accept();
                        string data    = null;

                        // Мы дождались клиента, пытающегося с нами соединиться
                        {
                            byte[] receiveBytes = new byte[5000];
                            Console.WriteLine("-----------*******Получиние информации о файле*******-----------");

                            int bytesRec = handler.Receive(receiveBytes);

                            InfoFileNew infoPath = receiveBytes.UnpackFromXml <InfoFileNew>();

                            InfoFileNew infoFile = this.GetInformationAboutFile(infoPath._path);
                            handler.Send(infoFile.PackToXml());
                            Console.WriteLine("----Информация о файле получена и отправлена!");

                            //InfoFileNew infoFile = receiveBytes.UnpackFromXml<InfoFileNew>();
                            Console.WriteLine("-----------*******Ожидание файла*******-----------");
                            byte[] file = GetFile(infoPath._path);



                            //byte[] receiveBytes1 = new byte[infoFile._sizeFile];

                            //int bytesRec1 = handler.Receive(receiveBytes1);
                            Console.WriteLine("----Файл получен... Отправление...");
                            handler.Send(file);
                            // CreateFile(receiveBytes1, infoFile._path, infoFile._fileName);
                            Console.WriteLine("----Файл отправлен...");
                        }

                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    i--;
                }
                finally
                {
                    Console.ReadLine();
                }
            }
        }