1

I have a serious problem with this code. This is how it should work: Client connects to server and choose a file on disk. after that client sends a (byte[] buffer) to the server by this format("File" (4 bytes) + FileNameLength (4 bytes) + FileDataLength (4 bytes)). After that server creates a (byte[] buffer) with this size (new byte[FileNameLength + FileDataLength]).So client sends a data to the server by this format(byte[] buffer = FileName + FileData). And the server gets a file. The problem is here that i have a MessageBox in the Server to see the FileName after receiving that but MessageBox is always blank and it runs times and times and times. what's the solution?

The Server:

    private Socket SServer = null;
    private Socket SClient = null;
    private byte[] buffer = new byte[1024];
    private byte[] FileNameLength = null;
    private byte[] FileSize = null;

    private void Server_Load(object sender, EventArgs e)
    {
        SServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SServer.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000));
        SServer.Listen(1);
        new Thread(() =>
        {
            SClient = SServer.Accept();
            MessageBox.Show("Connected.");
            new Thread(() => Receiver()).Start();
        }).Start();
    }

    private void Receiver()
    {
        buffer = new byte[1024];
        while (true)
        {
            Int32 AllLength = SClient.Receive(buffer, 0, buffer.Length, SocketFlags.None);
            byte[] Devider = new byte[4];
            Array.Copy(buffer, 0, Devider, 0, 4);
            string Devide = Encoding.ASCII.GetString(Devider);
            if (AllLength > 0)
            {
                if (Devide == "File")
                {
                    FileNameLength = new byte[4];
                    Array.Copy(buffer, 4, FileNameLength, 0, 4);
                    FileSize = new byte[4];
                    Array.Copy(buffer, 8, FileSize, 0, 4);
                    buffer = null;
                    buffer = new byte[BitConverter.ToInt32(FileNameLength, 0) + BitConverter.ToInt32(FileSize, 0)];
                }
                else
                {
                    byte[] FileNameBytes = new byte[BitConverter.ToInt32(FileNameLength, 0)];
                    Array.Copy(buffer, 0, FileNameBytes, 0, BitConverter.ToInt32(FileNameLength, 0));
                    byte[] FileBytes = new byte[BitConverter.ToInt32(FileSize, 0)];
                    Array.Copy(buffer, BitConverter.ToInt32(FileNameLength, 0), FileBytes, 0, BitConverter.ToInt32(FileBytes, 0));
                    string FileName = Encoding.ASCII.GetString(FileNameBytes);
                    MessageBox.Show(FileName);
                    buffer = null;
                    buffer = new byte[1024];
                }
            }
        }
    }

TheClient:

    private Socket SClient = null;
    string Path, FileName;

    private void Client_Load(object sender, EventArgs e)
    {
        SClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000));
    }

    private void BT_SendFile_Click(object sender, EventArgs e)
    {
        byte[] FileLengthBytes = BitConverter.GetBytes(FileName.Length);
        byte[] FileBytes = File.ReadAllBytes(Path + FileName);
        byte[] buffer = new byte[FileLengthBytes.Length + FileBytes.Length + 4];
        //buffer = Encoding.Unicode.GetBytes("File") + FileLengthBytes + FileBytes;
        Array.Copy(Encoding.ASCII.GetBytes("File"), 0, buffer, 0, 4);
        Array.Copy(FileLengthBytes, 0, buffer, 4, FileLengthBytes.Length);
        Array.Copy(BitConverter.GetBytes(FileBytes.Length), 0, buffer, 8, 4);
        SClient.Send(buffer, 0, buffer.Length, SocketFlags.None);
        byte[] FileNameBytes = Encoding.ASCII.GetBytes(FileName);
        buffer = null;
        buffer = new byte[FileNameBytes.Length + FileBytes.Length];
        Array.Copy(FileNameBytes, 0, buffer, 0, FileNameBytes.Length);
        Array.Copy(FileBytes, 0, buffer, FileNameBytes.Length, FileBytes.Length);
        SClient.Send(buffer, 0, buffer.Length, SocketFlags.None);
    }

    private void BT_Browse_Click(object sender, EventArgs e)
    {
        OpenFileDialog N = new OpenFileDialog();
        if (N.ShowDialog() == DialogResult.OK)
        {
            TB_Address.Text = N.FileName;
            string[] Seperate = N.FileName.Split('\\');
            FileName = Seperate[Seperate.Length - 1];
            Path = null;
            foreach (string str in Seperate)
            {
                if (str != Seperate[Seperate.Length - 1])
                    Path += str + "\\";
            }
        }
    }
mamadh1378
  • 41
  • 3
  • 1
    You will not get the entire receive message in one chunk TCP has a maximum datagram size of ~1500 bytes. You file is probably a lot larger. So you will only have to remove the filename and size from initial chunk. Try debugging yourself and make sure you are extracting the filename and size correctly and the first chunk of file. It is very basic debugging that you do in CS 101 Basket Weaving. – jdweng Jan 31 '19 at 12:01
  • Why reinvent the wheel when there is [FTP](https://en.wikipedia.org/wiki/File_Transfer_Protocol), [FTPs, SFTP](https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol), [SCP](https://en.wikipedia.org/wiki/Secure_copy), ...? – Fildor Jan 31 '19 at 12:03

1 Answers1

0

as this friend (Fildor) said, i try to read more about the protocols. thank you two guys but i think if i separate the file and send it pieces by pieces, i can send the hole file. I know maybe this is stupid but i think it works.

mamadh1378
  • 41
  • 3