Click here to Skip to main content
15,884,176 members

problem with sending file C#

Amar zaidi asked:

Open original thread
my problem is :
i make two programs one server and the second as client the client send files to server but when he receive it the size of received file is bigger than the sent file .

and thats the sent code
C#
private void send_Click(object sender, EventArgs e)
{

    FileStream fs = new FileStream(@"c:\\file11.wav", FileMode.Open, FileAccess.Read);
    byte[] data= new byte[fs.Length];
    fs.Read(data, 0, data.Length);
   // Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); the line was declared as global variable
    sck.Send(BitConverter.GetBytes(data.Length), 0, 4, 0);// sending the size of file
    sck.Send(data);// sending the data

}



and at receiving we have this code

C#
FileStream fs = new FileStream(@"c:\\jj.wav", FileMode.Create, FileAccess.Write);//creat a receiv file 
            sckt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sckt.Bind(new IPEndPoint(0,08));
            sckt.Listen(1);
            acc = sckt.Accept();
            sckt.Close();
            new Thread(() =>
                {
                    while (true)
                    {
                        byte[] sizebuffer = new byte[4];
                        acc.Receive(sizebuffer, 0, sizebuffer.Length, 0);// receive the first packet how represent the file size
                        int size = BitConverter.ToInt32(sizebuffer, 0);
                       
// now we ll receive the rest of data
                        while (size > 0)
                        {
                            byte[] buffer;
                            if (size < acc.ReceiveBufferSize)
                            {
                                buffer = new byte[size];
                            }
                            else
                                buffer = new byte[acc.ReceiveBufferSize];
                            int rec = acc.Receive(buffer, 0, buffer.Length, 0);
                            size -= rec;
                            if (size < 0)
                            { size = 0; }
                            fs.Write(buffer, 0, buffer.Length);


                        }

if any one can help me plz i ll be so glade
Tags: Sockets

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900