Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Sockets
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
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
 
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 19-Nov-12 0:41am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Amar zaidi at 19-Nov-12 12:49pm
   
thats dont work the connection , the client don't find the server
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

i found solution usink Invoker change the server code
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
 
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//////////////////////////////////////////////////////////
//*************************************************************************
                        }
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

thank you but his code is for a tiny files i wanna to use it for a big files
  Permalink  
Comments
André Kraak at 19-Nov-12 7:25am
   
If you have a question about or comment on a given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.
 
Please move the content of this solution to the solution you are commenting on and remove the solution.
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

any help please i m waiting your response
  Permalink  
Comments
Krunal R at 19-Nov-12 7:08am
   
In this article there are two links... Go through it :)

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100