Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

 
Share this answer
 
Comments
Amar zaidi 19-Nov-12 12:49pm    
thats dont work the connection , the client don't find the server
i found solution usink Invoker change the server 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);


                        }


to

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; }
//change it here//////////////////////////////////////////////////////////
//*************************************************************************
 Invoke((MethodInvoker)delegate
                            {
                                fs.Write(buffer, 0, buffer.Length);
                            });
//change it here//////////////////////////////////////////////////////////
//*************************************************************************
                        }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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