using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net.Sockets;

namespace sender
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string FileName = "";

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = openFileDialog1.FileName;
                FileName = openFileDialog1.SafeFileName;
            }
        }

        private void btnSendFile_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(txtFileName.Text, FileMode.Open);
            // read all bytes of file into buffer
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0,(int) fs.Length);
            fs.Close();
            // declare a binary formatter to serialize name of file
            BinaryFormatter bf = new BinaryFormatter();
            // a client to send file
            TcpClient client = new TcpClient(txtIP.Text, 7000);
            // stream that data should be send through 
            NetworkStream ns = client.GetStream();
            // serialize filename
            bf.Serialize(ns, FileName);
            // write file data into network stream
            BinaryWriter bw = new BinaryWriter(ns);
            bw.Write(buffer);
            bw.Close();
            ns.Close();
            client.Close();
        }
    }
}
