Hello,

I have this code for sending and receiving file transfer using C#.
But I don't know how to send the file name and save it as file name..
have tried many methods and googling too and still haven't found any clue.

Here the client send file:

Stream Fs = File.OpenRead(textBox1.Text);
            Byte[] buffer = new Byte[Fs.Length];
            Fs.Read(buffer, 0, buffer.Length);
            TcpClient socket = new TcpClient(ipAddress, port);
            NetworkStream nw = socket.GetStream();
            nw.Write(buffer, 0, buffer.Length);
            nw.Close();

And Here the Server Receive File using threading
*The one I put red is actually I had to set the filename and Extension..
What I want is, how to accept the filename and extension from sender and put it there?

void handlerThread()
        {
            TcpClient handlerSocket = (TcpClient)AiSocket[AiSocket.Count - 1];
            NetworkStream Nw = new NetworkStream(handlerSocket.Client);
            int thisRead = 0;
            int Blocksize = 1024;
            Byte[] dataByte = new Byte[Blocksize];


            string fileName = Encoding.ASCII.GetString(dataByte);
            lock (this)
            {
                Stream strm = File.Open("C:\\testfile.txt", FileMode.OpenOrCreate); 
                while (true)
                {
                thisRead = Nw.Read(dataByte, 0, Blocksize);
                strm.Write(dataByte, 0, thisRead);
                if (thisRead == 0)
                break;
                }
                strm.Close();
                if (listBox1.InvokeRequired)
                {
                    additem1 mydel = new additem1(additem2);
                    listBox1.BeginInvoke(mydel,"Transfer Done");
                }
                handlerSocket = null;
            }
        }

You'll need to send it, just like you are the file itself. Just send it first, then send the file.

You'll need to send it, just like you are the file itself. Just send it first, then send the file.

is sending the filename just the same as sending the file? using network stream to write?
I'm quite new to this programming language, care to show any example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.