Beispiel #1
0
        public static FileReceiveStatus FileReceive(Socket socketFd)
        {
            //receive File size
            var sizeBytes = new byte[sizeof(int)];

            try
            {
                socketFd.Receive(sizeBytes, sizeBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            var size = BitConverter.ToInt32(sizeBytes, 0);

            //receive File
            var fas = new FileAndSize
            {
                SizeRemaining = size,
                SocketFd      = socketFd
            };

            socketFd.BeginReceive(fas.Buffer, 0, FileAndSize.BUF_SIZE, 0, FileReceiveCallback, fas);

            return(FileReceiveStatus.Ok);
        }
        public static FileReceiveStatus FileReceive(Socket socketFd)
        {
            //receive File size
            var sizeBytes = new byte[sizeof(int)];

            try
            {
                socketFd.Receive(sizeBytes, sizeBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            var size = BitConverter.ToInt32(sizeBytes, 0);

            //receive File
            var fas = new FileAndSize
            {
                SizeRemaining = size,
                SocketFd = socketFd
            };

            socketFd.BeginReceive(fas.Buffer, 0, FileAndSize.BUF_SIZE, 0,  FileReceiveCallback, fas);

            return FileReceiveStatus.Ok;
        }
Beispiel #3
0
        public static void SendFile(string path, string parentDirectory, Socket socketFd, ManualResetEvent handle)
        {
            //send File name
            var filePath = Path.Combine(parentDirectory, Path.GetFileName(path));

            filePath = filePath.Replace('\\', '/');
            var pathBytes = Encoding.ASCII.GetBytes(filePath);

            var pathSizeBytes = BitConverter.GetBytes(pathBytes.Length);

            try
            {
                socketFd.Send(pathSizeBytes, pathSizeBytes.Length, 0);

                socketFd.Send(pathBytes, pathBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            //send File size
            var file = File.ReadAllBytes(path);

            var fileSizeBytes = BitConverter.GetBytes(file.Length);

            try
            {
                socketFd.Send(fileSizeBytes, fileSizeBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            //send File
            var fas = new FileAndSize
            {
                SocketFd      = socketFd,
                File          = file,
                FileSize      = file.Length,
                SizeRemaining = file.Length,
                Handle        = handle
            };

            socketFd.BeginSend(file, 0, (fas.SizeRemaining < FileAndSize.BUF_SIZE ? fas.SizeRemaining : FileAndSize.BUF_SIZE), 0, SendFileCallback, fas);
        }
        public static void SendFile(string path, string parentDirectory, Socket socketFd, ManualResetEvent handle)
        {
            //send File name
            var filePath = Path.Combine(parentDirectory, Path.GetFileName(path));
            filePath = filePath.Replace('\\', '/');
            var pathBytes = Encoding.ASCII.GetBytes(filePath);

            var pathSizeBytes = BitConverter.GetBytes(pathBytes.Length);

            try
            {
                socketFd.Send(pathSizeBytes, pathSizeBytes.Length, 0);

                socketFd.Send(pathBytes, pathBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            //send File size
            var file = File.ReadAllBytes(path);

            var fileSizeBytes = BitConverter.GetBytes(file.Length);

            try
            {
                socketFd.Send(fileSizeBytes, fileSizeBytes.Length, 0);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Exception:\t\n" + exc.Message);
                var window = (ProjectZip)Application.OpenForms[0];
                window.SetControls(true);
            }

            //send File
            var fas = new FileAndSize
            {
                SocketFd = socketFd,
                File = file,
                FileSize = file.Length,
                SizeRemaining = file.Length,
                Handle = handle
            };

            socketFd.BeginSend(file, 0, (fas.SizeRemaining < FileAndSize.BUF_SIZE ? fas.SizeRemaining : FileAndSize.BUF_SIZE), 0, SendFileCallback, fas);
        }