简体   繁体   中英

File sending from server to one client or multiple in C#

this is my server side application code: there is logical error that is not being resolved plz anyone hlp.....

public void SendFile(string IP, string path, string FileName) { try {

            FileStream fs = new FileStream(path, FileMode.Open);//assign file to stream
            byte[] buffer = new byte[fs.Length];//initialize buffer with stream length
            int len = (int)fs.Length;
            fs.Read(buffer, 0, len);//read bytes from stream
            fs.Close();


            BinaryFormatter br = new BinaryFormatter();
            TcpClient myclient = new TcpClient(IP, 9000);
            NetworkStream myns = myclient.GetStream();//assign stream to client
            br.Serialize(myns, FileName);//provide header to stream

            BinaryWriter mysw = new BinaryWriter(myns);
            mysw.Write(buffer);//write file in stream
            MessageBox.Show("File Successfully Transfered", "File Send", MessageBoxButtons.OK, MessageBoxIcon.Information);
            mysw.Close();

            myns.Close();
            myclient.Close();
        }
        catch
        {
            MessageBox.Show("Reciever end Not Found", "File Sending Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

And this is my client side application code there is logical error that is not being resolved plz anyone hlp.....

public void FileReceiver() {

        NetworkStream myns;
        TcpListener mytcpfilelistner;
        Socket mysocketfile;
        Thread myth;
        BinaryReader bb;
        try
        {
            mytcpfilelistner = new TcpListener(9000);
            mytcpfilelistner.Start();
            mysocketfile = mytcpfilelistner.AcceptSocket();
            myns = new NetworkStream(mysocketfile);
            BinaryFormatter br = new BinaryFormatter();
            object op;

            op = br.Deserialize(myns); // Deserialize the Object from Stream


            bb = new BinaryReader(myns);
            byte[] buffer = bb.ReadBytes(2000000);


            FileStream fss = new FileStream("C:\\" + (string)op, FileMode.CreateNew, FileAccess.Write);
            fss.Write(buffer, 0, buffer.Length);
            fss.Close();
            mytcpfilelistner.Stop();
            MessageBox.Show("One File Reeceived and Saved in C Drive", "File Recieve", MessageBoxButtons.OK, MessageBoxIcon.Information);
            LB.Client.lbclient objlb = new LB.Client.lbclient();
            objlb.popup("One File Recieved and And Seved in  C:\\" + (string)op);

            if (mysocketfile.Connected == true)
            {
                while (true)
                {
                    FileReceiver();
                }
            }
        }
        catch
        { }
    }

I don't see what your question is above. But, you can take a look at this thread SendFile over TCP/IP .NET CF for a simpler way to send files over a socket. Also, I really recommend you read about and make use of the using statement in C# and IDisposable because you should be using that throughout your code above to deterministically clean up resources your code allocates. Here's an article to get you started https://msdn.microsoft.com/en-us/library/yh598w02.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM