Exemple #1
0
        /// <summary>
        /// Send handshake response to client application
        /// </summary>
        /// <param name="continueSending">Instruct the client application whether to
        /// send the file or not</param>
        public void HandshakeResponse(Socket client, bool continueSending)
        {
            HandshakeResponse response = new HandshakeResponse(continueSending);

            ///Serialize the response to sender
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                ///Serialize the response
                formatter.Serialize(stream, response);

                byte[] buffer = new byte[4];

                ///Send the length first
                buffer = AppUtil.ToByteArray((int)stream.Length);

                ///The Send() function is used to send the buffer to the server
                ///Send the length first
                SocketUtil.Send(client, buffer);

                ///Now send the actual data
                SocketUtil.Send(client, stream.ToArray());

                stream.Close();
            }
        }
Exemple #2
0
        /// <summary>
        /// Initialize handshake process and transfer initial information.
        /// Actual data will be sent after handshake
        /// </summary>
        /// <param name="filename">Name of file to send</param>
        /// <param name="len">Length of file</param>
        public void Handshake(string filename, int len)
        {
            HandshakeData handshake = new HandshakeData(filename, len);

            ///Lets now serialize the handshake data. Create a binary
            ///formatter which will help convert the serializable object
            ///into binary form
            BinaryFormatter formatter = new BinaryFormatter();

            ///Create a memory string that will hold the serialized data
            using (MemoryStream stream = new MemoryStream())
            {
                ///Now do the serialization of handshake data
                formatter.Serialize(stream, handshake);

                byte[] buffer = new byte[4];

                ///Send the length first
                buffer = AppUtil.ToByteArray((int)stream.Length);

                ///Send the length first
                SocketUtil.Send(this._socket, buffer);

                ///Send the data
                SocketUtil.Send(this._socket, stream.ToArray());

                ///Now get the handshake response
                HandshakeResponse response = this.GetHandshakeResponse();

                ///Close the stream as its not longer needed
                stream.Close();

                ///If casting failed
                if (response == null)
                {
                    throw new Exception("The handshake process with receiver failed. Receiver sent an invalid response");
                }

                ///Otherwise we check receivers response. If receiver denied receiving data...
                if (!response.Continue)
                {
                    throw new Exception("The destination computer denied to receive data");
                }
            }

            ///All is well and initial handshake process is completed.
            ///
        }
Exemple #3
0
        /// <summary>
        /// Get the response from server
        /// </summary>
        /// <returns></returns>
        private HandshakeResponse GetHandshakeResponse()
        {
            byte[] buffer = new byte[4];

            ///First we receive the 4 bytes of data
            ///
            SocketUtil.Receive(this._socket, buffer);

            int incomingLen = AppUtil.FromByteArray(buffer);

            if (incomingLen <= 0)
            {
                throw new Exception("The handshake process with receiver failed. No response from the server received");
            }

            ///Creat a new buffer to receive handshake response bytes
            buffer = new byte[incomingLen];

            SocketUtil.Receive(this._socket, buffer);

            ///Now we create a binary formatter to deserialize the handshake response
            ///
            BinaryFormatter formatter = new BinaryFormatter();

            ///Create a stream containing the serialized bytes
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                ///Deserialize it and try to cast it in HandshakeResponse
                HandshakeResponse response = formatter.Deserialize(stream) as HandshakeResponse;

                ///Close the stream
                stream.Close();

                return(response);
            }
        }