发送文件C#的问题 [英] problem with sending file C#

查看:117
本文介绍了发送文件C#的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:
我制作了两个程序,一台服务器,另一台作为客户端,客户端将文件发送到服务器,但是当他接收到该文件时,接收到的文件的大小大于发送的文件.

多数民众赞成在发送代码

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

推荐答案

仔细检查一下,
在C#.NET 2.0中使用套接字应用程序进行文件传输 [ ^ ]
Go through it,
File Transfer using Socket Application in C# .NET 2.0[^]


我找到了解决方案usink Invoker更改服务器代码
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//////////////////////////////////////////////////////////
//*************************************************************************
                        }


这篇关于发送文件C#的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆