Example #1
0
 private void ReportAddition(NamedIPEndPoint client)
 {
     if (client != null && !clientsListView.Items.Contains(client))
     {
         clientsListView.Items.Add(client);
     }
 }
Example #2
0
        /*
         * Discovers hosts that are in public mode (that can accept file transfers).
         */
        public NamedIPEndPoint ClientDiscovery()
        {
            Debug.WriteLine("[SERVER] Searching hosts...");

            if (udpClient.Available > 0)
            {
                IPEndPoint clientEp = new IPEndPoint(0, 0); // Initializing an "empty" IPEndPoint

                var clientRequestData = udpClient.Receive(ref clientEp);
                var clientRequest     = Encoding.ASCII.GetString(clientRequestData);

                if (clientRequest.Contains(tfString)) // If I received the broadcast packet containing the tfString
                {
                    string[] announcement = clientRequest.Split('_');
                    clientEp.Port = Convert.ToInt32(announcement[2]); // Replacing the endpoint port with the one received into the UDP payload
                    NamedIPEndPoint namedClientEp = new NamedIPEndPoint(announcement[1], clientEp);
                    if (!availableClients.Contains(namedClientEp))
                    {
                        availableClients.Add(namedClientEp); // Client added to the list of available clients
                        return(namedClientEp);
                    }
                }
            }

            return(null);
        }
Example #3
0
        public SendingFileWindow(Server server, NamedIPEndPoint selectedClient, string filePath)
        {
            this.filePath       = filePath;
            this.server         = server;
            this.selectedClient = selectedClient;
            InitializeComponent();

            StartSending();
        }
Example #4
0
        public FileTransferData StartSending(string filePath, NamedIPEndPoint selectedClient)
        {
            FileTransferData fileTransferData = new FileTransferData();

            fileTransferData.HostName = selectedClient.Name;

            TcpClient tcpClient = new TcpClient();

            tcpClient.Connect(selectedClient.EndPoint); // Connecting to the client specified (throws an exception if not available)

            if (Directory.Exists(filePath))
            {
                string tempPath = Path.GetTempPath() + new DirectoryInfo(filePath).Name + ".zip";
                if (File.Exists(tempPath)) // Check if the file already exists so that ZipFile doesn't throw an exception
                {
                    File.Delete(tempPath);
                }
                ZipFile.CreateFromDirectory(filePath, tempPath);
                filePath = tempPath;
            }
            FileInfo fi = new FileInfo(filePath); // Obtaining the infos of the specified file

            fileTransferData.Name   = fi.Name;
            fileTransferData.Length = fi.Length;
            Debug.WriteLine("[SERVER] File length of the sent file: " + fileTransferData.Length);
            Debug.WriteLine("[SERVER] File name of the sent file: " + fileTransferData.Name);

            fileTransferData.NetworkStream = tcpClient.GetStream();
            fileTransferData.NetworkStream.WriteTimeout = 20000;

            byte[] hostNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(Environment.UserName));
            fileTransferData.NetworkStream.Write(hostNameLengthBuffer, 0, hostNameLengthBuffer.Length);

            byte[] hostNameBuffer = Encoding.Unicode.GetBytes(Environment.UserName);
            fileTransferData.NetworkStream.Write(hostNameBuffer, 0, hostNameBuffer.Length);


            byte[] fileNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(fileTransferData.Name));
            fileTransferData.NetworkStream.Write(fileNameLengthBuffer, 0, fileNameLengthBuffer.Length);

            byte[] fileNameBuffer = Encoding.Unicode.GetBytes(fileTransferData.Name);
            fileTransferData.NetworkStream.Write(fileNameBuffer, 0, fileNameBuffer.Length);


            byte[] fileLengthBuffer = BitConverter.GetBytes(fileTransferData.Length);
            fileTransferData.NetworkStream.Write(fileLengthBuffer, 0, fileLengthBuffer.Length);
            fileTransferData.FileStream = File.OpenRead(filePath);

            Debug.WriteLine("[SERVER] Initial file data sent successfully");

            return(fileTransferData);
        }