示例#1
0
        private void WorkWithClient()
        {
            try
            {
                Client.Client.ReceiveTimeout = 7000;
                Client.Client.SendTimeout    = 7000;
                string firstMessage = myStream.ReadString();
                myStream.SendNEXT();

                if (firstMessage != "<isClient>")
                {
                    throw new Exception($"first message = {firstMessage}, unknown client try to connect");
                }


                string fileName = myStream.ReadString();
                if (!ListHolder.Files.Exists(c => c.FileName == fileName))
                {
                    myStream.Write("<unknownFile>");
                    return;
                }
                else
                {
                    myStream.Write("<fileFound>");
                }

                udpListenerIP = IPBuilder.GetIP();
                udpListener   = new UdpClient(udpListenerIP);

                string udpClientAddress = myStream.ReadString();
                myStream.Write(udpListenerIP.Address.ToString());

                int udpClientPort = myStream.ReadInt();
                myStream.Write(udpListenerIP.Port);

                IPEndPoint udpClientIP = IPBuilder.GetIP(udpClientAddress, udpClientPort);

                SendFile(fileName, udpClientIP);

                udpListener.Close();
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception e)
            {
                MyDispatcher.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show(e.Message, "File server error: when initial sending file");
                });
            }
        }
        private IPEndPoint GetMainServerIP()
        {
            IPEndPoint ip = IPBuilder.GetIP(MainServerAddress.Text, int.Parse(MainServerPort.Text));

            return(ip);
        }