示例#1
0
        static void Send(string pathToFile, string endAddress)
        {
            // Create socket
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Create image of file
            FileImage fi = null;

            try
            {
                fi = new FileImage(pathToFile);
            }
            // I know these are pretty sweaping catches but we really don't
            // need to handle specific exceptions we just need to keep going
            catch (Exception e)
            {
                Error(e.GetType().ToString() + " - " + e.Message, false);
                return;
            }

            // Setup network endpoint
            IPAddress  ip = IPAddress.Parse(endAddress);
            IPEndPoint ep = new IPEndPoint(ip, 13450);

            try
            {
                // Try to connect to end point
                Console.Write("Connecting to [{0}]...", ep.ToString());
                LoadingSpinner.Start();
                sock.Connect(ep);
                LoadingSpinner.Stop();
            }
            catch (Exception e)
            {
                // Incase we exceptioned before we could stop the spinner
                LoadingSpinner.Stop();
                Error(e.GetType().ToString() + " - " + e.Message);
            }

            try
            {
                // Serialise FileImage object onto the socket's network stream
                Console.Write("Sending file [{0}]...", fi.Name);
                LoadingSpinner.Start();
                NetworkStream objStream = SerializeFileImage(fi, sock);
                LoadingSpinner.Stop();
                Console.WriteLine("Transfer complete. ");
            }
            catch (Exception e)
            {
                LoadingSpinner.Stop();
                Error(e.GetType().ToString() + " - " + e.Message, false);
            }

            // Clean up
            sock.Shutdown(SocketShutdown.Both);
            sock.Close();
            fi.Dispose();
            Console.WriteLine("Connection closed.");
        }
示例#2
0
 public void CreateFile(string path)
 {
     try
     {
         // Create file at specified path
         path = Path.Combine(path.Replace("\"", ""), Name);
         LoadingSpinner.Start();
         File.WriteAllBytes(path, Data);
         LoadingSpinner.Stop();
         Console.WriteLine("file created at [{0}]", path);
     }
     catch (Exception e)
     {
         LoadingSpinner.Stop();
         Program.Error(e.GetType().ToString() + " - " + e.Message, false);
     }
 }
示例#3
0
        static void Recieve(string transferPath, string address)
        {
            // Create listener for port 11000
            TcpListener listener = new TcpListener(IPAddress.Parse(address), 13450);

            listener.Start();
            Console.WriteLine("Listening on {0}...", listener.LocalEndpoint.ToString());

            // Connection loop
            while (true)
            {
                try
                {
                    using (var client = listener.AcceptTcpClient())
                        using (var stream = client.GetStream())
                        {
                            // Deserialise stream
                            Console.Write("[{0}] is sending a file, processing...", client.Client.RemoteEndPoint.ToString().Split(':')[0]);
                            LoadingSpinner.Start();
                            using (FileImage fi = DeserializeFileImage(stream))
                            {
                                LoadingSpinner.Stop();
                                Console.WriteLine("Transfer complete.");

                                // Create transfered file
                                Console.Write("Creating \"" + fi.Name + "\"...");
                                fi.CreateFile(transferPath);
                            }
                        }
                }
                catch (Exception e)
                {
                    Error(e.GetType().ToString() + " - " + e.Message, false);
                }
            }
        }